Skip to content

Select out of the defaults syntect syntax themes in theme.ron #2532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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))
* 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))

### Changed
Expand Down
1 change: 1 addition & 0 deletions src/components/syntax_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ impl SyntaxTextComponent {
AsyncSyntaxJob::new(
content.clone(),
path.clone(),
self.theme.get_syntax(),
),
);

Expand Down
1 change: 1 addition & 0 deletions src/popups/blame_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,7 @@ impl BlameFilePopup {
job.spawn(AsyncSyntaxJob::new(
text,
params.file_path.clone(),
self.theme.get_syntax(),
));
}

Expand Down
12 changes: 12 additions & 0 deletions src/ui/style.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -34,6 +35,7 @@ pub struct Theme {
branch_fg: Color,
line_break: String,
block_title_focused: Color,
syntax: String,
}

impl Theme {
Expand Down Expand Up @@ -298,6 +300,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();

Expand Down Expand Up @@ -353,6 +359,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: DEFAULT_SYNTAX_THEME.to_string(),
}
}
}
Expand All @@ -378,6 +387,7 @@ mod tests {
(
selection_bg: Some("Black"),
selection_fg: Some("#ffffff"),
syntax: Some("InspiredGitHub")
)
"##
)
Expand All @@ -388,7 +398,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");
}
}
21 changes: 17 additions & 4 deletions src/ui/syntax_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use syntect::{

use crate::{AsyncAppNotification, SyntaxHighlightProgress};

pub const DEFAULT_SYNTAX_THEME: &str = "base16-eighties.dark";

struct SyntaxLine {
items: Vec<(Style, usize, Range<usize>)>,
}
Expand Down Expand Up @@ -70,6 +72,7 @@ impl SyntaxText {
text: String,
file_path: &Path,
params: &RunParams<AsyncAppNotification, ProgressPercent>,
syntax: &str,
) -> asyncgit::Result<Self> {
scope_time!("syntax_highlighting");
let mut state = {
Expand All @@ -86,9 +89,12 @@ 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(|| {
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);

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

Expand Down Expand Up @@ -212,14 +218,20 @@ enum JobState {
#[derive(Clone, Default)]
pub struct AsyncSyntaxJob {
state: Arc<Mutex<Option<JobState>>>,
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,
}
}

Expand Down Expand Up @@ -255,6 +267,7 @@ impl AsyncJob for AsyncSyntaxJob {
content,
Path::new(&path),
&params,
&self.syntax,
)?;
JobState::Response(syntax)
}
Expand Down