Skip to content

Commit bdf00b1

Browse files
committed
Add current operation to print the last scheme name set by the user
- 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
1 parent 2cd4215 commit bdf00b1

File tree

11 files changed

+128
-3
lines changed

11 files changed

+128
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 0.7.0 (2024-02-16)
4+
5+
- Add `current` subcommand to print the last scheme name set.
6+
37
## 0.6.0 (2024-02-15)
48

59
- Change config.toml properties to use dashes instead of underscores.

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "tinty"
33
description = "Change the theme of your terminal, text editor and anything else with one command!"
4-
version = "0.6.0"
4+
version = "0.7.0"
55
edition = "2021"
66
license = "MIT"
77
readme = "README.md"

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Tinty supports [Base16] and [Base24] scheming systems.
2020
- [set](#set)
2121
- [update](#update)
2222
- [init](#init)
23+
- [current](#current)
2324
- [Flags](#flags)
2425
- [Configuration](#configuration)
2526
- [config.toml](#configtoml)
@@ -86,6 +87,10 @@ value set in your `config.toml` file.
8687
This command is useful when added to your shell `.*rc` file to make sure
8788
your shell and other themes are set correctly.
8889

90+
#### `current`
91+
92+
This prints the last scheme name that was set.
93+
8994
#### Flags
9095

9196
**`--config` or `-c`**

license.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2014,7 +2014,7 @@ <h4>Used by:</h4>
20142014
<h3 id="MIT">MIT License</h3>
20152015
<h4>Used by:</h4>
20162016
<ul class="license-used-by">
2017-
<li><a href=" https://github.com/tinted-theming/tinty ">tinty 0.6.0</a></li>
2017+
<li><a href=" https://github.com/tinted-theming/tinty ">tinty 0.7.0</a></li>
20182018
</ul>
20192019
<pre class="license-text">MIT License
20202020

src/cli.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ pub fn build_cli() -> Command {
1919
.action(ArgAction::Set)
2020
)
2121
// Define subcommands
22+
.subcommand(
23+
Command::new("current").about("Prints the last scheme name set")
24+
)
2225
.subcommand(
2326
Command::new("init").about("Initializes base16 with the exising config. Used to Initialize exising theme for when your shell starts up.")
2427
)

src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ fn main() -> Result<()> {
4848

4949
// Handle the subcommands passed to the CLI
5050
match matches.subcommand() {
51+
Some(("current", _)) => {
52+
operations::current::current(&data_path)?;
53+
}
5154
Some(("init", _)) => {
5255
operations::init::init(&config_path, &data_path)?;
5356
}

src/operations/current.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use crate::constants::CURRENT_SCHEME_FILE_NAME;
2+
use crate::utils::read_file_to_string;
3+
use anyhow::{anyhow, Result};
4+
use std::path::Path;
5+
6+
/// Initialize based on existing data_path files
7+
///
8+
/// This is used to set the theme when your shell is opened. It is based on your previously set
9+
/// theme or your default theme set in config.
10+
pub fn current(data_path: &Path) -> Result<()> {
11+
let current_scheme_name = read_file_to_string(&data_path.join(CURRENT_SCHEME_FILE_NAME)).ok();
12+
13+
match current_scheme_name {
14+
Some(scheme_name) => {
15+
println!("{}", scheme_name);
16+
Ok(())
17+
}
18+
None => Err(anyhow!(
19+
"Failed to read last scheme from file. Try setting a scheme and try again."
20+
)),
21+
}
22+
}

src/operations/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod current;
12
pub mod init;
23
pub mod list;
34
pub mod set;

tests/cli_current_subcommand_tests.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
mod common;
2+
3+
use crate::common::{cleanup, write_to_file, COMMAND_NAME, REPO_NAME};
4+
use anyhow::{anyhow, Result};
5+
use std::fs;
6+
use std::path::{Path, PathBuf};
7+
8+
#[test]
9+
fn test_cli_current_subcommand_with_setup() -> Result<()> {
10+
// -------
11+
// Arrange
12+
// -------
13+
let config_path = Path::new("test_cli_current_subcommand_with_setup");
14+
let scheme_name = "base16-oceanicnext";
15+
let command = format!(
16+
"{} --config=\"{}\" current",
17+
COMMAND_NAME,
18+
config_path.display(),
19+
);
20+
let command_vec = shell_words::split(command.as_str()).map_err(anyhow::Error::new)?;
21+
let system_data_path: PathBuf =
22+
dirs::data_dir().ok_or_else(|| anyhow!("Error getting data directory"))?;
23+
let data_dir = system_data_path.join(format!("tinted-theming/{}", REPO_NAME));
24+
let current_scheme_path = data_dir.join("current_scheme");
25+
cleanup(config_path)?;
26+
if !config_path.exists() {
27+
fs::create_dir(config_path)?;
28+
}
29+
write_to_file(&current_scheme_path, scheme_name)?;
30+
31+
// // ---
32+
// // Act
33+
// // ---
34+
let (stdout, stderr) = common::run_command(command_vec).unwrap();
35+
36+
// // ------
37+
// // Assert
38+
// // ------
39+
assert!(
40+
stdout.contains(scheme_name),
41+
"stdout does not contain the expected output"
42+
);
43+
assert!(
44+
stderr.is_empty(),
45+
"stderr does not contain the expected output"
46+
);
47+
48+
cleanup(config_path)?;
49+
Ok(())
50+
}
51+
52+
#[test]
53+
fn test_cli_current_subcommand_without_setup() -> Result<()> {
54+
// -------
55+
// Arrange
56+
// -------
57+
let config_path = Path::new("test_cli_current_subcommand_without_setup");
58+
let command = format!(
59+
"{} --config=\"{}\" current",
60+
COMMAND_NAME,
61+
config_path.display(),
62+
);
63+
let command_vec = shell_words::split(command.as_str()).map_err(anyhow::Error::new)?;
64+
cleanup(config_path)?;
65+
fs::create_dir(config_path)?;
66+
67+
// // ---
68+
// // Act
69+
// // ---
70+
let (_, stderr) = common::run_command(command_vec).unwrap();
71+
72+
// // ------
73+
// // Assert
74+
// // ------
75+
cleanup(config_path)?;
76+
assert!(
77+
stderr
78+
.contains("Failed to read last scheme from file. Try setting a scheme and try again."),
79+
"stderr does not contain the expected output"
80+
);
81+
82+
Ok(())
83+
}

tests/common/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ pub fn write_to_file(path: &Path, contents: &str) -> Result<()> {
7171
fs::remove_file(path)?;
7272
}
7373

74+
if path.parent().is_some() && !path.parent().unwrap().exists() {
75+
fs::create_dir_all(path.parent().unwrap())?;
76+
}
77+
7478
let mut file = File::create(path)?;
7579

7680
file.write_all(contents.as_bytes())?;

0 commit comments

Comments
 (0)