Skip to content

Commit

Permalink
Use a custom scope to allow customizable font style in themes
Browse files Browse the repository at this point in the history
  • Loading branch information
mdibaiee committed Dec 24, 2021
1 parent 129210e commit 847d07b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
Binary file modified assets/themes.bin
Binary file not shown.
11 changes: 11 additions & 0 deletions assets/themes/ansi.tmTheme
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,17 @@
<string>#03000000</string>
</dict>
</dict>
<dict>
<key>name</key>
<string>Line highlight style</string>
<key>scope</key>
<string>line_highlight</string>
<key>settings</key>
<dict>
<key>fontStyle</key>
<string>underline</string>
</dict>
</dict>
</array>
<key>uuid</key>
<string>uuid</string>
Expand Down
46 changes: 42 additions & 4 deletions src/printer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::io::Write;
use std::str::FromStr;
use std::vec::Vec;

use ansi_term::Colour::{Fixed, Green, Red, Yellow};
Expand All @@ -8,7 +9,9 @@ use console::AnsiCodeIterator;

use syntect::easy::HighlightLines;
use syntect::highlighting::Color;
use syntect::highlighting::FontStyle;
use syntect::highlighting::Theme;
use syntect::parsing::ScopeStack;
use syntect::parsing::SyntaxSet;

use content_inspector::ContentType;
Expand Down Expand Up @@ -125,6 +128,7 @@ pub(crate) struct InteractivePrinter<'a> {
pub line_changes: &'a Option<LineChanges>,
highlighter_from_set: Option<HighlighterFromSet<'a>>,
background_color_highlight: Option<Color>,
font_style_highlight: Option<FontStyle>,
}

impl<'a> InteractivePrinter<'a> {
Expand All @@ -138,6 +142,17 @@ impl<'a> InteractivePrinter<'a> {

let background_color_highlight = theme.settings.line_highlight;

let font_style_highlight = theme
.scopes
.iter()
.find(|dict| {
dict.scope
.does_match(ScopeStack::from_str("line_highlight").unwrap().as_slice())
.is_some()
})
.map(|item| item.style.font_style)
.flatten();

let colors = if config.colored_output {
Colors::colored(theme, config.true_color)
} else {
Expand Down Expand Up @@ -208,6 +223,7 @@ impl<'a> InteractivePrinter<'a> {
line_changes,
highlighter_from_set,
background_color_highlight,
font_style_highlight,
})
}

Expand Down Expand Up @@ -421,8 +437,19 @@ impl<'a> Printer for InteractivePrinter<'a> {
let highlight_this_line =
self.config.highlighted_lines.0.check(line_number) == RangeCheckResult::InRange;

if highlight_this_line && self.config.theme == "ansi" {
self.ansi_style.update("^[4m");
if highlight_this_line {
match self.font_style_highlight {
Some(FontStyle::BOLD) => {
self.ansi_style.update("^[1m");
}
Some(FontStyle::ITALIC) => {
self.ansi_style.update("^[3m");
}
Some(FontStyle::UNDERLINE) => {
self.ansi_style.update("^[4m");
}
_ => (),
}
}

let background_color = self
Expand Down Expand Up @@ -611,8 +638,19 @@ impl<'a> Printer for InteractivePrinter<'a> {
writeln!(handle)?;
}

if highlight_this_line && self.config.theme == "ansi" {
self.ansi_style.update("^[24m");
if highlight_this_line {
match self.font_style_highlight {
Some(FontStyle::BOLD) => {
self.ansi_style.update("^[22m");
}
Some(FontStyle::ITALIC) => {
self.ansi_style.update("^[23m");
}
Some(FontStyle::UNDERLINE) => {
self.ansi_style.update("^[24m");
}
_ => (),
}
}

Ok(())
Expand Down

0 comments on commit 847d07b

Please sign in to comment.