From 2e81bbc30453aa011e1bbac2091f0d0272911b4b Mon Sep 17 00:00:00 2001 From: Jonathan CHARDON Date: Fri, 5 Sep 2025 14:41:25 +0200 Subject: [PATCH] fix: Remove interactive prompt on startup Previously, the application would show an interactive prompt to select a dialect (cat or dog) on the first run, even when running non-interactive commands like --help. This was caused by the ConfigManager initializing the configuration interactively when the config file was not found. This commit fixes the issue by making the configuration initialization non-interactive. A default configuration file is now created automatically if it doesn't exist. The user can change the dialect using the 'set-dialect' command. --- src/config.rs | 57 +++++++++++---------------------------------------- src/keys.rs | 2 +- 2 files changed, 13 insertions(+), 46 deletions(-) diff --git a/src/config.rs b/src/config.rs index 5f4e043..c513bba 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,21 +1,20 @@ // src/config.rs use serde::{Deserialize, Serialize}; -use std::io::{self, Write}; -use std::path::PathBuf; -use std::{fs, path::Path}; +use std::path::{Path, PathBuf}; +use std::{fs}; use thiserror::Error; #[derive(Error, Debug)] pub enum ConfigError { #[error("IO error: {0}")] - Io(#[from] io::Error), + Io(#[from] std::io::Error), #[error("TOML error: {0}")] Toml(#[from] toml::ser::Error), #[error("TOML de error: {0}")] TomlDe(#[from] toml::de::Error), } -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, Clone, Copy)] pub enum PreferredDialect { #[serde(rename = "cat")] Cat, @@ -51,39 +50,6 @@ impl Config { fs::write(config_path, contents)?; Ok(()) } - - pub fn initialize(config_dir: &Path) -> Result { - let config_path = config_dir.join("config.toml"); - - if config_path.exists() { - return Self::load(&config_path); - } - - // Create config directory if it doesn't exist - fs::create_dir_all(config_dir)?; - - print!("🐱 Welcome to purrcrypt! Do you prefer cat or dog mode? [cat/dog]: "); - io::stdout().flush()?; - - let mut input = String::new(); - io::stdin().read_line(&mut input)?; - - let config = Config { - dialect: match input.trim().to_lowercase().as_str() { - "dog" => PreferredDialect::Dog, - _ => PreferredDialect::Cat, - }, - }; - - config.save(&config_path)?; - - match config.dialect { - PreferredDialect::Cat => println!("😺 Meow! Cat mode activated!"), - PreferredDialect::Dog => println!("🐕 Woof! Dog mode activated!"), - } - - Ok(config) - } } pub struct ConfigManager { @@ -94,11 +60,12 @@ pub struct ConfigManager { impl ConfigManager { pub fn new(config_dir: &Path) -> Result { let config_path = config_dir.join("config.toml"); - let config = if config_path.exists() { - Config::load(&config_path)? - } else { - Config::initialize(config_dir)? - }; + let config = Config::load(&config_path)?; + + if !config_path.exists() { + fs::create_dir_all(config_dir)?; + config.save(&config_path)?; + } Ok(Self { config, @@ -106,8 +73,8 @@ impl ConfigManager { }) } - pub fn get_dialect(&self) -> &PreferredDialect { - &self.config.dialect + pub fn get_dialect(&self) -> PreferredDialect { + self.config.dialect } pub fn set_dialect(&mut self, dialect: PreferredDialect) -> Result<(), ConfigError> { diff --git a/src/keys.rs b/src/keys.rs index 4abbbb9..7e97eb0 100644 --- a/src/keys.rs +++ b/src/keys.rs @@ -15,7 +15,7 @@ use std::fs; use thiserror::Error; #[cfg(unix)] -use fs::os::unix::fs::PermissionsExt; +use std::os::unix::fs::PermissionsExt; #[derive(Error, Debug)] pub enum KeyError {