From 521b35eedd1e438c5946a89f4909c95185c6401b Mon Sep 17 00:00:00 2001 From: Srevin Saju Date: Tue, 22 Feb 2022 09:53:01 +0300 Subject: [PATCH] fix: use dialoguer for reading password and username --- Cargo.lock | 50 +++++++++++++++++++++++--- scrob-cli/Cargo.toml | 2 +- scrob-cli/src/main.rs | 32 +++++++++++++---- scrob-core/src/integrations/discord.rs | 6 +++- types/src/config.rs | 26 ++++++++++++++ 5 files changed, 103 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 222bd8f..8c49f14 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -309,6 +309,21 @@ dependencies = [ "toml", ] +[[package]] +name = "console" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a28b32d32ca44b70c3e4acd7db1babf555fa026e385fb95f18028f88848b3c31" +dependencies = [ + "encode_unicode", + "libc", + "once_cell", + "regex", + "terminal_size", + "unicode-width", + "winapi 0.3.9", +] + [[package]] name = "const_fn" version = "0.4.8" @@ -578,6 +593,17 @@ dependencies = [ "syn 0.11.11", ] +[[package]] +name = "dialoguer" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "349d6b4fabcd9e97e1df1ae15395ac7e49fb144946a0d453959dc2696273b9da" +dependencies = [ + "console", + "tempfile", + "zeroize", +] + [[package]] name = "directories" version = "2.0.2" @@ -641,6 +667,12 @@ version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" +[[package]] +name = "encode_unicode" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" + [[package]] name = "encoding_rs" version = "0.8.29" @@ -2370,6 +2402,7 @@ dependencies = [ "clap", "config", "confy", + "dialoguer", "env_logger", "futures 0.3.18", "log", @@ -2377,7 +2410,6 @@ dependencies = [ "scrob-core", "serde", "serde_json", - "text_io", "types", ] @@ -2744,10 +2776,14 @@ dependencies = [ ] [[package]] -name = "text_io" -version = "0.1.9" +name = "terminal_size" +version = "0.1.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee74b5019b48991b09803402aaf9e65a053b3993fe9d9c475ab67a395358ba76" +checksum = "633c1a546cee861a1a6d0dc69ebeca693bf4296661ba7852b9d21d159e0506df" +dependencies = [ + "libc", + "winapi 0.3.9", +] [[package]] name = "textwrap" @@ -3615,6 +3651,12 @@ dependencies = [ "syn 1.0.82", ] +[[package]] +name = "zeroize" +version = "1.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c88870063c39ee00ec285a2f8d6a966e5b6fb2becc4e8dac77ed0d370ed6006" + [[package]] name = "zvariant" version = "2.10.0" diff --git a/scrob-cli/Cargo.toml b/scrob-cli/Cargo.toml index 1ca0c3d..6bcd8ff 100644 --- a/scrob-cli/Cargo.toml +++ b/scrob-cli/Cargo.toml @@ -17,8 +17,8 @@ async-trait = "0.1.21" futures = "0.3" env_logger = "0.9.0" clap = { version = "2.34.0", features = ["suggestions", "color"] } -text_io = "0.1.9" confy = "0.4.0" +dialoguer = "0.10.0" config = { path = "../config" } diff --git a/scrob-cli/src/main.rs b/scrob-cli/src/main.rs index 748393a..c6a5a8e 100644 --- a/scrob-cli/src/main.rs +++ b/scrob-cli/src/main.rs @@ -3,13 +3,19 @@ use clap::clap_app; use config as meta; use log::trace; -use text_io::read; +use dialoguer::{theme::ColorfulTheme, Input, Password}; use env_logger; use scrob_core::core::core; use scrob_core::Preferences; use types::config::ScrobConfig; +fn config_unix() { + let cfg: ScrobConfig = confy::load("scrob") + .expect("Error loading config. Have you logged in yet? Login with 'login' subcommand"); + todo!("Not implemented yet"); +} + fn main() { env_logger::init(); @@ -31,23 +37,35 @@ fn main() { trace!("Cli: {:?}", matches); if let Some(_) = matches.subcommand_matches("login") { - println!("Enter your last.fm username\n"); - // read until a newline (but not including it) - let username: String = read!("{}\n"); + let username: String = Input::with_theme(&ColorfulTheme::default()) + .with_prompt("last.fm username") + .interact_text() + .unwrap(); - println!("Enter your last.fm password\n"); - let password: String = read!("{}\n"); + let password = Password::with_theme(&ColorfulTheme::default()) + .with_prompt("password") + .interact() + .unwrap(); - println!("Logged in successfully!\n"); + println!("Login details saved to config folder"); let cfg = ScrobConfig { version: 1, password: password, username: username, + ..Default::default() }; confy::store("scrob", &cfg).expect("Failed to store config"); + return; + } + + if let Some(_) = matches.subcommand_matches("config") { + #[cfg(unix)] + config_unix(); + + return; } let prefs = Preferences { diff --git a/scrob-core/src/integrations/discord.rs b/scrob-core/src/integrations/discord.rs index dc75a45..6752ab8 100644 --- a/scrob-core/src/integrations/discord.rs +++ b/scrob-core/src/integrations/discord.rs @@ -58,7 +58,11 @@ impl BaseIntegrationTrait for Discord { } let app_desc = format!("{} {}", meta::APP_NAME, meta::APP_VERSION); - trace!("setting discord activity with image '{}' and app desc '{}'", song.source.as_str(), app_desc); + trace!( + "setting discord activity with image '{}' and app desc '{}'", + song.source.as_str(), + app_desc + ); let assets = activity::Assets::new() .large_image(song.source.as_str()) .large_text(app_desc.as_str()) diff --git a/types/src/config.rs b/types/src/config.rs index 4f9a66d..3fb0e5b 100644 --- a/types/src/config.rs +++ b/types/src/config.rs @@ -2,11 +2,27 @@ use serde::{Deserialize, Serialize}; use crate::integrations::{Event, Integrations}; +#[derive(Serialize, Deserialize, Clone)] +pub struct DiscordSettings { + pub enabled: bool, + pub blacklist_apps: Vec, + pub blacklist_urls: Vec, +} + +#[derive(Serialize, Deserialize, Clone)] +pub struct ScrobbleSettings { + pub enabled: bool, + pub blacklist_apps: Vec, + pub blacklist_urls: Vec, +} + #[derive(Serialize, Deserialize, Clone)] pub struct ScrobConfig { pub version: u8, pub password: String, pub username: String, + pub discord: DiscordSettings, + pub scrobble: ScrobbleSettings, } /// `MyConfig` implements `Default` @@ -16,6 +32,16 @@ impl ::std::default::Default for ScrobConfig { version: 1, password: "".into(), username: "".into(), + scrobble: ScrobbleSettings { + enabled: true, + blacklist_apps: vec![], + blacklist_urls: vec![], + }, + discord: DiscordSettings { + enabled: true, + blacklist_apps: vec![], + blacklist_urls: vec![], + }, } } }