diff --git a/Cargo.toml b/Cargo.toml index 01d2f9f..bd6f74a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "silicon" -version = "0.4.3" +version = "0.5.0" description = "Create beautiful image of your code" authors = ["Aloxaf "] categories = ["command-line-utilities"] diff --git a/README.md b/README.md index 785b71f..6472020 100644 --- a/README.md +++ b/README.md @@ -125,9 +125,10 @@ see `silicon --help` for detail ## Adding new syntaxes / themes -Silicon reads syntax-definition and theme cache from bat's cache directory. +Silicon reads syntax-definition and theme cache from user's cache directory. -You can find the steps to add new syntaxes / themes for bat here: [sharkdp/bat#adding-new-syntaxes--language-definitions](https://github.com/sharkdp/bat#adding-new-syntaxes--language-definitions). +The steps to add new syntaxes / themes is as same as bat: [sharkdp/bat#adding-new-syntaxes--language-definitions](https://github.com/sharkdp/bat#adding-new-syntaxes--language-definitions). +Just replace `bat cache --build` to `silicon --build-cache`. ## Configuration file diff --git a/src/assets.rs b/src/assets.rs index c6e21be..4afd5f4 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -39,9 +39,9 @@ impl HighlightingAssets { Ok(()) } - pub fn dump_to_file(&self) -> Result<()> { - dumps::dump_to_file(&self.syntax_set, "./syntaxes.bin")?; - dumps::dump_to_file(&self.theme_set, "./themes.bin")?; + pub fn dump_to_file>(&self, path: P) -> Result<()> { + dumps::dump_to_file(&self.syntax_set, path.as_ref().join("syntaxes.bin"))?; + dumps::dump_to_file(&self.theme_set, path.as_ref().join("themes.bin"))?; Ok(()) } } diff --git a/src/bin/silicon/config.rs b/src/bin/silicon/config.rs index 93d3532..9fc9849 100644 --- a/src/bin/silicon/config.rs +++ b/src/bin/silicon/config.rs @@ -202,8 +202,8 @@ pub struct Config { // #[structopt(long)] // watermark: Option, /// build syntax definition and theme cache - #[structopt(long)] - pub build_cache: Option, + #[structopt(long, value_name = "OUTPUT_DIR")] + pub build_cache: Option>, } impl Config { diff --git a/src/bin/silicon/main.rs b/src/bin/silicon/main.rs index e87827c..4d77d57 100644 --- a/src/bin/silicon/main.rs +++ b/src/bin/silicon/main.rs @@ -3,6 +3,7 @@ extern crate anyhow; use anyhow::Error; use image::DynamicImage; +use std::env; use structopt::StructOpt; use syntect::easy::HighlightLines; use syntect::util::LinesWithEndings; @@ -17,9 +18,9 @@ use {image::ImageOutputFormat, pasteboard::Pasteboard}; use {image::ImageOutputFormat, std::process::Command}; mod config; -use crate::config::{config_file, get_args_from_config_file}; -use config::Config; +use crate::config::{config_file, get_args_from_config_file, Config}; use silicon::assets::HighlightingAssets; +use silicon::directories::PROJECT_DIRS; #[cfg(target_os = "linux")] pub fn dump_image_to_clipboard(image: &DynamicImage) -> Result<(), Error> { @@ -86,8 +87,12 @@ fn run() -> Result<(), Error> { if let Some(path) = config.build_cache { let mut ha = HighlightingAssets::new(); - ha.add_from_folder(path)?; - ha.dump_to_file()?; + ha.add_from_folder(env::current_dir()?)?; + if let Some(path) = path { + ha.dump_to_file(path)?; + } else { + ha.dump_to_file(PROJECT_DIRS.cache_dir())?; + } return Ok(()); } else if config.list_themes { for i in ts.themes.keys() { diff --git a/src/directories.rs b/src/directories.rs index 952e565..407da43 100644 --- a/src/directories.rs +++ b/src/directories.rs @@ -1,5 +1,6 @@ use lazy_static::lazy_static; use std::env; +use std::fs::create_dir_all; use std::path::{Path, PathBuf}; pub struct SiliconProjectDirs { @@ -22,6 +23,9 @@ impl SiliconProjectDirs { let config_dir = config_dir_op.map(|d| d.join("silicon"))?; + create_dir_all(&config_dir).expect("cannot create config dir"); + create_dir_all(&cache_dir).expect("cannot create cache dir"); + Some(Self { cache_dir, config_dir,