diff --git a/CHANGELOG.md b/CHANGELOG.md index 9195166..fc01fe0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 0.7.0 (2024-02-16) + +- Add `current` subcommand to print the last scheme name set. + ## 0.6.0 (2024-02-15) - Change config.toml properties to use dashes instead of underscores. diff --git a/Cargo.lock b/Cargo.lock index bc8437b..07a33f8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -355,7 +355,7 @@ dependencies = [ [[package]] name = "tinty" -version = "0.6.0" +version = "0.7.0" dependencies = [ "anyhow", "clap", diff --git a/Cargo.toml b/Cargo.toml index 36c934c..67f3886 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "tinty" description = "Change the theme of your terminal, text editor and anything else with one command!" -version = "0.6.0" +version = "0.7.0" edition = "2021" license = "MIT" readme = "README.md" diff --git a/README.md b/README.md index caeae19..aa3b321 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ Tinty supports [Base16] and [Base24] scheming systems. - [set](#set) - [update](#update) - [init](#init) + - [current](#current) - [Flags](#flags) - [Configuration](#configuration) - [config.toml](#configtoml) @@ -86,6 +87,10 @@ value set in your `config.toml` file. This command is useful when added to your shell `.*rc` file to make sure your shell and other themes are set correctly. +#### `current` + +This prints the last scheme name that was set. + #### Flags **`--config` or `-c`** diff --git a/license.html b/license.html index 0f455d4..65fd554 100644 --- a/license.html +++ b/license.html @@ -2014,7 +2014,7 @@
MIT License diff --git a/src/cli.rs b/src/cli.rs index 8028539..4d7122d 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -19,6 +19,9 @@ pub fn build_cli() -> Command { .action(ArgAction::Set) ) // Define subcommands + .subcommand( + Command::new("current").about("Prints the last scheme name set") + ) .subcommand( Command::new("init").about("Initializes base16 with the exising config. Used to Initialize exising theme for when your shell starts up.") ) diff --git a/src/main.rs b/src/main.rs index 35e0718..3fcffec 100644 --- a/src/main.rs +++ b/src/main.rs @@ -48,6 +48,9 @@ fn main() -> Result<()> { // Handle the subcommands passed to the CLI match matches.subcommand() { + Some(("current", _)) => { + operations::current::current(&data_path)?; + } Some(("init", _)) => { operations::init::init(&config_path, &data_path)?; } diff --git a/src/operations/current.rs b/src/operations/current.rs new file mode 100644 index 0000000..0d66cdf --- /dev/null +++ b/src/operations/current.rs @@ -0,0 +1,22 @@ +use crate::constants::CURRENT_SCHEME_FILE_NAME; +use crate::utils::read_file_to_string; +use anyhow::{anyhow, Result}; +use std::path::Path; + +/// Initialize based on existing data_path files +/// +/// This is used to set the theme when your shell is opened. It is based on your previously set +/// theme or your default theme set in config. +pub fn current(data_path: &Path) -> Result<()> { + let current_scheme_name = read_file_to_string(&data_path.join(CURRENT_SCHEME_FILE_NAME)).ok(); + + match current_scheme_name { + Some(scheme_name) => { + println!("{}", scheme_name); + Ok(()) + } + None => Err(anyhow!( + "Failed to read last scheme from file. Try setting a scheme and try again." + )), + } +} diff --git a/src/operations/mod.rs b/src/operations/mod.rs index d9d1f72..5a458ea 100644 --- a/src/operations/mod.rs +++ b/src/operations/mod.rs @@ -1,3 +1,4 @@ +pub mod current; pub mod init; pub mod list; pub mod set; diff --git a/tests/cli_current_subcommand_tests.rs b/tests/cli_current_subcommand_tests.rs new file mode 100644 index 0000000..0b475bb --- /dev/null +++ b/tests/cli_current_subcommand_tests.rs @@ -0,0 +1,83 @@ +mod common; + +use crate::common::{cleanup, write_to_file, COMMAND_NAME, REPO_NAME}; +use anyhow::{anyhow, Result}; +use std::fs; +use std::path::{Path, PathBuf}; + +#[test] +fn test_cli_current_subcommand_with_setup() -> Result<()> { + // ------- + // Arrange + // ------- + let config_path = Path::new("test_cli_current_subcommand_with_setup"); + let scheme_name = "base16-oceanicnext"; + let command = format!( + "{} --config=\"{}\" current", + COMMAND_NAME, + config_path.display(), + ); + let command_vec = shell_words::split(command.as_str()).map_err(anyhow::Error::new)?; + let system_data_path: PathBuf = + dirs::data_dir().ok_or_else(|| anyhow!("Error getting data directory"))?; + let data_dir = system_data_path.join(format!("tinted-theming/{}", REPO_NAME)); + let current_scheme_path = data_dir.join("current_scheme"); + cleanup(config_path)?; + if !config_path.exists() { + fs::create_dir(config_path)?; + } + write_to_file(¤t_scheme_path, scheme_name)?; + + // // --- + // // Act + // // --- + let (stdout, stderr) = common::run_command(command_vec).unwrap(); + + // // ------ + // // Assert + // // ------ + assert!( + stdout.contains(scheme_name), + "stdout does not contain the expected output" + ); + assert!( + stderr.is_empty(), + "stderr does not contain the expected output" + ); + + cleanup(config_path)?; + Ok(()) +} + +#[test] +fn test_cli_current_subcommand_without_setup() -> Result<()> { + // ------- + // Arrange + // ------- + let config_path = Path::new("test_cli_current_subcommand_without_setup"); + let command = format!( + "{} --config=\"{}\" current", + COMMAND_NAME, + config_path.display(), + ); + let command_vec = shell_words::split(command.as_str()).map_err(anyhow::Error::new)?; + cleanup(config_path)?; + fs::create_dir(config_path)?; + + // // --- + // // Act + // // --- + let (_, stderr) = common::run_command(command_vec).unwrap(); + + // // ------ + // // Assert + // // ------ + cleanup(config_path)?; + assert!( + stderr + .contains("Failed to read last scheme from file. Try setting a scheme and try again."), + "stderr does not contain the expected output" + ); + + Ok(()) +} diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 576d44f..b491334 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -71,6 +71,10 @@ pub fn write_to_file(path: &Path, contents: &str) -> Result<()> { fs::remove_file(path)?; } + if path.parent().is_some() && !path.parent().unwrap().exists() { + fs::create_dir_all(path.parent().unwrap())?; + } + let mut file = File::create(path)?; file.write_all(contents.as_bytes())?;