diff --git a/CHANGELOG.md b/CHANGELOG.md index 722b378..542df10 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Changelog +## Unreleased + +### Changed + +- Change `tinty generate-scheme` API by removing the `OUTFILE` option + and only printing to stdout or saving to the tinty data directory with + the `--save` flag + ## [0.22.0] - 2024-10-09 ### Added diff --git a/README.md b/README.md index c931e3b..189e1fe 100644 --- a/README.md +++ b/README.md @@ -169,7 +169,7 @@ The following is a table of the available subcommands for the CLI tool (Tinty), | `info` | Provides information about themes. | `[-]`: Optional argument `--custom-schemes` to provide information on any custom schemes | `tinty info base16-mocha` | | `build` | Builds the provided base16 or base24 template using [tinted-builder-rust]. | ``: Path to the base16 or base24 template directory. | `tinty build path/to/tinted-tmux` | | `generate-completion` | Generates a shell completion file to source in your shell startup file (`*rc`). | ``: Name of the shell to generate a completion script for. Supports `bash`, `elvish`, `fish`, `powershell`, `zsh` | `tinty generate-completion bash` | -| `generate-scheme` | Generates a yaml scheme file with colors inferred from provided image. | ``: Path to image. Either `` (`-` value to print to stdout) or `--save` to save for use within `tinty` | `tinty generate-completion bash` | +| `generate-scheme` | Generates a yaml scheme file with colors inferred from provided image. | ``: Path to image. Prints to stdout unless `--save` is provided which saves to `~/.local/share/tinted-theming/tinty/custom-schemes` for use within Tinty | `tinty generate-scheme --system=base16 --save /path/to/image.png` | | `install` | Installs requirements for the configuration. (Use `tinty sync`) | - | `tinty install` | | `update` | Updates the templates and schemes. (Use `tinty sync`) | - | `tinty update` | diff --git a/src/cli.rs b/src/cli.rs index 65289d7..20857a3 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,6 +1,6 @@ use clap::{ builder::{styling, PossibleValue}, - Arg, ArgAction, ArgGroup, ArgMatches, Command, ValueHint, + Arg, ArgAction, ArgMatches, Command, ValueHint, }; use clap_complete::Shell; @@ -79,13 +79,7 @@ pub fn build_cli() -> Command { Arg::new("image_path") .help("Which image file to use.") .required(true) - .value_name("INFILE") - .value_hint(ValueHint::FilePath) - ) - .arg( - Arg::new("outfile") - .help("Output path to save the .yaml file to. Use '-' for stdout") - .value_name("OUTFILE") + .value_name("IMAGE_FILE") .value_hint(ValueHint::FilePath) ) .arg( @@ -132,9 +126,6 @@ pub fn build_cli() -> Command { ]) .value_hint(ValueHint::Other) ) - .group(ArgGroup::new("required_flags") - .args(["outfile", "save"]) - .required(true)), ) .subcommand( Command::new("info").about(format!("Shows scheme colors for all schemes matching - (Eg: {} info base16-mocha)", REPO_NAME)) diff --git a/src/main.rs b/src/main.rs index 851ecba..11ef126 100644 --- a/src/main.rs +++ b/src/main.rs @@ -213,10 +213,7 @@ fn main() -> Result<()> { Some(custom_scheme_path.join(format!("{}/{}", system, filename))) } else { - match sub_matches.get_one::("outfile").map(|s| s.as_str()) { - Some("-") | None => None, - Some(value) => Some(PathBuf::from(value)), - } + None } }; diff --git a/src/operations/generate_scheme.rs b/src/operations/generate_scheme.rs index 709bbbf..ce1af40 100644 --- a/src/operations/generate_scheme.rs +++ b/src/operations/generate_scheme.rs @@ -35,7 +35,7 @@ pub(crate) fn generate_scheme( println!("Scheme created: {}", path.display()); } - None => println!("scheme:\n{}", scheme), + None => print!("{scheme}"), // Scheme .display() already ends with a newline }; Ok(()) diff --git a/tests/cli_generatescheme_subcommand_tests.rs b/tests/cli_generatescheme_subcommand_tests.rs new file mode 100644 index 0000000..8ad72c6 --- /dev/null +++ b/tests/cli_generatescheme_subcommand_tests.rs @@ -0,0 +1,196 @@ +mod utils; + +use std::fs; + +use crate::utils::setup; +use anyhow::Result; +use utils::CUSTOM_SCHEMES_DIR_NAME; + +#[test] +fn test_cli_generatescheme_subcommand_custom_properties() -> Result<()> { + // --- + // Act + // --- + let system = "base24"; + let author = "Some Author (https://github.com/tinted-theming)"; + let name = "Some custom name"; + let slug = "some-custom-slug"; + let variant = "light"; + let (_, _, command_vec, cleanup) = setup( + "test_cli_generatescheme_subcommand_custom_properties", + format!( + "generate-scheme --author \"{}\" --name \"{}\" --slug {} --system {} --variant {} ./tests/fixtures/assets/article-featured-image.webp", + author, + name, + slug, + system, + variant, + ) + .as_str(), + )?; + let expected_output = format!( + r#"author: "{author}" +name: "{name}" +slug: "{slug}" +system: "{system}" +variant: "{variant}" +palette: + base00: "f7f7f7" + base01: "d5d9d6" + base02: "b3bbb6" + base03: "919d95" + base04: "707f75" + base05: "4e6154" + base06: "2c4334" + base07: "0b2614" + base08: "dd5319" + base09: "e29c0d" + base0A: "e29c0d" + base0B: "079f31" + base0C: "00fffe" + base0D: "055de1" + base0E: "66495d" + base0F: "97421d" + base10: "ab674a" + base11: "ab8943" + base12: "ab8943" + base13: "2d7842" + base14: "41bdbd" + base15: "3d68a8" + base16: "5e505a" + base17: "774e3c" +"# + ); + + // --- + // Act + // --- + let (stdout, stderr) = utils::run_command(command_vec).unwrap(); + + // ------ + // Assert + // ------ + assert_eq!(stdout, expected_output); + assert!( + stderr.is_empty(), + "stdout does not contain the expected output" + ); + + cleanup()?; + Ok(()) +} + +#[test] +fn test_cli_generatescheme_subcommand_with_image() -> Result<()> { + // --- + // Act + // --- + let (_, _, command_vec, cleanup) = setup( + "test_cli_generatescheme_subcommand_with_image", + "generate-scheme --system base16 ./tests/fixtures/assets/article-featured-image.webp", + )?; + let expected_output = r#"author: "Tinty" +name: "Tinty Generated" +slug: "tinty-generated" +system: "base16" +variant: "dark" +palette: + base00: "0e2d19" + base01: "2f4938" + base02: "506658" + base03: "718378" + base04: "93a097" + base05: "b4bdb7" + base06: "d5dad7" + base07: "f7f7f7" + base08: "dd5319" + base09: "e29c0d" + base0A: "e29c0d" + base0B: "079f31" + base0C: "00fffe" + base0D: "055de1" + base0E: "66495d" + base0F: "97421d" +"#; + + // --- + // Act + // --- + let (stdout, stderr) = utils::run_command(command_vec).unwrap(); + + // ------ + // Assert + // ------ + assert_eq!(stdout, expected_output); + assert!( + stderr.is_empty(), + "stdout does not contain the expected output" + ); + + cleanup()?; + Ok(()) +} + +#[test] +fn test_cli_generatescheme_subcommand_with_save() -> Result<()> { + // --- + // Act + // --- + let scheme_system = "base16"; + let scheme_slug = "test-scheme-slug"; + let (_, data_path, command_vec, cleanup) = setup( + "test_cli_generatescheme_subcommand_with_save", + format!("generate-scheme --slug {scheme_slug} --system {scheme_system} --save ./tests/fixtures/assets/article-featured-image.webp").as_str(), + )?; + let out_scheme_path = data_path.join(format!( + "{CUSTOM_SCHEMES_DIR_NAME}/{scheme_system}/{scheme_slug}.yaml" + )); + let expected_output = format!( + r#"system: {scheme_system} +name: Tinty Generated +slug: {scheme_slug} +author: Tinty +description: null +variant: dark +palette: + base00: 0e2d19 + base01: 2f4938 + base02: '506658' + base03: '718378' + base04: 93a097 + base05: b4bdb7 + base06: d5dad7 + base07: f7f7f7 + base08: dd5319 + base09: e29c0d + base0A: e29c0d + base0B: 079f31 + base0C: 00fffe + base0D: 055de1 + base0E: 66495d + base0F: 97421d +"# + ); + + // --- + // Act + // --- + let (stdout, stderr) = utils::run_command(command_vec).unwrap(); + let actual_output = fs::read_to_string(&out_scheme_path)?; + + // ------ + // Assert + // ------ + assert_eq!(actual_output, expected_output); + assert_eq!( + stdout, + format!("Scheme created: {}\n", out_scheme_path.display()) + ); + assert!( + stderr.is_empty(), + "stdout does not contain the expected output" + ); + + cleanup()?; + Ok(()) +} diff --git a/tests/fixtures/assets/article-featured-image.webp b/tests/fixtures/assets/article-featured-image.webp new file mode 100644 index 0000000..e3f4d9e Binary files /dev/null and b/tests/fixtures/assets/article-featured-image.webp differ diff --git a/tests/utils.rs b/tests/utils.rs index 776f5bd..50e108c 100644 --- a/tests/utils.rs +++ b/tests/utils.rs @@ -17,6 +17,8 @@ pub const CURRENT_SCHEME_FILE_NAME: &str = "current_scheme"; pub const REPO_DIR: &str = "repos"; #[allow(dead_code)] pub const SCHEMES_REPO_NAME: &str = "schemes"; +#[allow(dead_code)] +pub const CUSTOM_SCHEMES_DIR_NAME: &str = "custom-schemes"; pub fn run_command(command_vec: Vec) -> Result<(String, String), Box> { let output = Command::new(&command_vec[0])