Skip to content

Commit

Permalink
Merge pull request #12 from ikanago/11-quit-key
Browse files Browse the repository at this point in the history
Accept ESC and Ctrl-C to quit interactive mode
  • Loading branch information
ikanago authored Apr 25, 2022
2 parents 712190d + 984053f commit a4458db
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions src/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ use crate::font::Font;

use crossterm::{
cursor::{MoveLeft, MoveRight, MoveToNextLine, MoveToPreviousLine},
event::{poll, read, Event, KeyCode, KeyEvent},
style::Print,
event::{poll, read, Event, KeyCode, KeyEvent, KeyModifiers},
style::{Print, Stylize},
terminal::{disable_raw_mode, enable_raw_mode, Clear, ClearType},
ExecutableCommand, QueueableCommand,
};

pub enum Action {
Confirm,
Quit,
Update,
None,
}
Expand All @@ -30,6 +31,7 @@ pub struct Prompt {

impl Prompt {
const POLL_DURATION_MS: u64 = 50;
const PROMPT_SYMBOL: &'static str = "> ";

pub fn new(fonts: Vec<Font>) -> Self {
let converter = Converter::new(&fonts);
Expand All @@ -50,11 +52,12 @@ impl Prompt {

let mut stderr = io::stderr();
self.initialize_prompt(&mut stderr)?;
self.render_input(&mut stderr)?;

self.start_event_loop(&mut stderr)?;

io::stdout()
.execute(MoveLeft(self.input.len() as u16))?
.execute(MoveLeft(self.input_line_len()))?
.execute(Clear(ClearType::CurrentLine))?
.execute(Print(format!(
"{}\r\n",
Expand Down Expand Up @@ -91,13 +94,17 @@ impl Prompt {
Action::Confirm => {
break;
}
Action::Quit => {
self.input = Vec::new();
break;
}
Action::Update => {
self.render_input(w)?;

self.render_candidates(w)?;

w.execute(MoveToPreviousLine((self.num_whole_lines - 1) as u16))?
.execute(MoveRight(self.input.len() as u16))?;
.execute(MoveRight(self.input_line_len()))?;
}
Action::None => {}
}
Expand All @@ -106,14 +113,20 @@ impl Prompt {
Ok(())
}

fn input_line_len(&self) -> u16 {
(Self::PROMPT_SYMBOL.len() + self.input.len()) as u16
}

fn handle_key_event<W>(&mut self, w: &mut W) -> crossterm::Result<Action>
where
W: Write,
{
if poll(Duration::from_millis(Self::POLL_DURATION_MS))? {
if let Event::Key(KeyEvent { code, .. }) = read()? {
if let Event::Key(KeyEvent { code, modifiers }) = read()? {
let action = match code {
KeyCode::Enter => Action::Confirm,
KeyCode::Esc => Action::Quit,
KeyCode::Char('c') if modifiers == KeyModifiers::CONTROL => Action::Quit,
KeyCode::Backspace => {
w.execute(MoveLeft(1))?;
self.input.pop();
Expand Down Expand Up @@ -152,9 +165,13 @@ impl Prompt {
where
W: Write,
{
w.execute(MoveLeft(self.input.len() as u16))?
w.execute(MoveLeft(self.input_line_len()))?
.execute(Clear(ClearType::CurrentLine))?
.execute(Print(self.input.iter().collect::<String>()))?;
.execute(Print(format!(
"{}{}",
Self::PROMPT_SYMBOL.blue(),
self.input.iter().collect::<String>()
)))?;
Ok(())
}

Expand All @@ -163,13 +180,13 @@ impl Prompt {
W: Write,
{
for i in 0..self.fonts.len() {
let selection = if i == self.current_font { 'x' } else { ' ' };
let selection = if i == self.current_font { "> " } else { " " };

w.queue(MoveToNextLine(1))?
.queue(Clear(ClearType::CurrentLine))?
.queue(Print(format!(
"[{}]{}",
selection,
"{}{}",
selection.red(),
self.converter.convert(&self.input, self.fonts[i])
)))?;
}
Expand Down

0 comments on commit a4458db

Please sign in to comment.