Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use base16_color_scheme for building the base16-themes #76

Merged
merged 1 commit into from
Apr 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 128 additions & 14 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ categories = ["command-line-utilities"]
repository = "https://github.com/Misterio77/flavours"

[dependencies]
base16_color_scheme = "0.3.0"
anyhow = "1.0"
calm_io = "0.1"
clap = { version = "=3.0.0-beta.4", features = ["wrap_help", "suggestions", "color"] }
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ pub mod completions;
pub mod config;
pub mod find;
pub mod operations;
pub mod scheme;
25 changes: 19 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
use anyhow::{anyhow, Context, Result};
use base16_color_scheme::{
scheme::{BaseIndex, RgbColor},
Scheme,
};
use dirs::{data_dir, preference_dir};
use std::collections::BTreeMap;
use std::convert::TryInto;
use std::env;
use std::path::Path;

use flavours::operations::{apply, build, current, generate, info, list, update};
use flavours::scheme::Scheme;
use flavours::{cli, completions};

use std::fs::{create_dir_all, write};
Expand Down Expand Up @@ -163,14 +168,22 @@ fn main() -> Result<()> {

let colors = generate::generate(&image, mode, verbose)?;
let scheme = Scheme {
scheme: name,
slug,
name,
author,
colors,
colors: colors
.into_iter()
.enumerate()
.map(|(index, color)| {
let mut rgb_color = [0u8; 3];
hex::decode_to_slice(color, &mut rgb_color)?;
Ok((BaseIndex(index.try_into()?), RgbColor(rgb_color)))
})
.collect::<Result<BTreeMap<_, _>>>()?,
};

if to_stdout {
print!("{}", scheme.to_string()?);
print!("{}", serde_yaml::to_string(&scheme)?);
} else {
let path = flavours_dir
.join("base16")
Expand All @@ -180,8 +193,8 @@ fn main() -> Result<()> {
create_dir_all(&path)
.with_context(|| format!("Couldn't create directory {:?}", &path))?;
}
let file_path = &path.join(format!("{}.yaml", scheme.slug));
write(file_path, scheme.to_string()?)
let file_path = &path.join(format!("{}.yaml", &scheme.slug));
write(file_path, serde_yaml::to_string(&scheme)?)
.with_context(|| format!("Couldn't write scheme file at {:?}", path))?;
}
Ok(())
Expand Down
11 changes: 6 additions & 5 deletions src/operations/apply.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::{anyhow, Context, Result};
use base16_color_scheme::Scheme;
use rand::seq::SliceRandom;
use std::fs;
use std::io::{self, Read};
Expand All @@ -10,7 +11,6 @@ use std::thread;
use crate::config::Config;
use crate::find::{find_schemes, find_template};
use crate::operations::build::build_template;
use crate::scheme::Scheme;

/// Picks a random path, from given vec
/// * `values` - Vec with paths
Expand Down Expand Up @@ -155,12 +155,13 @@ pub fn apply(
)
};

let scheme = Scheme::from_str(&scheme_contents, &scheme_slug)?;
let mut scheme: Scheme = serde_yaml::from_str(&scheme_contents)?;
scheme.slug = scheme_slug;

if verbose {
println!(
"Using scheme: {} ({}), by {}",
scheme.name, scheme.slug, scheme.author
scheme.scheme, scheme.slug, scheme.author
);
println!();
}
Expand Down Expand Up @@ -257,7 +258,7 @@ pub fn apply(
.with_context(||format!("Couldn't read template {}/{} at {:?}. Check if the correct template/subtemplate was specified, and run the update templates command if you didn't already.", template, subtemplate, subtemplate_file))?;

//Template with correct colors
let built_template = build_template(template_content, &scheme)
let built_template = build_template(&template_content, &scheme)
.context("Couldn't replace placeholders. Check if all colors on the specified scheme file are valid (don't include a leading '#').")?;

//File to write
Expand Down Expand Up @@ -298,7 +299,7 @@ pub fn apply(
}

let last_scheme_file = &base_dir.join("lastscheme");
fs::write(&last_scheme_file, &scheme.slug)
fs::write(&last_scheme_file, &scheme.scheme_slug())
.with_context(|| "Couldn't update applied scheme name")?;

while !hooks.is_empty() {
Expand Down
Loading