keyfork-prompt: make prompt_message show up nicer

This commit is contained in:
Ryan Heywood 2023-12-21 14:02:42 -05:00
parent 1ac99e16f8
commit be74cd8ad1
Signed by: ryan
GPG Key ID: 8E401478A3FBEF72
2 changed files with 19 additions and 10 deletions

View File

@ -5,8 +5,8 @@ use keyfork_prompt::*;
pub fn main() -> Result<()> { pub fn main() -> Result<()> {
let mut mgr = PromptManager::new(stdin(), stdout())?; let mut mgr = PromptManager::new(stdin(), stdout())?;
mgr.prompt_input("Mnemonic: ")?; mgr.prompt_input("Mnemonic: ")?;
mgr.prompt_passphrase("Passphrase: ")?;
mgr.prompt_message("Please press enter.")?; mgr.prompt_message("Please press enter.")?;
mgr.prompt_passphrase("Passphrase: ")?;
mgr.prompt_message("Please press space bar.")?; mgr.prompt_message("Please press space bar.")?;
Ok(()) Ok(())
} }

View File

@ -5,10 +5,11 @@ use std::{
use crossterm::{ use crossterm::{
event::{read, Event, KeyCode}, event::{read, Event, KeyCode},
style::Print, style::{Print, PrintStyledContent, Stylize},
terminal, terminal,
cursor,
tty::IsTty, tty::IsTty,
ExecutableCommand, QueueableCommand,
}; };
mod alternate_screen; mod alternate_screen;
@ -50,8 +51,9 @@ where
pub fn prompt_input(&mut self, prompt: &str) -> Result<String> { pub fn prompt_input(&mut self, prompt: &str) -> Result<String> {
let mut terminal = AlternateScreen::new(&mut self.write)?; let mut terminal = AlternateScreen::new(&mut self.write)?;
terminal terminal
.execute(terminal::Clear(terminal::ClearType::All))? .queue(terminal::Clear(terminal::ClearType::All))?
.execute(Print(prompt))?; .queue(Print(prompt))?
.flush()?;
let mut line = String::new(); let mut line = String::new();
self.read.read_line(&mut line)?; self.read.read_line(&mut line)?;
Ok(line) Ok(line)
@ -62,8 +64,9 @@ where
let mut terminal = AlternateScreen::new(&mut self.write)?; let mut terminal = AlternateScreen::new(&mut self.write)?;
let mut terminal = RawMode::new(&mut terminal)?; let mut terminal = RawMode::new(&mut terminal)?;
terminal terminal
.execute(terminal::Clear(terminal::ClearType::All))? .queue(terminal::Clear(terminal::ClearType::All))?
.execute(Print(prompt))?; .queue(Print(prompt))?
.flush()?;
let mut passphrase = String::new(); let mut passphrase = String::new();
loop { loop {
match read()? { match read()? {
@ -73,7 +76,7 @@ where
break; break;
} }
KeyCode::Char(c) => { KeyCode::Char(c) => {
terminal.execute(Print("*"))?; terminal.queue(Print("*"))?.flush()?;
passphrase.push(c); passphrase.push(c);
} }
_ => (), _ => (),
@ -88,8 +91,13 @@ where
let mut terminal = AlternateScreen::new(&mut self.write)?; let mut terminal = AlternateScreen::new(&mut self.write)?;
let mut terminal = RawMode::new(&mut terminal)?; let mut terminal = RawMode::new(&mut terminal)?;
terminal terminal
.execute(terminal::Clear(terminal::ClearType::All))? .queue(terminal::Clear(terminal::ClearType::All))?
.execute(Print(prompt))?; .queue(Print(prompt))?
.queue(cursor::DisableBlinking)?
.queue(cursor::MoveDown(1))?
.queue(cursor::MoveToColumn(0))?
.queue(PrintStyledContent(" OK ".negative()))?
.flush()?;
loop { loop {
match read()? { match read()? {
Event::Key(k) => match k.code { Event::Key(k) => match k.code {
@ -99,6 +107,7 @@ where
_ => (), _ => (),
} }
} }
terminal.queue(cursor::EnableBlinking)?.flush()?;
Ok(()) Ok(())
} }
} }