feat: add interactive config on first start (#289)

This commit is contained in:
Martin Hloska 2023-01-26 18:00:06 +01:00 committed by GitHub
parent 8cc126735b
commit 501a730340
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 106 additions and 5 deletions

36
Cargo.lock generated
View File

@ -203,6 +203,19 @@ dependencies = [
"tracing-error",
]
[[package]]
name = "console"
version = "0.15.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3d79fbe8970a77e3e34151cc13d3b3e248aa0faaecb9f6091fa07ebefe5ad60"
dependencies = [
"encode_unicode",
"lazy_static",
"libc",
"unicode-width",
"windows-sys",
]
[[package]]
name = "core-foundation"
version = "0.9.3"
@ -288,6 +301,16 @@ dependencies = [
"syn",
]
[[package]]
name = "dialoguer"
version = "0.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af3c796f3b0b408d9fd581611b47fa850821fcb84aa640b83a3c1a5be2d691f2"
dependencies = [
"console",
"shell-words",
]
[[package]]
name = "dirs-next"
version = "2.0.0"
@ -309,6 +332,12 @@ dependencies = [
"winapi",
]
[[package]]
name = "encode_unicode"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f"
[[package]]
name = "encoding"
version = "0.2.33"
@ -1246,6 +1275,12 @@ dependencies = [
"lazy_static",
]
[[package]]
name = "shell-words"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24188a676b6ae68c3b2cb3a01be17fbf7240ce009799bb56d5b1409051e78fde"
[[package]]
name = "signal-hook"
version = "0.3.14"
@ -1544,6 +1579,7 @@ dependencies = [
"clap",
"color-eyre",
"crossterm",
"dialoguer",
"fern",
"futures",
"fuzzy-matcher",

View File

@ -33,6 +33,7 @@ regex = "1.7.0"
color-eyre = "0.6.2"
log = "0.4.17"
fern = "0.6.1"
dialoguer = { version = "0.10.3", default-features = false }
[[bin]]
bench = false

View File

@ -19,6 +19,8 @@ use crate::{
utils::pathing::config_path,
};
use crate::handlers::interactive::interactive_config;
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
#[serde(default)]
pub struct CompleteConfig {
@ -262,6 +264,14 @@ impl FromStr for Theme {
}
}
fn persist_config(path: &Path, config: &CompleteConfig) -> Result<()> {
let toml_string = toml::to_string(&config)?;
let mut file = File::create(path)?;
file.write_all(toml_string.as_bytes())?;
drop(file);
Ok(())
}
impl CompleteConfig {
pub fn new(cli: Cli) -> Result<Self, Error> {
let path_str = config_path("config.toml");
@ -271,11 +281,13 @@ impl CompleteConfig {
if !p.exists() {
create_dir_all(p.parent().unwrap()).unwrap();
let default_toml_string = toml::to_string(&CompleteConfig::default()).unwrap();
let mut file = File::create(path_str.clone()).unwrap();
file.write_all(default_toml_string.as_bytes()).unwrap();
bail!("Configuration was generated at {path_str}, please fill it out with necessary information.")
if let Some(config) = interactive_config() {
persist_config(p, &config)?;
Ok(config)
} else {
persist_config(p, &CompleteConfig::default())?;
bail!("Configuration was generated at {path_str}, please fill it out with necessary information.")
}
} else if let Ok(config_contents) = read_to_string(p) {
let mut config: CompleteConfig = toml::from_str(config_contents.as_str()).unwrap();

View File

@ -0,0 +1,51 @@
use crate::handlers::config::{CompleteConfig, TwitchConfig};
use dialoguer::console::Style;
use dialoguer::theme::ColorfulTheme;
use dialoguer::{Confirm, Input};
pub(super) fn interactive_config() -> Option<CompleteConfig> {
let theme = ColorfulTheme {
values_style: Style::new().yellow().dim(),
..ColorfulTheme::default()
};
println!("It looks like Twitch TUI is not configured yet.");
if !Confirm::with_theme(&theme)
.with_prompt("Do you want to use interactive wizard?")
.interact()
.ok()?
{
return None;
}
let username: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Username: ")
.interact_text()
.unwrap();
let token: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Token: ")
.interact_text()
.unwrap();
let channel: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Channel: ")
.interact_text()
.unwrap();
let server: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("IRC server: ")
.default("irc.chat.twitch.tv".to_string())
.interact_text()
.unwrap();
Some(CompleteConfig {
twitch: TwitchConfig {
username,
channel,
server,
token: Some(token),
},
..Default::default()
})
}

View File

@ -3,5 +3,6 @@ pub mod args;
pub mod config;
pub mod data;
pub mod filters;
mod interactive;
pub mod storage;
pub mod user_input;