Skip to content

Commit

Permalink
added termbg library for color highlighting
Browse files Browse the repository at this point in the history
removed TerminalBackground library
  • Loading branch information
YorickdeJong committed Feb 20, 2024
1 parent 022dae0 commit c48f42b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
45 changes: 30 additions & 15 deletions implementations/rust/ockam/ockam_command/src/docs.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::terminal::TerminalBackground;
use termbg;
use r3bl_ansi_color::{AnsiStyledText, Style as StyleAnsi, Color};
use colorful::Colorful;
use ockam_core::env::get_env_with_default;
use once_cell::sync::Lazy;
use syntect::{
easy::HighlightLines,
highlighting::{Style, Theme, ThemeSet},
highlighting::{Style, Theme as SyntectTheme, ThemeSet},
parsing::Regex,
parsing::SyntaxSet,
util::LinesWithEndings,
Expand All @@ -27,15 +27,22 @@ discord channel https://discord.ockam.io

static SYNTAX_SET: Lazy<SyntaxSet> = Lazy::new(SyntaxSet::load_defaults_newlines);
static HEADER_RE: Lazy<Regex> = Lazy::new(|| Regex::new("^[A-Za-z][A-Za-z0-9 ]+:$".into()));
static THEME: Lazy<Theme> = Lazy::new(|| {
let theme_name = match TerminalBackground::detect_background_color() {
TerminalBackground::Light => "base16-ocean.light",
TerminalBackground::Dark => "base16-ocean.dark",
TerminalBackground::Unknown => "base16-ocean.dark",
};
let mut theme_set = ThemeSet::load_defaults();
let theme = theme_set.themes.remove(theme_name).unwrap();
theme
static THEME: Lazy<Option<SyntectTheme>> = Lazy::new(|| {
let timeout = std::time::Duration::from_millis(100);

match termbg::theme(timeout) {
Ok(termbg::Theme::Light) => {
let mut theme_set = ThemeSet::load_defaults();
theme_set.themes.remove("base16-ocean.light")
},
Ok(termbg::Theme::Dark) => {
let mut theme_set = ThemeSet::load_defaults();
theme_set.themes.remove("base16-ocean.dark")
},
Err(_) => {
None
}
}
});

fn is_markdown() -> bool {
Expand Down Expand Up @@ -112,22 +119,30 @@ fn process_terminal_docs(input: String) -> String {
output.join("")
}

pub struct FencedCodeBlockHighlighter<'a> {
struct FencedCodeBlockHighlighter<'a> {
inner: HighlightLines<'a>,
in_fenced_block: bool,
}

static DEFAULT_THEME: Lazy<SyntectTheme> = Lazy::new(|| {
let mut theme_set = ThemeSet::load_defaults();
theme_set.themes.remove("base16-ocean.light").expect("Default theme must exist")
});

impl FencedCodeBlockHighlighter<'_> {
pub fn new() -> Self {
fn new() -> Self {
let syntax = SYNTAX_SET.find_syntax_by_extension("sh").unwrap();
let inner = HighlightLines::new(syntax, &*THEME);

let theme = THEME.as_ref().unwrap_or(&DEFAULT_THEME);
let inner = HighlightLines::new(syntax, theme);

Self {
inner,
in_fenced_block: false,
}
}

pub fn process_line(&mut self, line: &str, output: &mut Vec<String>) -> bool {
fn process_line(&mut self, line: &str, output: &mut Vec<String>) -> bool {
if line == "```sh\n" {
self.in_fenced_block = true;
return true;
Expand Down
2 changes: 1 addition & 1 deletion implementations/rust/ockam/ockam_command/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ mod authority;
mod completion;
mod configuration;
mod credential;
pub mod docs;
mod docs;
pub mod enroll;
mod environment;
pub mod error;
Expand Down

0 comments on commit c48f42b

Please sign in to comment.