Now displays the unknown key entered

Signed-off-by: Hanif Bin Ariffin <hanif.ariffin.4326@gmail.com>
This commit is contained in:
Hanif Bin Ariffin 2021-06-12 18:25:14 +08:00
parent ee6419f11c
commit 28c6fad6e3

View file

@ -211,13 +211,13 @@ fn more(buff: &str, mut stdout: &mut Stdout, next_file: Option<&str>, silent: bo
let lines = break_buff(buff, usize::from(cols)); let lines = break_buff(buff, usize::from(cols));
let mut pager = Pager::new(rows, lines, next_file, silent); let mut pager = Pager::new(rows, lines, next_file, silent);
pager.draw(stdout, false); pager.draw(stdout, None);
if pager.should_close() { if pager.should_close() {
return; return;
} }
loop { loop {
let mut wrong_key = false; let mut wrong_key = None;
if event::poll(Duration::from_millis(10)).unwrap() { if event::poll(Duration::from_millis(10)).unwrap() {
match event::read().unwrap() { match event::read().unwrap() {
Event::Key(KeyEvent { Event::Key(KeyEvent {
@ -254,10 +254,11 @@ fn more(buff: &str, mut stdout: &mut Stdout, next_file: Option<&str>, silent: bo
Event::Resize(col, row) => { Event::Resize(col, row) => {
pager.page_resize(col, row); pager.page_resize(col, row);
} }
// FIXME: Need to fix, there are more than just unknown keys. Event::Key(KeyEvent {
_ => { code: KeyCode::Char(k),
wrong_key = true; ..
} }) => wrong_key = Some(k),
_ => continue,
} }
pager.draw(stdout, wrong_key); pager.draw(stdout, wrong_key);
@ -311,7 +312,7 @@ impl<'a> Pager<'a> {
self.content_rows = row.saturating_sub(1); self.content_rows = row.saturating_sub(1);
} }
fn draw(&self, stdout: &mut std::io::Stdout, wrong_key: bool) { fn draw(&self, stdout: &mut std::io::Stdout, wrong_key: Option<char>) {
let lower_mark = self let lower_mark = self
.line_count .line_count
.min(self.upper_mark.saturating_add(self.content_rows.into())); .min(self.upper_mark.saturating_add(self.content_rows.into()));
@ -335,7 +336,7 @@ impl<'a> Pager<'a> {
} }
} }
fn draw_prompt(&self, stdout: &mut Stdout, lower_mark: usize, wrong_key: bool) { fn draw_prompt(&self, stdout: &mut Stdout, lower_mark: usize, wrong_key: Option<char>) {
let status_inner = if lower_mark == self.line_count { let status_inner = if lower_mark == self.line_count {
format!("Next file: {}", self.next_file.unwrap_or_default()) format!("Next file: {}", self.next_file.unwrap_or_default())
} else { } else {
@ -348,10 +349,15 @@ impl<'a> Pager<'a> {
let status = format!("--More--({})", status_inner); let status = format!("--More--({})", status_inner);
let banner = match (self.silent, wrong_key) { let banner = match (self.silent, wrong_key) {
(true, true) => "[Press 'h' for instructions. (unimplemented)]".to_string(), (true, Some(key)) => {
(true, false) => format!("{}[Press space to continue, 'q' to quit.]", status), format!(
(false, true) => format!("{}{}", status, BELL), "{} [Unknown key: '{}'. Press 'h' for instructions. (unimplemented)]",
(false, false) => status, status, key
)
}
(true, None) => format!("{}[Press space to continue, 'q' to quit.]", status),
(false, Some(_)) => format!("{}{}", status, BELL),
(false, None) => status,
}; };
write!( write!(