Skip to content

Commit

Permalink
Remove outfile option for generate-scheme subcommand for simplicity
Browse files Browse the repository at this point in the history
  • Loading branch information
JamyGolden committed Oct 13, 2024
1 parent d43450c commit 2f7912c
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 17 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ The following is a table of the available subcommands for the CLI tool (Tinty),
| `info` | Provides information about themes. | `[<scheme_system>-<scheme_name>]`: 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]. | `<DIR>`: 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`). | `<shell_name>`: 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. | `<image_path>`: Path to image. Either `<outpath>` (`-` 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. | `<image_path>`: 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` |

Expand Down
13 changes: 2 additions & 11 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::{
builder::{styling, PossibleValue},
Arg, ArgAction, ArgGroup, ArgMatches, Command, ValueHint,
Arg, ArgAction, ArgMatches, Command, ValueHint,
};
use clap_complete::Shell;

Expand Down Expand Up @@ -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 <slug>.yaml file to. Use '-' for stdout")
.value_name("OUTFILE")
.value_name("IMAGE_FILE")
.value_hint(ValueHint::FilePath)
)
.arg(
Expand Down Expand Up @@ -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 <scheme_system>-<scheme_name> (Eg: {} info base16-mocha)", REPO_NAME))
Expand Down
5 changes: 1 addition & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,7 @@ fn main() -> Result<()> {

Some(custom_scheme_path.join(format!("{}/{}", system, filename)))
} else {
match sub_matches.get_one::<String>("outfile").map(|s| s.as_str()) {
Some("-") | None => None,
Some(value) => Some(PathBuf::from(value)),
}
None
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/operations/generate_scheme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
196 changes: 196 additions & 0 deletions tests/cli_generatescheme_subcommand_tests.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
Binary file added tests/fixtures/assets/article-featured-image.webp
Binary file not shown.
2 changes: 2 additions & 0 deletions tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>) -> Result<(String, String), Box<dyn Error>> {
let output = Command::new(&command_vec[0])
Expand Down

0 comments on commit 2f7912c

Please sign in to comment.