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<()> {
let mut mgr = PromptManager::new(stdin(), stdout())?;
mgr.prompt_input("Mnemonic: ")?;
mgr.prompt_passphrase("Passphrase: ")?;
mgr.prompt_message("Please press enter.")?;
mgr.prompt_passphrase("Passphrase: ")?;
mgr.prompt_message("Please press space bar.")?;
Ok(())
}

View File

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