Skip to content

Commit

Permalink
fix: build cache output dir
Browse files Browse the repository at this point in the history
  • Loading branch information
Aloxaf committed Oct 13, 2022
1 parent 6a3f037 commit e0fd52b
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "silicon"
version = "0.4.3"
version = "0.5.0"
description = "Create beautiful image of your code"
authors = ["Aloxaf <aloxafx@gmail.com>"]
categories = ["command-line-utilities"]
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<P: AsRef<Path>>(&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(())
}
}
4 changes: 2 additions & 2 deletions src/bin/silicon/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ pub struct Config {
// #[structopt(long)]
// watermark: Option<String>,
/// build syntax definition and theme cache
#[structopt(long)]
pub build_cache: Option<PathBuf>,
#[structopt(long, value_name = "OUTPUT_DIR")]
pub build_cache: Option<Option<PathBuf>>,
}

impl Config {
Expand Down
13 changes: 9 additions & 4 deletions src/bin/silicon/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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> {
Expand Down Expand Up @@ -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() {
Expand Down
4 changes: 4 additions & 0 deletions src/directories.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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,
Expand Down

0 comments on commit e0fd52b

Please sign in to comment.