Skip to content

Commit 9e7e7e7

Browse files
committed
Capture key input without Enter #7
1 parent 584b098 commit 9e7e7e7

File tree

4 files changed

+13
-25
lines changed

4 files changed

+13
-25
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ statistics = []
2929
[dependencies]
3030
crossbeam-channel = "0.1.2"
3131
ctrlc = "3.1.0"
32+
getch = "0.2.0"
3233
glob = "0.2.11"
3334
lazy_static = "1.0.0"
3435
memmap = "0.6.2"

src/ignore.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use glob::{MatchOptions, Pattern};
22
use std::fs::File;
3-
use std::io::BufReader;
43
use std::io::prelude::*;
4+
use std::io::BufReader;
55
use std::path::PathBuf;
66

77
// ---------------------------------------------------------------------------------------------------------------------

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
extern crate crossbeam_channel;
44
extern crate ctrlc;
5+
extern crate getch;
56
extern crate glob;
67
extern crate memmap;
78
extern crate num_cpus;

src/pipeline_replacer.rs

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use console::{Console, ConsoleTextKind};
22
use crossbeam_channel::{Receiver, Sender};
33
use ctrlc;
4+
use getch::Getch;
45
use memmap::Mmap;
56
use pipeline::{Pipeline, PipelineInfo};
67
use pipeline_matcher::PathMatch;
78
use std::fs::{self, File};
8-
use std::io;
99
use std::io::{Error, Write};
1010
use std::ops::Deref;
1111
use std::time::{Duration, Instant};
@@ -113,38 +113,24 @@ impl PipelineReplacer {
113113
"Replace keyword? ( Yes[Y], No[N], All[A], Quit[Q] ):",
114114
);
115115
self.console.flush();
116-
let mut buf = String::new();
117-
io::stdin().read_line(&mut buf).unwrap();
118-
match buf.trim().to_lowercase().as_ref() {
119-
"y" => {
116+
let getch = Getch::new();
117+
let key = getch.getch()?;
118+
let key = char::from(key);
119+
self.console.write(ConsoleTextKind::Other, &format!("{}\n", key));
120+
match key.to_ascii_lowercase() {
121+
'y' => {
120122
do_replace = true;
121123
break;
122124
}
123-
"yes" => {
124-
do_replace = true;
125-
break;
126-
}
127-
"n" => {
128-
do_replace = false;
129-
break;
130-
}
131-
"no" => {
125+
'n' => {
132126
do_replace = false;
133127
break;
134128
}
135-
"a" => {
129+
'a' => {
136130
self.all_replace = true;
137131
break;
138132
}
139-
"all" => {
140-
self.all_replace = true;
141-
break;
142-
}
143-
"q" => {
144-
let _ = tmpfile.close();
145-
exit(0, &mut self.console);
146-
}
147-
"quit" => {
133+
'q' => {
148134
let _ = tmpfile.close();
149135
exit(0, &mut self.console);
150136
}

0 commit comments

Comments
 (0)