Skip to content

Commit

Permalink
Add support for using a single template for multiple scheme systems
Browse files Browse the repository at this point in the history
  • Loading branch information
JamyGolden committed Feb 14, 2024
1 parent 9dc58a3 commit c815d30
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 76 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 0.5.0 (2024-02-14)

- Change `config.toml` `items.system` to `items.supported_systems` which
now accepts an array of strings instead of a string. This allows for
using a single template repo for setting both base16 and base24
themes.

## 0.4.0 (2024-02-11)

- Enforces config.toml `[[items]]` `name` property is a unique value to
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.4.0"
version = "0.5.0"
edition = "2021"
license = "MIT"
readme = "README.md"
Expand Down
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,23 +102,23 @@ required directories for data and configuration exist.

#### `config.toml`

- `shell` - Add a shell command which will be used by tinty to execute
- `shell` \[String\] - Add a shell command which will be used by tinty to execute
commands. This defaults to `sh -c '{}'`. If you want to use bash or zsh
the format is similar `bash -c '{}'` and `zsh -c '{}'`
- `default_scheme` - defaults to `default-dark`
- `items` - A toml array of tables. Each item represents a template
- `name` (Required) - A unique value indicating the name of the item
- `path` (Required) - A url to the git repository or a path to a local
- `default_scheme` \[String\] - defaults to `default-dark`
- `items` \[Array<Item>\]- A toml array of tables. Each item represents a template
- `name` \[String\] (Required) - A unique value indicating the name of the item
- `path` \[String\] (Required) - A url to the git repository or a path to a local
copy of the repository. It can start with `~/` which will map to
your home directory, otherwise it must be an absolute path.
- `themes_dir` (Required) - The template directory name that contains
- `themes_dir` \[String\] (Required) - The template directory name that contains
the theme files
- `hook` - A script that is executed after `tinty set <SCHEME_NAME>`
- `hook` \[String\]- A script that is executed after `tinty set <SCHEME_NAME>`
has been run. `%f` can be used in the hook which is a variable name
for the location of the theme file. `hook = ". %f"` will source the
theme file after the theme has been set
- system - The scheme system of the template. Defaults to `base16`.
Currently supports `base16` and `base24` templates.
- `supported_systems` \[Array&lt;String&gt;\] - The scheme system of the template. Defaults to `\["base16"\]`.
Currently supports `"base16"` and `"base24"` templates.

[Base16] and/or [Base24] templates are added to the `config.toml` file
and Tinty will clone those repositories and the theme file when you run
Expand All @@ -136,21 +136,21 @@ path = "https://github.com/tinted-theming/base16-shell"
name = "shell"
hook = ". %f"
themes_dir = "scripts"
system = "base16"
supported_systems = ["base16"]

[[items]]
path = "~/projects/base16-tmux"
name = "tmux"
hook = "tmux source-file %f"
themes_dir = "colors"
system = "base16"
supported_systems = ["base16"]

