diff --git a/Cargo.lock b/Cargo.lock index 1c4be6b..70b25ba 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -474,6 +474,19 @@ dependencies = [ "cipher", ] +[[package]] +name = "dialoguer" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "658bce805d770f407bc62102fca7c2c64ceef2fbcb2b8bd19d2765ce093980de" +dependencies = [ + "console", + "shell-words", + "tempfile", + "thiserror", + "zeroize", +] + [[package]] name = "digest" version = "0.10.7" @@ -581,6 +594,12 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "fastrand" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5" + [[package]] name = "ff" version = "0.13.0" @@ -1584,6 +1603,12 @@ dependencies = [ "digest", ] +[[package]] +name = "shell-words" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24188a676b6ae68c3b2cb3a01be17fbf7240ce009799bb56d5b1409051e78fde" + [[package]] name = "signature" version = "2.2.0" @@ -1705,6 +1730,19 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "tempfile" +version = "3.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01ce4141aa927a6d1bd34a041795abd0db1cccba5d5f24b009f694bdf3a1f3fa" +dependencies = [ + "cfg-if", + "fastrand", + "redox_syscall", + "rustix", + "windows-sys 0.52.0", +] + [[package]] name = "term" version = "0.7.0" @@ -1788,13 +1826,14 @@ dependencies = [ [[package]] name = "tiny-encrypt" -version = "1.7.4" +version = "1.7.5" dependencies = [ "aes-gcm-stream", "base64", "chacha20-poly1305-stream", "chrono", "clap", + "dialoguer", "flate2", "fs-set-times", "hex", diff --git a/Cargo.toml b/Cargo.toml index d486bb5..97efb97 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tiny-encrypt" -version = "1.7.4" +version = "1.7.5" edition = "2021" license = "MIT" description = "A simple and tiny file encrypt tool" @@ -50,6 +50,7 @@ pqcrypto-kyber = "0.8.0" pqcrypto-traits = "0.3.5" pinentry = "0.5.0" secrecy = "0.8.0" +dialoguer = "0.11.0" [build-dependencies] swift-rs = { path = "swift-rs", features = ["build"], optional = true } diff --git a/README.md b/README.md index caac8db..53737da 100644 --- a/README.md +++ b/README.md @@ -149,6 +149,7 @@ Environment | TINY_ENCRYPT_DEFAULT_COMPRESS | File compress, `1` or `on`, default `false` | | TINY_ENCRYPT_NO_PROGRESS | Do not display progress bar | | TINY_ENCRYPT_NO_DEFAULT_PIN_HINT | Do not display default PIN hint | +| TINY_ENCRYPT_USE_DIALOGUER | Use dialoguer | | TINY_ENCRYPT_PIN | PIV Card PIN | | TINY_ENCRYPT_KEY_ID | Default Key ID | | TINY_ENCRYPT_AUTO_SELECT_KEY_IDS | Auto select Key IDs | diff --git a/src/cmd_decrypt.rs b/src/cmd_decrypt.rs index f356bbe..4b5a29b 100644 --- a/src/cmd_decrypt.rs +++ b/src/cmd_decrypt.rs @@ -7,6 +7,8 @@ use std::process::Command; use std::time::{Instant, SystemTime}; use clap::Args; +use dialoguer::Select; +use dialoguer::theme::ColorfulTheme; use flate2::Compression; use openpgp_card::crypto_data::Cryptogram; use rust_util::{ @@ -743,11 +745,26 @@ pub fn select_envelop<'a>(meta: &'a TinyEncryptMeta, key_id: &Option, co } } - // TODO optimize select for exec-env - envelops.iter().enumerate().for_each(|(i, envelop)| { - println_ex!("#{} {}", i + 1, util_envelop::format_envelop(envelop, config)); - }); - let envelop_number = util::read_number("Please select an envelop:", 1, envelops.len()); + let use_dialoguer = util_env::get_use_dialoguer(); + let envelop_number = if use_dialoguer { + let format_envelops = envelops.iter().map(|envelop| { + format!("#{}", util_envelop::format_envelop(envelop, config)) + }).collect::>(); + // TODO catch ctrl-c Term::show_cursor() + let select_result = Select::with_theme(&ColorfulTheme::default()) + .with_prompt("Please select envelop: ") + .items(&format_envelops[..]) + .default(0) + .report(false) + .clear(true) + .interact(); + opt_result!(select_result, "Select envelop error: {}") + 1 + } else { + envelops.iter().enumerate().for_each(|(i, envelop)| { + println_ex!("#{} {}", i + 1, util_envelop::format_envelop(envelop, config)); + }); + util::read_number("Please select an envelop:", 1, envelops.len()) + }; let selected_envelop = &envelops[envelop_number - 1]; if silent { diff --git a/src/util_env.rs b/src/util_env.rs index a469d04..c339ae6 100644 --- a/src/util_env.rs +++ b/src/util_env.rs @@ -8,6 +8,7 @@ use crate::consts; pub const TINY_ENCRYPT_ENV_DEFAULT_ALGORITHM: &str = "TINY_ENCRYPT_DEFAULT_ALGORITHM"; pub const TINY_ENCRYPT_ENV_DEFAULT_COMPRESS: &str = "TINY_ENCRYPT_DEFAULT_COMPRESS"; pub const TINY_ENCRYPT_ENV_NO_PROGRESS: &str = "TINY_ENCRYPT_NO_PROGRESS"; +pub const TINY_ENCRYPT_ENV_USE_DIALOGUER: &str = "TINY_ENCRYPT_USE_DIALOGUER"; pub const TINY_ENCRYPT_ENV_PIN: &str = "TINY_ENCRYPT_PIN"; pub const TINY_ENCRYPT_ENV_KEY_ID: &str = "TINY_ENCRYPT_KEY_ID"; pub const TINY_ENCRYPT_ENV_AUTO_SELECT_KEY_IDS: &str = "TINY_ENCRYPT_AUTO_SELECT_KEY_IDS"; @@ -61,4 +62,8 @@ pub fn get_no_progress() -> bool { pub fn get_no_default_pin_hint() -> bool { rust_util_env::is_env_on(TINY_ENCRYPT_ENV_NO_DEFAULT_PIN_HINT) -} \ No newline at end of file +} + +pub fn get_use_dialoguer() -> bool { + rust_util_env::is_env_on(TINY_ENCRYPT_ENV_USE_DIALOGUER) +}