From 3190ba97dbf1007a252fa79ebff783a9c267ca65 Mon Sep 17 00:00:00 2001 From: ryan Date: Mon, 1 Jan 2024 16:57:57 -0500 Subject: [PATCH] keyfork-prompt: add bracketed paste --- keyfork-prompt/Cargo.toml | 2 +- keyfork-prompt/src/lib.rs | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/keyfork-prompt/Cargo.toml b/keyfork-prompt/Cargo.toml index 4ab511d..b69772e 100644 --- a/keyfork-prompt/Cargo.toml +++ b/keyfork-prompt/Cargo.toml @@ -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" diff --git a/keyfork-prompt/src/lib.rs b/keyfork-prompt/src/lib.rs index b9ea5a5..ef45874 100644 --- a/keyfork-prompt/src/lib.rs +++ b/keyfork-prompt/src/lib.rs @@ -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 { 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) }