[[items]]
path = "/home/user/projects/base16-fzf"
name = "fzf"
hook = ". %f"
themes_dir = "bash"
system = "base16"
supported_systems = ["base16"]
```

Once `tinty set ocean` is run, the following two files will be generated:
Expand Down
75 changes: 33 additions & 42 deletions license.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ <h1>Third Party Licenses</h1>

<h2>Overview of licenses:</h2>
<ul class="licenses-overview">
<li><a href="#Apache-2.0">Apache License 2.0</a> (40)</li>
<li><a href="#Apache-2.0">Apache License 2.0</a> (41)</li>
<li><a href="#MIT">MIT License</a> (3)</li>
<li><a href="#MPL-2.0">Mozilla Public License 2.0</a> (1)</li>
<li><a href="#Unicode-DFS-2016">Unicode License Agreement - Data Files and Software (2016)</a> (1)</li>
Expand Down Expand Up @@ -1993,13 +1993,28 @@ <h4>Used by:</h4>
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
</pre>
</li>
<li class="license">
<h3 id="Apache-2.0">Apache License 2.0</h3>
<h4>Used by:</h4>
<ul class="license-used-by">
<li><a href=" https://github.com/unicode-rs/unicode-normalization ">unicode-normalization 0.1.22</a></li>
</ul>
<pre class="license-text">Licensed under the Apache License, Version 2.0
&lt;LICENSE-APACHE or
http://www.apache.org/licenses/LICENSE-2.0&gt; or the MIT
license &lt;LICENSE-MIT or http://opensource.org/licenses/MIT&gt;,
at your option. All files in the project carrying such
notice may not be copied, modified, or distributed except
according to those terms.
</pre>
</li>
<li class="license">
<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.4.0</a></li>
<li><a href=" https://github.com/tinted-theming/tinty ">tinty 0.5.0</a></li>
</ul>
<pre class="license-text">MIT License

Expand Down Expand Up @@ -2470,50 +2485,26 @@ <h4>Used by:</h4>
</ul>
<pre class="license-text">UNICODE, INC. LICENSE AGREEMENT - DATA FILES AND SOFTWARE

See Terms of Use &lt;https://www.unicode.org/copyright.html&gt;
for definitions of Unicode Inc.’s Data Files and Software.
Unicode Data Files include all data files under the directories http://www.unicode.org/Public/, http://www.unicode.org/reports/, http://www.unicode.org/cldr/data/, http://source.icu-project.org/repos/icu/, and http://www.unicode.org/utility/trac/browser/.

Unicode Data Files do not include PDF online code charts under the directory http://www.unicode.org/Public/.

NOTICE TO USER: Carefully read the following legal agreement.
BY DOWNLOADING, INSTALLING, COPYING OR OTHERWISE USING UNICODE INC.&#x27;S
DATA FILES (&quot;DATA FILES&quot;), AND/OR SOFTWARE (&quot;SOFTWARE&quot;),
YOU UNEQUIVOCALLY ACCEPT, AND AGREE TO BE BOUND BY, ALL OF THE
TERMS AND CONDITIONS OF THIS AGREEMENT.
IF YOU DO NOT AGREE, DO NOT DOWNLOAD, INSTALL, COPY, DISTRIBUTE OR USE
THE DATA FILES OR SOFTWARE.
Software includes any source code published in the Unicode Standard or under the directories http://www.unicode.org/Public/, http://www.unicode.org/reports/, http://www.unicode.org/cldr/data/, http://source.icu-project.org/repos/icu/, and http://www.unicode.org/utility/trac/browser/.

NOTICE TO USER: Carefully read the following legal agreement. BY DOWNLOADING, INSTALLING, COPYING OR OTHERWISE USING UNICODE INC.&#x27;S DATA FILES (&quot;DATA FILES&quot;), AND/OR SOFTWARE (&quot;SOFTWARE&quot;), YOU UNEQUIVOCALLY ACCEPT, AND AGREE TO BE BOUND BY, ALL OF THE TERMS AND CONDITIONS OF THIS AGREEMENT. IF YOU DO NOT AGREE, DO NOT DOWNLOAD, INSTALL, COPY, DISTRIBUTE OR USE THE DATA FILES OR SOFTWARE.

COPYRIGHT AND PERMISSION NOTICE

Copyright © 1991-2022 Unicode, Inc. All rights reserved.
Distributed under the Terms of Use in https://www.unicode.org/copyright.html.

Permission is hereby granted, free of charge, to any person obtaining
a copy of the Unicode data files and any associated documentation
(the &quot;Data Files&quot;) or Unicode software and any associated documentation
(the &quot;Software&quot;) to deal in the Data Files or Software
without restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, and/or sell copies of
the Data Files or Software, and to permit persons to whom the Data Files
or Software are furnished to do so, provided that either
(a) this copyright and permission notice appear with all copies
of the Data Files or Software, or
(b) this copyright and permission notice appear in associated
Documentation.

THE DATA FILES AND SOFTWARE ARE PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT OF THIRD PARTY RIGHTS.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS
NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL
DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THE DATA FILES OR SOFTWARE.

Except as contained in this notice, the name of a copyright holder
shall not be used in advertising or otherwise to promote the sale,
use or other dealings in these Data Files or Software without prior
written authorization of the copyright holder.
Copyright © 1991-2016 Unicode, Inc. All rights reserved. Distributed under the Terms of Use in http://www.unicode.org/copyright.html.

Permission is hereby granted, free of charge, to any person obtaining a copy of the Unicode data files and any associated documentation (the &quot;Data Files&quot;) or Unicode software and any associated documentation (the &quot;Software&quot;) to deal in the Data Files or Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, and/or sell copies of the Data Files or Software, and to permit persons to whom the Data Files or Software are furnished to do so, provided that either

(a) this copyright and permission notice appear with all copies of the Data Files or Software, or
(b) this copyright and permission notice appear in associated Documentation.

THE DATA FILES AND SOFTWARE ARE PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THE DATA FILES OR SOFTWARE.

Except as contained in this notice, the name of a copyright holder shall not be used in advertising or otherwise to promote the sale, use or other dealings in these Data Files or Software without prior written authorization of the copyright holder.
</pre>
</li>
</ul>
Expand Down
30 changes: 23 additions & 7 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub const BASE16_SHELL_REPO_NAME: &str = "base16-shell";
pub const BASE16_SHELL_THEMES_DIR: &str = "scripts";
pub const BASE16_SHELL_HOOK: &str = ". %f";

#[derive(Debug, Default, Clone)]
#[derive(Debug, Default, Clone, PartialEq)]
pub enum SupportedSchemeSystems {
#[default]
Base16,
Expand Down Expand Up @@ -60,6 +60,14 @@ impl SupportedSchemeSystems {
}
}

pub fn from_str(system_string: &str) -> SupportedSchemeSystems {
match system_string {
"base16" => SupportedSchemeSystems::Base16,
"base24" => SupportedSchemeSystems::Base24,
_ => SupportedSchemeSystems::Base16,
}
}

pub fn variants() -> &'static [SupportedSchemeSystems] {
static VARIANTS: &[SupportedSchemeSystems] = &[
SupportedSchemeSystems::Base16,
Expand All @@ -76,13 +84,21 @@ pub struct ConfigItem {
pub path: String,
pub hook: Option<String>,
pub themes_dir: String,
pub system: Option<SupportedSchemeSystems>,
pub supported_systems: Option<Vec<SupportedSchemeSystems>>,
}

impl fmt::Display for ConfigItem {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let hook = self.hook.clone().unwrap_or_default();
let system = self.system.clone().unwrap_or_default();
let mut system_text = String::new();

for system in self
.supported_systems
.as_ref()
.unwrap_or(&vec![SupportedSchemeSystems::default()])
{
system_text += system.to_str();
}

// You can format the output however you like
writeln!(f, " - Item")?;
Expand All @@ -91,7 +107,7 @@ impl fmt::Display for ConfigItem {
if !hook.is_empty() {
writeln!(f, " - hook: {}", hook)?;
}
writeln!(f, " - system: {}", system.to_str())?;
writeln!(f, " - system: {}", system_text)?;
writeln!(f, " - themes_dir: {}", self.themes_dir)
}
}
Expand Down Expand Up @@ -137,7 +153,7 @@ impl Config {
name: BASE16_SHELL_REPO_NAME.to_string(),
themes_dir: BASE16_SHELL_THEMES_DIR.to_string(),
hook: Some(BASE16_SHELL_HOOK.to_string()),
system: Some(SupportedSchemeSystems::Base16), // DEFAULT_SCHEME_SYSTEM
supported_systems: Some(vec![SupportedSchemeSystems::Base16]), // DEFAULT_SCHEME_SYSTEM
};

// Add default `item` if no items exist
Expand All @@ -153,8 +169,8 @@ impl Config {
// Set default `system` property for missing systems
if let Some(ref mut items) = config.items {
for item in items.iter_mut() {
if item.system.is_none() {
item.system = Some(SupportedSchemeSystems::default());
if item.supported_systems.is_none() {
item.supported_systems = Some(vec![SupportedSchemeSystems::default()]);
}

// Replace `~/` with absolute home path
Expand Down
14 changes: 8 additions & 6 deletions src/operations/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ pub fn set(config_path: &Path, data_path: &Path, full_scheme_name: &str) -> Resu
write_to_file(&data_path.join(CURRENT_SCHEME_FILE_NAME), full_scheme_name)?;

// Collect config items that match the provided system
let system_items = items.iter().filter(|item| match &item.system {
Some(system) => system.to_str() == scheme_system,
let system_items = items.iter().filter(|item| match &item.supported_systems {
Some(supported_systems) => supported_systems.contains(&SupportedSchemeSystems::from_str(scheme_system)),
None => false,
});

Expand All @@ -71,8 +71,10 @@ pub fn set(config_path: &Path, data_path: &Path, full_scheme_name: &str) -> Resu

if !themes_path.exists() {
return Err(anyhow!(format!(
"Themes files are missing, try running `{} setup` or `{} update` and try again.",
REPO_NAME, REPO_NAME
"Provided theme path for {} does not exist: {}\nTry running `{} setup` or `{} update` or check your config.toml file and try again.",
item.name,
themes_path.display(),
REPO_NAME, REPO_NAME,
)));
}

Expand Down Expand Up @@ -121,8 +123,8 @@ pub fn set(config_path: &Path, data_path: &Path, full_scheme_name: &str) -> Resu
}
}
None => println!(
"Theme does not exists for {}. Try running `{} update` or submit an issue on {}",
item.name, REPO_NAME, REPO_URL
"Theme does not exists for {} in {}. Try running `{} update` or submit an issue on {}",
item.name, themes_path.display(), REPO_NAME, REPO_URL
),
}
}
Expand Down
6 changes: 1 addition & 5 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,7 @@ pub fn git_diff(target_dir: &Path) -> Result<bool> {

pub fn create_theme_filename_without_extension(item: &ConfigItem) -> Result<String> {
Ok(format!(
"{}-{}-{}-file",
item.system
.as_ref()
.unwrap_or(&SupportedSchemeSystems::default())
.to_str(),
"{}-{}-file",
item.name.clone(),
item.themes_dir.clone().replace('/', "-"), // Flatten path/to/dir to path-to-dir
))
Expand Down
12 changes: 10 additions & 2 deletions tests/cli_set_subcommand_tests.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
mod common;

use crate::common::{cleanup, COMMAND_NAME, REPO_NAME};
use anyhow::Result;
use anyhow::{Result, anyhow};
use std::fs;
use std::path::Path;
use std::path::{Path, PathBuf};

#[test]
fn test_cli_set_subcommand_with_setup() -> Result<()> {
Expand All @@ -19,6 +19,10 @@ fn test_cli_set_subcommand_with_setup() -> Result<()> {
&scheme_name,
);
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 shell_theme_filename = "base16-shell-scripts-file.sh";
cleanup(config_path)?;
fs::create_dir(config_path)?;

Expand All @@ -35,6 +39,10 @@ fn test_cli_set_subcommand_with_setup() -> Result<()> {
stdout.is_empty(),
"stdout does not contain the expected output"
);
assert!(
data_dir.join(shell_theme_filename).exists(),
"Path does not exist"
);

cleanup(config_path)?;
Ok(())
Expand Down

0 comments on commit c815d30

Please sign in to comment.