Merge pull request #704 from pka/fix-build-without-crossterm

Fix build without crossterm
This commit is contained in:
Jonathan Turner 2019-09-25 12:46:06 +12:00 committed by GitHub
commit 8c240ca3fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 69 additions and 63 deletions

View File

@ -359,7 +359,8 @@ pub async fn cli() -> Result<(), Box<dyn Error>> {
// Register Ctrl-r for history fuzzy search // Register Ctrl-r for history fuzzy search
// rustyline doesn't support custom commands, so we override Ctrl-D (EOF) // rustyline doesn't support custom commands, so we override Ctrl-D (EOF)
#[cfg(not(windows))] // https://github.com/nushell/nushell/issues/689 // https://github.com/nushell/nushell/issues/689
#[cfg(all(not(windows), feature = "crossterm"))]
rl.bind_sequence(rustyline::KeyPress::Ctrl('R'), rustyline::Cmd::EndOfFile); rl.bind_sequence(rustyline::KeyPress::Ctrl('R'), rustyline::Cmd::EndOfFile);
// Redefine Ctrl-D to same command as Ctrl-C // Redefine Ctrl-D to same command as Ctrl-C
rl.bind_sequence(rustyline::KeyPress::Ctrl('D'), rustyline::Cmd::Interrupt); rl.bind_sequence(rustyline::KeyPress::Ctrl('D'), rustyline::Cmd::Interrupt);

View File

@ -1,4 +1,5 @@
use ansi_term::{ANSIString, ANSIStrings, Colour, Style}; use ansi_term::{ANSIString, ANSIStrings, Colour, Style};
#[cfg(feature = "crossterm")]
use crossterm::{cursor, terminal, ClearType, InputEvent, KeyEvent, RawScreen}; use crossterm::{cursor, terminal, ClearType, InputEvent, KeyEvent, RawScreen};
use std::io::Write; use std::io::Write;
use sublime_fuzzy::best_match; use sublime_fuzzy::best_match;
@ -18,76 +19,78 @@ pub fn interactive_fuzzy_search(lines: &Vec<&str>, max_results: usize) -> Select
Edit(String), Edit(String),
} }
let mut state = State::Selecting; let mut state = State::Selecting;
if let Ok(_raw) = RawScreen::into_raw_mode() { #[cfg(feature = "crossterm")]
// User input for search {
let mut searchinput = String::new(); if let Ok(_raw) = RawScreen::into_raw_mode() {
let mut selected = 0; // User input for search
let mut searchinput = String::new();
let mut selected = 0;
let mut cursor = cursor(); let mut cursor = cursor();
let _ = cursor.hide(); let _ = cursor.hide();
let input = crossterm::input(); let input = crossterm::input();
let mut sync_stdin = input.read_sync(); let mut sync_stdin = input.read_sync();
while state == State::Selecting { while state == State::Selecting {
let mut selected_lines = fuzzy_search(&searchinput, &lines, max_results); let mut selected_lines = fuzzy_search(&searchinput, &lines, max_results);
let num_lines = selected_lines.len(); let num_lines = selected_lines.len();
paint_selection_list(&selected_lines, selected); paint_selection_list(&selected_lines, selected);
if let Some(ev) = sync_stdin.next() { if let Some(ev) = sync_stdin.next() {
match ev { match ev {
InputEvent::Keyboard(k) => match k { InputEvent::Keyboard(k) => match k {
KeyEvent::Esc | KeyEvent::Ctrl('c') => { KeyEvent::Esc | KeyEvent::Ctrl('c') => {
state = State::Quit; state = State::Quit;
}
KeyEvent::Up => {
if selected > 0 {
selected -= 1;
} }
} KeyEvent::Up => {
KeyEvent::Down => { if selected > 0 {
if selected + 1 < selected_lines.len() { selected -= 1;
selected += 1; }
} }
} KeyEvent::Down => {
KeyEvent::Char('\n') => { if selected + 1 < selected_lines.len() {
state = if selected_lines.len() > 0 { selected += 1;
State::Selected(selected_lines.remove(selected).text) }
} else { }
State::Edit("".to_string()) KeyEvent::Char('\n') => {
}; state = if selected_lines.len() > 0 {
} State::Selected(selected_lines.remove(selected).text)
KeyEvent::Char('\t') | KeyEvent::Right => { } else {
state = if selected_lines.len() > 0 { State::Edit("".to_string())
State::Edit(selected_lines.remove(selected).text) };
} else { }
State::Edit("".to_string()) KeyEvent::Char('\t') | KeyEvent::Right => {
}; state = if selected_lines.len() > 0 {
} State::Edit(selected_lines.remove(selected).text)
KeyEvent::Char(ch) => { } else {
searchinput.push(ch); State::Edit("".to_string())
selected = 0; };
} }
KeyEvent::Backspace => { KeyEvent::Char(ch) => {
searchinput.pop(); searchinput.push(ch);
selected = 0; selected = 0;
} }
_ => { KeyEvent::Backspace => {
// println!("OTHER InputEvent: {:?}", k); searchinput.pop();
} selected = 0;
}, }
_ => {} _ => {
// println!("OTHER InputEvent: {:?}", k);
}
},
_ => {}
}
}
if num_lines > 0 {
cursor.move_up(num_lines as u16);
} }
} }
if num_lines > 0 { let (_x, y) = cursor.pos();
cursor.move_up(num_lines as u16); let _ = cursor.goto(0, y - 1);
} let _ = cursor.show();
let _ = RawScreen::disable_raw_mode();
} }
let (_x, y) = cursor.pos(); terminal().clear(ClearType::FromCursorDown).unwrap();
let _ = cursor.goto(0, y - 1);
let _ = cursor.show();
let _ = RawScreen::disable_raw_mode();
} }
terminal().clear(ClearType::FromCursorDown).unwrap();
match state { match state {
State::Selected(line) => SelectionResult::Selected(line), State::Selected(line) => SelectionResult::Selected(line),
State::Edit(line) => SelectionResult::Edit(line), State::Edit(line) => SelectionResult::Edit(line),
@ -132,6 +135,7 @@ pub fn fuzzy_search(searchstr: &str, lines: &Vec<&str>, max_results: usize) -> V
results results
} }
#[cfg(feature = "crossterm")]
fn highlight(textmatch: &Match, normal: Style, highlighted: Style) -> Vec<ANSIString> { fn highlight(textmatch: &Match, normal: Style, highlighted: Style) -> Vec<ANSIString> {
let text = &textmatch.text; let text = &textmatch.text;
let mut ansi_strings = vec![]; let mut ansi_strings = vec![];
@ -147,6 +151,7 @@ fn highlight(textmatch: &Match, normal: Style, highlighted: Style) -> Vec<ANSISt
ansi_strings ansi_strings
} }
#[cfg(feature = "crossterm")]
fn paint_selection_list(lines: &Vec<Match>, selected: usize) { fn paint_selection_list(lines: &Vec<Match>, selected: usize) {
let terminal = terminal(); let terminal = terminal();
let size = terminal.terminal_size(); let size = terminal.terminal_size();