mirror of
https://github.com/lotabout/skim.git
synced 2026-09-10 07:16:23 -04:00
[input] fix stdin conflict between input and reader
Input used `wget_wch` to fetch keys pressed which occupys stdin of terminal, thus causes reader failure in reading from stdin. So fzf-rs won't be able to handle input from stdin. Now the input is read from `/dev/tty` file. Requires nightly to build.
This commit is contained in:
parent
5147927e74
commit
6816c22d00
|
|
@ -1,2 +1,4 @@
|
|||
# fzf-rs
|
||||
An experimental clone of fzf in rust
|
||||
|
||||
Requires nightly version
|
||||
|
|
|
|||
67
src/input.rs
67
src/input.rs
|
|
@ -3,12 +3,12 @@
|
|||
|
||||
use std::sync::Arc;
|
||||
use std::char;
|
||||
use std::io::prelude::*;
|
||||
use std::fs::File;
|
||||
|
||||
use util::eventbox::EventBox;
|
||||
use event::Event;
|
||||
|
||||
use ncurses::*;
|
||||
|
||||
pub struct Input {
|
||||
query: Vec<char>,
|
||||
index: usize, // index in chars
|
||||
|
|
@ -47,53 +47,40 @@ impl Input {
|
|||
}
|
||||
|
||||
pub fn run(&mut self) {
|
||||
loop {
|
||||
self.handle_char();
|
||||
let f = File::open("/dev/tty").unwrap();
|
||||
for c in f.chars() {
|
||||
self.handle_char(c.unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
// fetch input from curses and turn it into query.
|
||||
fn handle_char(&mut self) {
|
||||
let ch = wget_wch(stdscr);
|
||||
|
||||
fn handle_char(&mut self, ch: char) {
|
||||
match ch {
|
||||
Some(WchResult::KeyCode(_)) => {
|
||||
// will later handle readline-like shortcuts
|
||||
'\x7F' => { // backspace
|
||||
self.delete_char();
|
||||
self.eb.set(Event::EvQueryChange, Box::new((self.get_query(), self.pos)));
|
||||
}
|
||||
|
||||
Some(WchResult::Char(c)) => {
|
||||
/* Enable attributes and output message. */
|
||||
let ch = char::from_u32(c as u32).expect("Invalid char");
|
||||
match ch {
|
||||
'\x7F' => { // backspace
|
||||
self.delete_char();
|
||||
self.eb.set(Event::EvQueryChange, Box::new((self.get_query(), self.pos)));
|
||||
}
|
||||
|
||||
'\x0A' => { // enter
|
||||
self.eb.set(Event::EvInputSelect, Box::new(true));
|
||||
}
|
||||
|
||||
'\x09' => { // tab
|
||||
self.eb.set(Event::EvInputToggle, Box::new(true));
|
||||
}
|
||||
|
||||
'\x10' => { // ctrl-p
|
||||
self.eb.set(Event::EvInputUp, Box::new(true));
|
||||
}
|
||||
|
||||
'\x0E' => { // ctrl-n
|
||||
self.eb.set(Event::EvInputDown, Box::new(true));
|
||||
}
|
||||
|
||||
ch => { // other characters
|
||||
self.add_char(ch);
|
||||
self.eb.set(Event::EvQueryChange, Box::new((self.get_query(), self.pos)));
|
||||
}
|
||||
}
|
||||
'\x0D' => { // enter
|
||||
self.eb.set(Event::EvInputSelect, Box::new(true));
|
||||
}
|
||||
|
||||
None => { }
|
||||
'\x09' => { // tab
|
||||
self.eb.set(Event::EvInputToggle, Box::new(true));
|
||||
}
|
||||
|
||||
'\x10' => { // ctrl-p
|
||||
self.eb.set(Event::EvInputUp, Box::new(true));
|
||||
}
|
||||
|
||||
'\x0E' => { // ctrl-n
|
||||
self.eb.set(Event::EvInputDown, Box::new(true));
|
||||
}
|
||||
|
||||
ch => { // other characters
|
||||
self.add_char(ch);
|
||||
self.eb.set(Event::EvQueryChange, Box::new((self.get_query(), self.pos)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
#![feature(io)]
|
||||
extern crate ncurses;
|
||||
mod util;
|
||||
mod item;
|
||||
|
|
|
|||
|
|
@ -55,6 +55,10 @@ impl Model {
|
|||
|
||||
pub fn toggle_select(&self, selected: Option<bool>) {
|
||||
let mut items = self.items.write().unwrap();
|
||||
if items.len() <= 0 {
|
||||
return;
|
||||
}
|
||||
|
||||
items[self.matched_items[self.item_cursor].index].toggle_select(selected);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue