Skip to content

Commit 597e944

Browse files
authored
Select out of the defaults syntect syntax themes in theme.ron (#2532)
1 parent 4031b0d commit 597e944

File tree

5 files changed

+32
-4
lines changed

5 files changed

+32
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
## Unreleased
99

1010
### Added
11+
* Select syntax highlighting theme out of the defaults from syntect [[@vasilismanol](https://github.com/vasilismanol)] ([#1931](https://github.com/extrawurst/gitui/issues/1931))
1112
* new command-line option to override the default log file path (`--logfile`) [[@acuteenvy](https://github.com/acuteenvy)] ([#2539](https://github.com/gitui-org/gitui/pull/2539))
1213

1314
### Changed

src/components/syntax_text.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ impl SyntaxTextComponent {
117117
AsyncSyntaxJob::new(
118118
content.clone(),
119119
path.clone(),
120+
self.theme.get_syntax(),
120121
),
121122
);
122123

src/popups/blame_file.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,7 @@ impl BlameFilePopup {
576576
job.spawn(AsyncSyntaxJob::new(
577577
text,
578578
params.file_path.clone(),
579+
self.theme.get_syntax(),
579580
));
580581
}
581582

src/ui/style.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::ui::syntax_text::DEFAULT_SYNTAX_THEME;
12
use anyhow::Result;
23
use asyncgit::{DiffLineType, StatusItemType};
34
use ratatui::style::{Color, Modifier, Style};
@@ -34,6 +35,7 @@ pub struct Theme {
3435
branch_fg: Color,
3536
line_break: String,
3637
block_title_focused: Color,
38+
syntax: String,
3739
}
3840

3941
impl Theme {
@@ -298,6 +300,10 @@ impl Theme {
298300
Ok(())
299301
}
300302

303+
pub fn get_syntax(&self) -> String {
304+
self.syntax.clone()
305+
}
306+
301307
pub fn init(theme_path: &PathBuf) -> Self {
302308
let mut theme = Self::default();
303309

@@ -353,6 +359,9 @@ impl Default for Theme {
353359
branch_fg: Color::LightYellow,
354360
line_break: "¶".to_string(),
355361
block_title_focused: Color::Reset,
362+
// Available themes can be found in:
363+
// [ThemeSet::load_defaults function](https://github.com/trishume/syntect/blob/7fe13c0fd53cdfa0f9fea1aa14c5ba37f81d8b71/src/dumps.rs#L215).
364+
syntax: DEFAULT_SYNTAX_THEME.to_string(),
356365
}
357366
}
358367
}
@@ -378,6 +387,7 @@ mod tests {
378387
(
379388
selection_bg: Some("Black"),
380389
selection_fg: Some("#ffffff"),
390+
syntax: Some("InspiredGitHub")
381391
)
382392
"##
383393
)
@@ -388,7 +398,9 @@ mod tests {
388398
assert_eq!(theme.selected_tab, Theme::default().selected_tab);
389399

390400
assert_ne!(theme.selection_bg, Theme::default().selection_bg);
401+
assert_ne!(theme.syntax, Theme::default().syntax);
391402
assert_eq!(theme.selection_bg, Color::Black);
392403
assert_eq!(theme.selection_fg, Color::Rgb(255, 255, 255));
404+
assert_eq!(theme.syntax, "InspiredGitHub");
393405
}
394406
}

src/ui/syntax_text.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ use syntect::{
2121

2222
use crate::{AsyncAppNotification, SyntaxHighlightProgress};
2323

24+
pub const DEFAULT_SYNTAX_THEME: &str = "base16-eighties.dark";
25+
2426
struct SyntaxLine {
2527
items: Vec<(Style, usize, Range<usize>)>,
2628
}
@@ -70,6 +72,7 @@ impl SyntaxText {
7072
text: String,
7173
file_path: &Path,
7274
params: &RunParams<AsyncAppNotification, ProgressPercent>,
75+
syntax: &str,
7376
) -> asyncgit::Result<Self> {
7477
scope_time!("syntax_highlighting");
7578
let mut state = {
@@ -86,9 +89,12 @@ impl SyntaxText {
8689
ParseState::new(syntax)
8790
};
8891

89-
let highlighter = Highlighter::new(
90-
&THEME_SET.themes["base16-eighties.dark"],
91-
);
92+
let theme =
93+
THEME_SET.themes.get(syntax).unwrap_or_else(|| {
94+
log::error!("The syntax theme:\"{}\" cannot be found. Using default theme:\"{}\" instead.", syntax, DEFAULT_SYNTAX_THEME);
95+
&THEME_SET.themes[DEFAULT_SYNTAX_THEME]
96+
});
97+
let highlighter = Highlighter::new(theme);
9298

9399
let mut syntax_lines: Vec<SyntaxLine> = Vec::new();
94100

@@ -212,14 +218,20 @@ enum JobState {
212218
#[derive(Clone, Default)]
213219
pub struct AsyncSyntaxJob {
214220
state: Arc<Mutex<Option<JobState>>>,
221+
syntax: String,
215222
}
216223

217224
impl AsyncSyntaxJob {
218-
pub fn new(content: String, path: String) -> Self {
225+
pub fn new(
226+
content: String,
227+
path: String,
228+
syntax: String,
229+
) -> Self {
219230
Self {
220231
state: Arc::new(Mutex::new(Some(JobState::Request((
221232
content, path,
222233
))))),
234+
syntax,
223235
}
224236
}
225237

@@ -255,6 +267,7 @@ impl AsyncJob for AsyncSyntaxJob {
255267
content,
256268
Path::new(&path),
257269
&params,
270+
&self.syntax,
258271
)?;
259272
JobState::Response(syntax)
260273
}

0 commit comments

Comments
 (0)