Skip to content

Commit

Permalink
Add tinty config subcommand (with flags) to return config related i…
Browse files Browse the repository at this point in the history
…nformation (#24)
  • Loading branch information
JamyGolden authored May 7, 2024
1 parent 4b4b17b commit 93bd9a9
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 18 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## [Unreleased]

### Added

- Add `tinty config` subcommand with flags to return config related
information

## [0.12.0] - 2024-04-29

### Added
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ test_docker: setup_tests
@echo "Running test_docker"
@echo "-------------------"
docker build --no-cache --target tests -t tinty-clippy .
cargo fmt --all --check

test: setup_tests
@echo "------------"
@echo "Running test"
@echo "------------"
RUST_TEST_THREADS=1 cargo test --release
RUST_TEST_THREADS=1 cargo test --release $(TINTY_RUST_TEST)

setup_tests: build
@echo "-----------------"
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ Tinty have a look at our [Base16 Gallery].
- [Basic usage](#basic-usage)
- [Usage](#usage)
- [Configuration](#configuration)
- [Debugging Tinty issues](#debugging-tinty-issues)
- [Contributing](#contributing)
- [License](#license)

## How it Works

Expand Down Expand Up @@ -161,6 +164,7 @@ The following is a table of the available subcommands for the CLI tool (Tinty),
| `update` | Updates the templates and schemes. | - | `tinty update` |
| `init` | Initializes the tool with the last applied theme otherwise `default-scheme` from `config.toml`. | - | `tinty init` |
| `current` | Displays the currently applied theme. | - | `tinty current` |
| `config` | Displays config related information currently in use by Tinty. Without flags it returns `config.yml` content. | - | `tinty config` |
| `info` | Provides information about themes. | `[<scheme_system>-<scheme_name>]`: Optional argument to specify a theme for detailed info. | `tinty info base16-mocha` |
| `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` |

Expand All @@ -172,6 +176,8 @@ Some subcommands support additional flags and options to modify their behavior:
| `--data-dir` | Specifies a custom path for the data directory. | All | If not provided tinty looks for the data directory at `$XDG_DATA_HOME/tinted-theming/tinty` otherwise `~/.local/share/tinted-theming/tinty` | `tinty install --data-dir /path/to/custom/data-dir` |
| `--help` `-h` | Displays help information for the subcommand. | All | - | `tinty --help`, `tinty apply --help`, etc. |
| `--version` `-V` | Shows the version of tinty. | All | - | `tinty --version` |
| `--config-path` | Shows the config.yml path. | `config` | - | `tinty config --config-path` |
| `--data-dir-path` | Shows the data directory path. | `config` | - | `tinty config --data-dir-path` |

## Configuration

Expand Down Expand Up @@ -346,6 +352,13 @@ hook = "cp -f %f ~/.config/sway/config && swaymsg reload"
So the hook does a copy of the selected theme and replaces
`~/.config/alacritty/colors.toml`.

## Debugging Tinty issues

`tinty config` with the relevant flags, `--config-path` and
`--data-dir-path`, can be useful when attempting to debug an issue with
Tinty. These commands can help to make sure that the expected config
file is applied and the expected data directory are being used by Tinty.

## Contributing

Contributions are welcome. Have a look at [CONTRIBUTING.md] for more
Expand Down
19 changes: 19 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,25 @@ pub fn build_cli() -> Command {
Command::new("init").about("Initializes with the exising config. Used to Initialize exising theme for when your shell starts up")
)
.subcommand(Command::new("list").about("Lists available schemes"))
.subcommand(
Command::new("config").about("Provides config related information")
.arg(
Arg::new("config-path")
.help(format!("Returns path to the {} config file", REPO_NAME))
.value_name("FILE")
.long("config-path")
.conflicts_with("data-dir-path")
.action(ArgAction::SetTrue)
)
.arg(
Arg::new("data-dir-path")
.help(format!("Returns path to the {} data directory", REPO_NAME))
.value_name("DIRECTORY")
.long("data-dir-path")
.conflicts_with("config-path")
.action(ArgAction::SetTrue)
)
)
.subcommand(
Command::new("apply").about("Applies a theme based on the chosen scheme").arg(
Arg::new("scheme_name")
Expand Down
34 changes: 17 additions & 17 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,20 @@ impl fmt::Display for ConfigItem {
.clone()
.unwrap_or(default_supported_systems)
.into_iter()
.map(|system| system.to_str())
.collect::<Vec<&str>>()
.map(|system| format!("\"{}\"", system))
.collect::<Vec<String>>()
.join(", ");

// You can format the output however you like
writeln!(f, " - Item")?;
writeln!(f, " - name: {}", self.name)?;
writeln!(f, " - path: {}", self.path)?;
writeln!(f)?;
writeln!(f, "[[items]]")?;
writeln!(f, "name = \"{}\"", self.name)?;
writeln!(f, "path = \"{}\"", self.path)?;
if !hook.is_empty() {
writeln!(f, " - hook: {}", hook)?;
writeln!(f, "hook = \"{}\"", hook)?;
}
writeln!(f, " - systems: [{}]", system_text)?;
writeln!(f, " - themes-dir: {}", self.themes_dir)
writeln!(f, "supported-systems = [{}]", system_text)?;
write!(f, "themes-dir = \"{}\"", self.themes_dir)
}
}

Expand Down Expand Up @@ -228,22 +229,21 @@ impl Config {

impl fmt::Display for Config {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "Config")?;
writeln!(
f,
"- shell: {}",
"shell = \"{}\"",
self.shell.as_ref().unwrap_or(&"None".to_string())
)?;
writeln!(
f,
"- default-scheme: {}",
self.default_scheme.as_ref().unwrap_or(&"".to_string())
)?;
if self.default_scheme.is_some() {
writeln!(
f,
"default-scheme = \"{}\"",
self.default_scheme.as_ref().unwrap_or(&"".to_string())
)?;
}

match &self.items {
Some(items) => {
writeln!(f, "- Items")?;

for item in items {
writeln!(f, "{}", item)?;
}
Expand Down
11 changes: 11 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ fn main() -> Result<()> {
Some(("current", _)) => {
operations::current::current(&data_path)?;
}
Some(("config", sub_matches)) => {
let data_dir_path_flag = sub_matches.get_flag("data-dir-path");
let config_path_flag = sub_matches.get_flag("config-path");

operations::config::config(
&config_path,
&data_path,
config_path_flag,
data_dir_path_flag,
)?;
}
Some(("generate-completion", sub_matches)) => {
if let Some(generator) = sub_matches.get_one::<Shell>("shell_name") {
let mut cmd = build_cli();
Expand Down
34 changes: 34 additions & 0 deletions src/operations/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use anyhow::{anyhow, Result};
use std::path::Path;

use crate::config::Config;

pub fn config(
config_path: &Path,
data_path: &Path,
config_path_flag: bool,
data_dir_path_flag: bool,
) -> Result<()> {
let config = Config::read(config_path)?;
let path_tuple: (bool, bool) = (config_path_flag, data_dir_path_flag);

match path_tuple {
(true, false) => {
println!("{}", config_path.display());
}
(false, true) => {
println!("{}", data_path.display());
}
(false, false) => {
println!("{config}");
}
(true, true) => {
// This case should be dealt with by clap
return Err(anyhow!(
"the argument '--data-dir-path' cannot be used with '--config-path'",
));
}
}

Ok(())
}
1 change: 1 addition & 0 deletions src/operations/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod apply;
pub mod config;
pub mod current;
pub mod info;
pub mod init;
Expand Down
118 changes: 118 additions & 0 deletions tests/cli_config_subcommand_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
mod common;

use crate::common::{setup, write_to_file};
use anyhow::Result;

#[test]
fn test_cli_config_without_config() -> Result<()> {
// -------
// Arrange
// -------
let expected = r#"shell = "sh -c '{}'"
[[items]]
name = "base16-shell"
path = "https://github.com/tinted-theming/base16-shell"
hook = ". %f"
supported-systems = ["base16"]
themes-dir = "scripts"
"#;
let (_, _, command_vec, cleanup) = setup("test_cli_config_without_config", "config")?;

// ---
// Act
// ---
let (stdout, _) = common::run_command(command_vec).unwrap();

// ------
// Assert
// ------
assert_eq!(stdout, expected);

cleanup()?;
Ok(())
}

#[test]
fn test_cli_config_with_config() -> Result<()> {
// -------
// Arrange
// -------
let config_text = r#"shell = "zsh -c '{}'"
default-scheme = "base16-oceanicnext"
[[items]]
name = "tinted-vim"
path = "https://github.com/tinted-theming/tinted-vim"
supported-systems = ["base16", "base24"]
themes-dir = "colors"
"#;
let (config_path, _, command_vec, cleanup) = setup("test_cli_config_with_config", "config")?;

write_to_file(&config_path, config_text)?;

// ---
// Act
// ---
let (stdout, _) = common::run_command(command_vec).unwrap();

// ------
// Assert
// ------
assert_eq!(stdout, config_text);

cleanup()?;
Ok(())
}

#[test]
fn test_cli_config_with_config_flag() -> Result<()> {
// -------
// Arrange
// -------
let (config_path, _, command_vec, cleanup) =
setup("test_cli_config_with_config_flag", "config --config-path")?;

// ---
// Act
// ---
let (stdout, _) = common::run_command(command_vec).unwrap();

// ------
// Assert
// ------
assert!(
stdout.contains(format!("{}", config_path.display()).as_str()),
"stdout does not contain the expected output"
);

cleanup()?;
Ok(())
}

#[test]
fn test_cli_config_with_data_flag() -> Result<()> {
// -------
// Arrange
// -------
let (_, data_path, command_vec, cleanup) =
setup("test_cli_config_with_data_flag", "config --data-dir-path")?;

// ---
// Act
// ---
let (stdout, _) = common::run_command(command_vec).unwrap();

// ------
// Assert
// ------
assert!(
stdout.contains(format!("{}", data_path.display()).as_str()),
"stdout does not contain the expected output"
);

cleanup()?;
Ok(())
}

0 comments on commit 93bd9a9

Please sign in to comment.