Skip to content

Commit

Permalink
Add support for external syntax&theme
Browse files Browse the repository at this point in the history
  • Loading branch information
Aloxaf committed Jul 10, 2019
1 parent 50c782a commit 3c79497
Show file tree
Hide file tree
Showing 7 changed files with 511 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
**/*.rs.bk
assets/*.bin
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ structopt = { version = "0.2.18", default-features = false, features = [ "color"
itertools = "0.8.0"

[build-dependencies]
syntect = "3.2"
cc = "1.0.37"

[profile.release]
Expand Down
451 changes: 451 additions & 0 deletions assets/syntaxes/PowerShellSyntax.sublime-syntax

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,33 @@
use syntect::dumps::dump_to_file;
use syntect::highlighting::ThemeSet;
use syntect::parsing::SyntaxSet;

fn create_theme_dump() {
let mut default = ThemeSet::load_defaults();
let from_folder = ThemeSet::load_from_folder("./assets/themes").expect("failed to load themes");

default.themes.extend(from_folder.themes.into_iter());

dump_to_file(&default, "./assets/themes.bin").expect("failed to dump");
}

fn create_syntax_dump() {
let mut builder = SyntaxSet::load_defaults_newlines().into_builder();
builder.add_plain_text_syntax();

builder
.add_from_folder("./assets/syntaxes", true)
.expect("failed to load syntaxes");

let default = builder.build();

dump_to_file(&default, "./assets/syntaxes.bin").expect("failed to dump");
}

fn main() {
create_theme_dump();
create_syntax_dump();

cc::Build::new()
.file("src/gauss/gauss.c")
.cpp(false)
Expand Down
18 changes: 7 additions & 11 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ pub struct Config {
#[structopt(long, default_value = "2")]
line_pad: u32,

// List all themes.
// #[structopt(long)]
// list_themes: bool,
/// List all themes.
#[structopt(long)]
pub list_themes: bool,

// Build theme cache.
// #[structopt(long)]
Expand All @@ -58,7 +58,7 @@ pub struct Config {
// to_clipboard: bool,
/// Write output image to specific location instead of cwd.
#[structopt(short = "o", long, value_name = "path")]
output: PathBuf,
pub output: Option<PathBuf>,

/// Hide the window controls.
#[structopt(long)]
Expand Down Expand Up @@ -132,7 +132,7 @@ impl Config {

let language = if let Some(language) = &self.language {
ps.find_syntax_by_token(language)
.ok_or(format_err!("There is no such a language: {}", language))?
.ok_or(format_err!("Unsupported language: {}", language))?
} else {
ps.find_syntax_by_first_line(&code)
.ok_or(format_err!("Failed to detect the language"))?
Expand All @@ -148,7 +148,7 @@ impl Config {

let language = if let Some(language) = &self.language {
ps.find_syntax_by_token(language)
.ok_or(format_err!("There is no such a language: {}", language))?
.ok_or(format_err!("Unsupported language: {}", language))?
} else {
ps.find_syntax_for_file(path)?
.ok_or(format_err!("Failed to detect the language"))?
Expand All @@ -163,7 +163,7 @@ impl Config {

let language = if let Some(language) = &self.language {
ps.find_syntax_by_token(language)
.ok_or(format_err!("There is no such a language: {}", language))?
.ok_or(format_err!("Unsupported language: {}", language))?
} else {
ps.find_syntax_by_first_line(&s)
.ok_or(format_err!("Failed to detect the language"))?
Expand Down Expand Up @@ -218,8 +218,4 @@ impl Config {
.offset_x(self.shadow_offset_x)
.offset_y(self.shadow_offset_y)
}

pub fn output(&self) -> &PathBuf {
&self.output
}
}
4 changes: 2 additions & 2 deletions src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use crate::utils::ToRgba;
use failure::Error;
use image::{DynamicImage, Rgba, RgbaImage};
use imageproc::drawing::draw_text_mut;
use itertools::Itertools;
use rusttype::Font;
use syntect::highlighting::{Color, Style, Theme};
use itertools::Itertools;

pub struct ImageFormatter<'a> {
/// pad between lines
Expand Down Expand Up @@ -206,7 +206,7 @@ impl<'a> ImageFormatter<'a> {
continue;
}

if text.as_bytes()[0].is_ascii() ||self.cjk_font.is_none() {
if text.as_bytes()[0].is_ascii() || self.cjk_font.is_none() {
// let text = text.trim_end_matches('\n');
let font = self.font.get_by_style(style);

Expand Down
26 changes: 20 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ extern crate failure;
use crate::config::Config;
use crate::utils::{add_window_controls, round_corner};
use failure::Error;
use std::io::stdout;
use structopt::StructOpt;
use syntect::dumps;
use syntect::easy::HighlightLines;
use syntect::highlighting::ThemeSet;
use syntect::parsing::SyntaxSet;
use syntect::util::LinesWithEndings;
use image::ImageFormat;

mod blur;
mod config;
Expand All @@ -19,8 +22,15 @@ mod utils;
fn run() -> Result<(), Error> {
let config: Config = Config::from_args();

let ps = SyntaxSet::load_defaults_newlines();
let ts = ThemeSet::load_defaults();
let ps = dumps::from_binary::<SyntaxSet>(include_bytes!("../assets/syntaxes.bin")); //SyntaxSet::load_defaults_newlines();
let ts = dumps::from_binary::<ThemeSet>(include_bytes!("../assets/themes.bin")); // ThemeSet::load();

if config.list_themes {
for i in ts.themes.keys() {
println!("{}", i);
}
return Ok(());
}

let (syntax, code) = config.get_source_code(&ps)?;

Expand All @@ -43,10 +53,14 @@ fn run() -> Result<(), Error> {

let image = config.get_shadow_adder().apply_to(&image);

let path = config.output();
image
.save(path)
.map_err(|e| format_err!("Failed to save image to {}: {}", path.display(), e))?;
if let Some(path) = &config.output {
image
.save(path)
.map_err(|e| format_err!("Failed to save image to {}: {}", path.display(), e))?;
} else {
let mut stdout = stdout();
image.write_to(&mut stdout, ImageFormat::PNG)?;
}

Ok(())
}
Expand Down

0 comments on commit 3c79497

Please sign in to comment.