Skip to content

Commit

Permalink
Allow dynamic loading of themes and syntaxes
Browse files Browse the repository at this point in the history
Signed-off-by: Bastian Rinsche <[email protected]>
  • Loading branch information
brinsche committed Mar 5, 2018
1 parent 58a5900 commit ae1cbd8
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 9 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,12 @@ Setting a theme (default is an [included xcode-like theme](qlhighlight/res/Xcode

Available themes are the default theme and [these](https://docs.rs/syntect/2.0.0/syntect/highlighting/struct.ThemeSet.html#method.load_defaults).

Adding additional themes. Set to absolute path of directory containing `.tmTheme` files:

defaults write de.bastianrinsche.QLSyntaxHighlight themeDirectory '/path/to/themes'

Adding addiontal syntaxes. Set to absolute path of directory containing `.sublime-syntax` files:

defaults write de.bastianrinsche.QLSyntaxHighlight syntaxDirectory '/path/to/syntaxes'

![screenshot](img/screenshot.png)
28 changes: 25 additions & 3 deletions qlhighlight/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod quicklook;

use std::fmt::Write;
use std::io::Cursor;
use std::path::Path;

use core_foundation::base::{OSStatus, TCFType};
use core_foundation::data::CFData;
Expand Down Expand Up @@ -36,9 +37,30 @@ pub extern "C" fn GeneratePreviewForURL(
let theme_bytes = include_bytes!("../res/XCodelike.tmTheme");
let xcode_theme = ThemeSet::load_from_reader(&mut Cursor::new(&theme_bytes[..])).unwrap();

let ts = ThemeSet::load_defaults();
let syntax_set = SyntaxSet::load_defaults_nonewlines();
let theme = ts.themes
let mut theme_set = ThemeSet::load_defaults();
let mut syntax_set = SyntaxSet::load_defaults_nonewlines();

if let Some(theme_dir) = conf.theme_dir {
let directory = theme_dir.to_string();
let theme_dir = Path::new(&directory);
if let Ok(mut custom_themes) = ThemeSet::load_from_folder(&theme_dir) {
theme_set.themes.append(&mut custom_themes.themes);
}
};

if let Some(syntax_dir) = conf.syntax_dir {
let directory = syntax_dir.to_string();
let syntax_dir = Path::new(&directory);
if let Ok(mut custom_syntaxes) = SyntaxSet::load_from_folder(&syntax_dir) {
for set in custom_syntaxes.syntaxes() {
syntax_set.add_syntax(set.clone());
}
syntax_set.link_syntaxes()
}
};

let theme = theme_set
.themes
.get(&conf.theme_name.to_string())
.unwrap_or(&xcode_theme);

Expand Down
1 change: 1 addition & 0 deletions qlhighlight/src/quicklook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use core_foundation::data::CFDataRef;
use core_foundation::string::CFStringRef;
use core_foundation::dictionary::CFDictionaryRef;

#[allow(dead_code)]
extern "C" {
#[link_name = "kUTTypeHTML"]
pub static kUTTypeHTML: CFStringRef;
Expand Down
32 changes: 26 additions & 6 deletions qlhighlight/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,28 @@ extern "C" {
pub static kCFPreferencesAnyHost: CFStringRef;
}

const FONT_SIZE: &'static str = "fontSize";
const FONT_FAMILY: &'static str = "fontFamily";
const THEME: &'static str = "theme";
const THEME_DIR: &'static str = "themeDirectory";
const SYNTAX_DIR: &'static str = "syntaxDirectory";

#[derive(Debug)]
pub struct Config {
pub font_size: CFString,
pub font_family: CFString,
pub theme_name: CFString,
pub theme_dir: Option<CFString>,
pub syntax_dir: Option<CFString>,
}

pub fn get_settings() -> Config {
let keys = CFArray::from_CFTypes(&[
CFString::new("fontSize"),
CFString::new("fontFamily"),
CFString::new("theme"),
CFString::new(FONT_SIZE),
CFString::new(FONT_FAMILY),
CFString::new(THEME),
CFString::new(THEME_DIR),
CFString::new(SYNTAX_DIR),
]);

let prefs = unsafe {
Expand All @@ -43,23 +53,33 @@ pub fn get_settings() -> Config {
let prefs: CFDictionary = unsafe { CFDictionary::wrap_under_get_rule(prefs) };

let font_size = prefs
.find2(&CFString::new("fontSize"))
.find2(&CFString::new(FONT_SIZE))
.and_then(|ptr| unsafe { CFType::wrap_under_create_rule(ptr).downcast::<CFString>() })
.unwrap_or(CFString::new("11"));

let font_family = prefs
.find2(&CFString::new("fontFamily"))
.find2(&CFString::new(FONT_FAMILY))
.and_then(|ptr| unsafe { CFType::wrap_under_create_rule(ptr).downcast::<CFString>() })
.unwrap_or(CFString::new("Menlo, monospace"));

let theme_name = prefs
.find2(&CFString::new("theme"))
.find2(&CFString::new(THEME))
.and_then(|ptr| unsafe { CFType::wrap_under_create_rule(ptr).downcast::<CFString>() })
.unwrap_or(CFString::new("InspiredGithub"));

let theme_dir = prefs
.find2(&CFString::new(THEME_DIR))
.and_then(|ptr| unsafe { CFType::wrap_under_create_rule(ptr).downcast::<CFString>() });

let syntax_dir = prefs
.find2(&CFString::new(SYNTAX_DIR))
.and_then(|ptr| unsafe { CFType::wrap_under_create_rule(ptr).downcast::<CFString>() });

Config {
font_size,
font_family,
theme_name,
theme_dir,
syntax_dir,
}
}

0 comments on commit ae1cbd8

Please sign in to comment.