Skip to content

Commit

Permalink
Merge pull request #37 from lusingander/preview-syntaxes
Browse files Browse the repository at this point in the history
Fix to allow adding color theme syntax definitions for text preview
  • Loading branch information
lusingander authored Oct 19, 2024
2 parents 2698d1a + 5ca91d3 commit 6fe33cd
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
39 changes: 28 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,24 +150,41 @@ date_format = "%Y-%m-%d %H:%M:%S"
# type: bool
highlight = false
# The name of the color theme to use for syntax highlighting in the object preview.
# By default the following themes are available:
# - base16-ocean.dark
# - base16-eighties.dark
# - base16-mocha.dark
# - base16-ocean.light
# - https://github.com/SublimeText/Spacegray
# - InspiredGitHub
# - https://github.com/sethlopez/InspiredGitHub.tmtheme
# - Solarized (dark)
# - Solarized (light)
# Also, by creating `xxx.tmTheme` in `$STU_ROOT_DIR/preview_theme/`, you can use `xxx` and load it.
# type: string
highlight_theme = "base16-ocean.dark"
# Whether image file preview is enabled in the object preview.
# type: bool
image = false
```

### Syntax highlighting

In the object preview, Syntax highlighting using syntect is available. To enable this, set `preview.highlight = true` in the config file.

#### Color themes

You can change the color theme by specifying the theme name in `preview.highlight_theme`.

By default the following themes are available:

- base16-ocean.dark
- base16-eighties.dark
- base16-mocha.dark
- base16-ocean.light
- https://github.com/SublimeText/Spacegray
- InspiredGitHub
- https://github.com/sethlopez/InspiredGitHub.tmtheme
- Solarized (dark)
- Solarized (light)

Also, by creating `xxx.tmTheme` in `$STU_ROOT_DIR/preview_theme/`, you can use `xxx` and load it.

#### Syntax definitions

You can add syntax definitions for file types that are not supported by default. You can use it by creating a `.sublime-syntax` file in `$STU_ROOT_DIR/preview_syntax/`.

https://www.sublimetext.com/docs/syntax.html

## Features / Screenshots

### Bucket list
Expand Down
7 changes: 7 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const ERROR_LOG_FILE_NAME: &str = "error.log";
const DEBUG_LOG_FILE_NAME: &str = "debug.log";
const DOWNLOAD_DIR: &str = "download";
const PREVIEW_THEME_DIR: &str = "preview_theme";
const PREVIEW_SYNTAX_DIR: &str = "preview_syntax";
const CACHE_FILE_NAME: &str = "cache.txt";

#[derive(Serialize, Deserialize, Debug, Clone)]
Expand Down Expand Up @@ -164,6 +165,12 @@ impl Config {
Ok(String::from(path.to_string_lossy()))
}

pub fn preview_syntax_dir_path() -> anyhow::Result<String> {
let dir = Config::get_app_base_dir()?;
let path = dir.join(PREVIEW_SYNTAX_DIR);
Ok(String::from(path.to_string_lossy()))
}

fn get_app_base_dir() -> anyhow::Result<PathBuf> {
match env::var(STU_ROOT_DIR_ENV_VAR) {
Ok(dir) => Ok(PathBuf::from(dir)),
Expand Down
19 changes: 18 additions & 1 deletion src/widget/text_preview.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::PathBuf;

use ansi_to_tui::IntoText;
use once_cell::sync::Lazy;
use ratatui::{
Expand All @@ -22,7 +24,22 @@ use crate::{
widget::{ScrollLines, ScrollLinesOptions, ScrollLinesState},
};

static SYNTAX_SET: Lazy<SyntaxSet> = Lazy::new(SyntaxSet::load_defaults_newlines);
static SYNTAX_SET: Lazy<SyntaxSet> = Lazy::new(|| {
if let Ok(path) = Config::preview_syntax_dir_path() {
let path = PathBuf::from(path); // fixme: remove when function returns pathbuf directly
if path.exists() {
// SyntaxSetBuilder::build is terribly slow in debug build...
// To avoid unnecessary processing, we won't use the builder if the syntax directory doesn't exist...
let mut builder = SyntaxSet::load_defaults_newlines().into_builder();
builder.add_from_folder(path, true).unwrap();
builder.build()
} else {
SyntaxSet::load_defaults_newlines()
}
} else {
SyntaxSet::load_defaults_newlines()
}
});
static DEFAULT_THEME_SET: Lazy<ThemeSet> = Lazy::new(ThemeSet::load_defaults);
static USER_THEME_SET: Lazy<ThemeSet> = Lazy::new(|| {
Config::preview_theme_dir_path()
Expand Down

0 comments on commit 6fe33cd

Please sign in to comment.