Skip to content

Commit

Permalink
Add current operation to print the last scheme name set by the user
Browse files Browse the repository at this point in the history
- Version bump to 0.7.0
- Useful command to easily check the last set scheme instead of
  manually looking at the value of `current_scheme` file
- Attempt to reach feature parity with Flavours
  • Loading branch information
JamyGolden committed Feb 16, 2024
1 parent 2cd4215 commit bdf00b1
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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`**
Expand Down
2 changes: 1 addition & 1 deletion license.html
Original file line number Diff line number Diff line change
Expand Up @@ -2014,7 +2014,7 @@ <h4>Used by:</h4>
<h3 id="MIT">MIT License</h3>
<h4>Used by:</h4>
<ul class="license-used-by">
<li><a href=" https://github.com/tinted-theming/tinty ">tinty 0.6.0</a></li>
<li><a href=" https://github.com/tinted-theming/tinty ">tinty 0.7.0</a></li>
</ul>
<pre class="license-text">MIT License

Expand Down
3 changes: 3 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
)
Expand Down
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
}
Expand Down
22 changes: 22 additions & 0 deletions src/operations/current.rs
Original file line number Diff line number Diff line change
@@ -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."
)),
}
}
1 change: 1 addition & 0 deletions src/operations/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod current;
pub mod init;
pub mod list;
pub mod set;
Expand Down
83 changes: 83 additions & 0 deletions tests/cli_current_subcommand_tests.rs
Original file line number Diff line number Diff line change
@@ -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(&current_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(())
}
4 changes: 4 additions & 0 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())?;
Expand Down

0 comments on commit bdf00b1

Please sign in to comment.