From f8ad257fda2c7a843da6542b563b4efb0bc3ad49 Mon Sep 17 00:00:00 2001 From: Vasilis Manolopoulos Date: Sat, 31 Aug 2024 13:08:00 +0200 Subject: [PATCH 1/3] Select one of the defaults syntect syntax themes in theme.ron --- CHANGELOG.md | 3 +++ src/components/syntax_text.rs | 1 + src/popups/blame_file.rs | 1 + src/ui/style.rs | 11 +++++++++++ src/ui/syntax_text.rs | 18 ++++++++++++++---- 5 files changed, 30 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c47274c74..3d202b3bbf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +### Added +* Select syntax highlighting theme out of the defaults from syntect [[@vasilismanol](https://github.com/vasilismanol)] ([#1931](https://github.com/extrawurst/gitui/issues/1931)) + ### Changed * improve syntax highlighting file detection [[@acuteenvy](https://github.com/acuteenvy)] ([#2524](https://github.com/extrawurst/gitui/pull/2524)) * After commit: jump back to unstaged area [[@tommady](https://github.com/tommady)] ([#2476](https://github.com/extrawurst/gitui/issues/2476)) diff --git a/src/components/syntax_text.rs b/src/components/syntax_text.rs index aaf348ddaf..7a82aca273 100644 --- a/src/components/syntax_text.rs +++ b/src/components/syntax_text.rs @@ -117,6 +117,7 @@ impl SyntaxTextComponent { AsyncSyntaxJob::new( content.clone(), path.clone(), + self.theme.get_syntax(), ), ); diff --git a/src/popups/blame_file.rs b/src/popups/blame_file.rs index 80369b0470..273ca5ace3 100644 --- a/src/popups/blame_file.rs +++ b/src/popups/blame_file.rs @@ -576,6 +576,7 @@ impl BlameFilePopup { job.spawn(AsyncSyntaxJob::new( text, params.file_path.clone(), + self.theme.get_syntax(), )); } diff --git a/src/ui/style.rs b/src/ui/style.rs index 839ec380fb..391515c4f7 100644 --- a/src/ui/style.rs +++ b/src/ui/style.rs @@ -34,6 +34,7 @@ pub struct Theme { branch_fg: Color, line_break: String, block_title_focused: Color, + syntax: String, } impl Theme { @@ -298,6 +299,10 @@ impl Theme { Ok(()) } + pub fn get_syntax(&self) -> String { + self.syntax.clone() + } + pub fn init(theme_path: &PathBuf) -> Self { let mut theme = Self::default(); @@ -353,6 +358,9 @@ impl Default for Theme { branch_fg: Color::LightYellow, line_break: "ΒΆ".to_string(), block_title_focused: Color::Reset, + // Available themes can be found in: + // [ThemeSet::load_defaults function](https://github.com/trishume/syntect/blob/7fe13c0fd53cdfa0f9fea1aa14c5ba37f81d8b71/src/dumps.rs#L215). + syntax: "base16-eighties.dark".to_string(), } } } @@ -378,6 +386,7 @@ mod tests { ( selection_bg: Some("Black"), selection_fg: Some("#ffffff"), + syntax: Some("InspiredGitHub") ) "## ) @@ -388,7 +397,9 @@ mod tests { assert_eq!(theme.selected_tab, Theme::default().selected_tab); assert_ne!(theme.selection_bg, Theme::default().selection_bg); + assert_ne!(theme.syntax, Theme::default().syntax); assert_eq!(theme.selection_bg, Color::Black); assert_eq!(theme.selection_fg, Color::Rgb(255, 255, 255)); + assert_eq!(theme.syntax, "InspiredGitHub"); } } diff --git a/src/ui/syntax_text.rs b/src/ui/syntax_text.rs index 20820df915..62bd20dd37 100644 --- a/src/ui/syntax_text.rs +++ b/src/ui/syntax_text.rs @@ -70,6 +70,7 @@ impl SyntaxText { text: String, file_path: &Path, params: &RunParams, + syntax: &str, ) -> asyncgit::Result { scope_time!("syntax_highlighting"); let mut state = { @@ -86,9 +87,11 @@ impl SyntaxText { ParseState::new(syntax) }; - let highlighter = Highlighter::new( - &THEME_SET.themes["base16-eighties.dark"], - ); + let theme = + THEME_SET.themes.get(syntax).unwrap_or_else(|| { + &THEME_SET.themes["base16-eighties.dark"] + }); + let highlighter = Highlighter::new(theme); let mut syntax_lines: Vec = Vec::new(); @@ -212,14 +215,20 @@ enum JobState { #[derive(Clone, Default)] pub struct AsyncSyntaxJob { state: Arc>>, + syntax: String, } impl AsyncSyntaxJob { - pub fn new(content: String, path: String) -> Self { + pub fn new( + content: String, + path: String, + syntax: String, + ) -> Self { Self { state: Arc::new(Mutex::new(Some(JobState::Request(( content, path, ))))), + syntax, } } @@ -255,6 +264,7 @@ impl AsyncJob for AsyncSyntaxJob { content, Path::new(&path), ¶ms, + &self.syntax, )?; JobState::Response(syntax) } From 017cbf6199cfce132ef453cc9b6e3180e2d18ab8 Mon Sep 17 00:00:00 2001 From: Vasilis Manolopoulos Date: Mon, 24 Feb 2025 20:59:36 +0100 Subject: [PATCH 2/3] Log info when syntax theme not found and using default. --- src/ui/syntax_text.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/ui/syntax_text.rs b/src/ui/syntax_text.rs index 62bd20dd37..cc29e01e7e 100644 --- a/src/ui/syntax_text.rs +++ b/src/ui/syntax_text.rs @@ -21,6 +21,8 @@ use syntect::{ use crate::{AsyncAppNotification, SyntaxHighlightProgress}; +const DEFAULT_SYNTAX_THEME: &str = "base16-eighties.dark"; + struct SyntaxLine { items: Vec<(Style, usize, Range)>, } @@ -89,7 +91,8 @@ impl SyntaxText { let theme = THEME_SET.themes.get(syntax).unwrap_or_else(|| { - &THEME_SET.themes["base16-eighties.dark"] + log::info!("The syntax theme:\"{}\" cannot be found. Using default theme:\"{}\" instead.", syntax, DEFAULT_SYNTAX_THEME); + &THEME_SET.themes[DEFAULT_SYNTAX_THEME] }); let highlighter = Highlighter::new(theme); From fb0b3f6a5e9247ae130a25ae054bfe505b3a7ef3 Mon Sep 17 00:00:00 2001 From: Vasilis Manolopoulos Date: Sat, 15 Mar 2025 21:25:54 +0100 Subject: [PATCH 3/3] Use default theme constant and log::error when missing theme --- src/ui/style.rs | 3 ++- src/ui/syntax_text.rs | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/ui/style.rs b/src/ui/style.rs index 391515c4f7..7913835766 100644 --- a/src/ui/style.rs +++ b/src/ui/style.rs @@ -1,3 +1,4 @@ +use crate::ui::syntax_text::DEFAULT_SYNTAX_THEME; use anyhow::Result; use asyncgit::{DiffLineType, StatusItemType}; use ratatui::style::{Color, Modifier, Style}; @@ -360,7 +361,7 @@ impl Default for Theme { block_title_focused: Color::Reset, // Available themes can be found in: // [ThemeSet::load_defaults function](https://github.com/trishume/syntect/blob/7fe13c0fd53cdfa0f9fea1aa14c5ba37f81d8b71/src/dumps.rs#L215). - syntax: "base16-eighties.dark".to_string(), + syntax: DEFAULT_SYNTAX_THEME.to_string(), } } } diff --git a/src/ui/syntax_text.rs b/src/ui/syntax_text.rs index cc29e01e7e..e0cd5bf6c5 100644 --- a/src/ui/syntax_text.rs +++ b/src/ui/syntax_text.rs @@ -21,7 +21,7 @@ use syntect::{ use crate::{AsyncAppNotification, SyntaxHighlightProgress}; -const DEFAULT_SYNTAX_THEME: &str = "base16-eighties.dark"; +pub const DEFAULT_SYNTAX_THEME: &str = "base16-eighties.dark"; struct SyntaxLine { items: Vec<(Style, usize, Range)>, @@ -91,7 +91,7 @@ impl SyntaxText { let theme = THEME_SET.themes.get(syntax).unwrap_or_else(|| { - log::info!("The syntax theme:\"{}\" cannot be found. Using default theme:\"{}\" instead.", syntax, DEFAULT_SYNTAX_THEME); + log::error!("The syntax theme:\"{}\" cannot be found. Using default theme:\"{}\" instead.", syntax, DEFAULT_SYNTAX_THEME); &THEME_SET.themes[DEFAULT_SYNTAX_THEME] }); let highlighter = Highlighter::new(theme);