use crate::diagnostic::EnkryptitOutput;
use crate::errors::EnkryptitError;
use crate::frontend::cli::show_params;
use crate::frontend::tui::input::TuiInput;
use crate::parameters::params::{EnkryptitParams, load_params, save_params};
use crate::types::CompressionType::{self, Lz4, NoComp, Xz};
use crate::types::KeyParams::{File, Os, PassWord};
use crate::types::ParallelismType;
use colored::*;
pub fn launch_params(input: &impl TuiInput) -> Result<(), EnkryptitError> {
println!("\n{}", "Parameters Panel".cyan().bold());
loop {
let choices = vec![
"Change compression type",
"Change key type",
"Change parallelism type",
"Show current parameters",
"Back to main menu",
];
match input.select("What do you want to configure?", &choices) {
Ok(choice) if choice == "Change compression type" => change_compression(input)?,
Ok(choice) if choice == "Change key type" => change_key_type(input)?,
Ok(choice) if choice == "Change parallelism type" => change_parallelism(input)?,
Ok(choice) if choice == "Show current parameters" => show_current_params()?,
Ok(choice) if choice == "Back to main menu" => break,
Err(_) => {
tracing::info!("Selection cancelled");
}
_ => continue,
}
}
EnkryptitOutput::success("Parameters updated !").display();
Ok(())
}
fn change_compression(input: &impl TuiInput) -> Result<(), EnkryptitError> {
let old_params = load_params()?;
let choices = vec![
"Auto (automatically choosed by Enkryptit!)",
"Zstd (balanced)",
"Lz4 (fastest)",
"Xz (best compression)",
"No compression",
];
let choice = input.select("Select compression type:", &choices)?;
let compression = match choice.as_str() {
"Auto (automatically choosed by Enkryptit!)" => CompressionType::Auto,
"Zstd (balanced)" => CompressionType::Zstd,
"Lz4 (fastest)" => Lz4,
"Xz (best compression)" => Xz,
"No compression" => NoComp,
_ => CompressionType::Zstd,
};
let params = EnkryptitParams::new(old_params.key_params, compression, old_params.parallelism);
save_params(¶ms)?;
EnkryptitOutput::success(format!("Compression changed to {:?}", compression)).display();
Ok(())
}
fn change_key_type(input: &impl TuiInput) -> Result<(), EnkryptitError> {
let old_params = load_params()?;
let choices = vec!["Password (Argon2id)", "OS Keyring", "Key File"];
let choice = input.select("Select key type:", &choices)?;
let kt = match choice.as_str() {
"Password (Argon2id)" => PassWord,
"OS Keyring" => Os,
"Key File" => File,
_ => PassWord,
};
let params = EnkryptitParams::new(kt.clone(), old_params.compression, old_params.parallelism);
save_params(¶ms)?;
EnkryptitOutput::success(format!("Key type changed to {:?}", kt)).display();
Ok(())
}
fn change_parallelism(input: &impl TuiInput) -> Result<(), EnkryptitError> {
let old_params = load_params()?;
let choices = vec![
"Auto (automatically choosed by Enkryptit!)",
"Single",
"MultiThread",
];
let choice = input.select("Select parallelism type:", &choices)?;
let pt = match choice.as_str() {
"Auto (automatically choosed by Enkryptit!)" => ParallelismType::Auto,
"Single" => ParallelismType::Single,
"MultiThread" => ParallelismType::MultiThread(choose_threads(input)?),
_ => ParallelismType::Single,
};
let params = EnkryptitParams::new(old_params.key_params, old_params.compression, pt);
save_params(¶ms)?;
EnkryptitOutput::success(format!("Parallelism type changed to {:?}", pt)).display();
Ok(())
}
fn choose_threads(input: &impl TuiInput) -> Result<u8, EnkryptitError> {
let choosed =
input.custom_counter("Enter the number of threads you want (recommended : 4-16) :")?;
Ok(choosed)
}
fn show_current_params() -> Result<(), EnkryptitError> {
let params = load_params()?;
show_params(¶ms.key_params, ¶ms.compression, ¶ms.parallelism);
Ok(())
}