keyfork-prompt: add bracketed paste

This commit is contained in:
Ryan Heywood 2024-01-01 16:57:57 -05:00
parent d7f33874f6
commit 3190ba97db
Signed by: ryan
GPG Key ID: 8E401478A3FBEF72
2 changed files with 11 additions and 3 deletions

View File

@ -10,6 +10,6 @@ default = ["mnemonic"]
mnemonic = ["keyfork-mnemonic-util"]
[dependencies]
crossterm = { version = "0.27.0", default-features = false, features = ["use-dev-tty", "events"] }
crossterm = { version = "0.27.0", default-features = false, features = ["use-dev-tty", "events", "bracketed-paste"] }
keyfork-mnemonic-util = { version = "0.1.0", path = "../keyfork-mnemonic-util", optional = true }
thiserror = "1.0.51"

View File

@ -8,7 +8,7 @@ use keyfork_mnemonic_util::Wordlist;
use crossterm::{
cursor,
event::{read, Event, KeyCode, KeyModifiers},
event::{read, DisableBracketedPaste, EnableBracketedPaste, Event, KeyCode, KeyModifiers},
style::{Print, PrintStyledContent, Stylize},
terminal,
tty::IsTty,
@ -77,6 +77,7 @@ where
Ok(line)
}
// TODO: create a wrapper for bracketed paste similar to RawMode
#[cfg(feature = "mnemonic")]
pub fn prompt_wordlist(&mut self, prompt: &str, wordlist: &Wordlist) -> Result<String> {
let mut terminal = AlternateScreen::new(&mut self.write)?;
@ -84,7 +85,8 @@ where
terminal
.queue(terminal::Clear(terminal::ClearType::All))?
.queue(cursor::MoveTo(0, 0))?;
.queue(cursor::MoveTo(0, 0))?
.queue(EnableBracketedPaste)?;
let mut lines = prompt.lines().peekable();
let mut prefix_length = 0;
while let Some(line) = lines.next() {
@ -107,6 +109,10 @@ where
cols = new_cols;
_rows = new_rows;
}
Event::Paste(mut p) => {
p.retain(|c| c != '\n');
input.push_str(&p);
}
Event::Key(k) => match k.code {
KeyCode::Enter => {
input.push('\n');
@ -184,6 +190,8 @@ where
terminal.flush()?;
}
terminal.queue(DisableBracketedPaste)?.flush()?;
Ok(input)
}