From 0500a95430fccfe7ce01c0cbe9f2c4b81783978f Mon Sep 17 00:00:00 2001 From: sxyazi Date: Sat, 30 Dec 2023 01:21:38 +0800 Subject: [PATCH] Port `ansi.tmTheme` to `yazi-prebuild` crate --- Cargo.lock | 4 +- yazi-config/preset/ansi.tmTheme | 762 ------------------------ yazi-plugin/Cargo.toml | 2 +- yazi-plugin/src/external/highlighter.rs | 33 +- 4 files changed, 17 insertions(+), 784 deletions(-) delete mode 100644 yazi-config/preset/ansi.tmTheme diff --git a/Cargo.lock b/Cargo.lock index 2a5739cf..137dc1b4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2736,9 +2736,9 @@ dependencies = [ [[package]] name = "yazi-prebuild" -version = "0.1.1" +version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "394457709d77af570fcb3c0ef878336213b4e9c8f298eccde728aecd602aa7e0" +checksum = "f4b6c8e12e39ac0f79fa96f36e5b88e0da8d230691abd729eec709b43c74f632" [[package]] name = "yazi-scheduler" diff --git a/yazi-config/preset/ansi.tmTheme b/yazi-config/preset/ansi.tmTheme deleted file mode 100644 index b84fcbfa..00000000 --- a/yazi-config/preset/ansi.tmTheme +++ /dev/null @@ -1,762 +0,0 @@ - - - - - author - Chiel ten Brinke - colorSpaceName - sRGB - comment - Generated from the neo-ansi template at https://github.com/chtenb/neo-ansi. The colors in this theme are encoded as RRGGBBAA where RR is an ANSI palette number from 00 to 0f (the 8 colors and their bright versions), and AA is the special value 00 to indicate this encoding is being used. - name - Neo-ansi Terminal Theme for Bat - semanticClass - theme.neo-ansi - settings - - - settings - - background - #00000001 - foreground - #00000001 - gutter - #00000001 - gutterForeground - #00000001 - - - - name - Invalid - scope - invalid, invalid.illegal - settings - - foreground - #09000000 - - - - name - Invalid deprecated - scope - invalid.deprecated - settings - - foreground - #0b000000 - - - - name - URL - scope - *url*, *link*, *uri* - settings - - fontStyle - underline - - - - name - Storage operators - scope - storage.modifier.pointer, storage.modifier.reference - settings - - foreground - #0c000000 - - - - name - Pointer access - scope - punctuation.separator.pointer-access - settings - - foreground - #0c000000 - - - - name - CSS Class and Support - scope - source.css support.type.property-name, source.sass support.type.property-name, source.scss support.type.property-name, source.less support.type.property-name, source.stylus support.type.property-name, source.postcss support.type.property-name - settings - - foreground - #06000000 - - - - name - CSS Classes - scope - entity.other.attribute-name.pseudo-class, entity.other.attribute-name.pseudo-element - settings - - foreground - #0b000000 - - - - name - CSS Variables - scope - source.css variable - settings - - foreground - #0f000000 - - - - name - CSS Properties - scope - source.css support.type.property-name - settings - - foreground - #07000000 - - - - name - COMMENT - scope - comment, punctuation.definition.comment, string.quoted.docstring - settings - - fontStyle - italic - foreground - #00000000 - - - - name - VALUE TYPE, BUILTIN TYPE, TAG - scope - keyword.type, entity.name.tag, meta.tag.sgml, entity.name.type.struct, support.type, support.class, support.type.sys-types, support.type.built-in - settings - - foreground - #01000000 - - - - name - STRING, SYMBOL - scope - string, constant.other.symbol - settings - - foreground - #02000000 - - - - name - TYPE, ATTRIBUTE - scope - entity.name, entity.other.attribute-name - settings - - foreground - #03000000 - - - - name - FUNCTION, METHOD, OPERATOR - scope - entity.name.function, variable.function - settings - - foreground - #04000000 - - - - name - KEYWORD, STORAGE - scope - keyword, storage - settings - - fontStyle - - foreground - #05000000 - - - - name - SUPPORT, SPECIAL, BUILTIN OTHER - scope - support, keyword.other.special-method, variable.language, string.regexp, constant.character.escape, support.other.variable - settings - - foreground - #06000000 - - - - name - TEXT - scope - source, text, variable, entity.name.variable, entity.name.namespace, constant.other.placeholder - settings - - foreground - #07000000 - - - - name - CONSTANT - scope - constant.numeric, constant.language, support.constant, constant.character, constant.escape, keyword.other.unit - settings - - foreground - #09000000 - - - - name - STRING LINK - scope - string.other.link - settings - - foreground - #0a000000 - - - - name - ALT TYPE - scope - entity.name.type.parameter, entity.name.type.type-parameter, variable.other.generic-type - settings - - foreground - #0b000000 - - - - name - ALT FUNCTION - scope - keyword.operator, support.function - settings - - foreground - #0c000000 - - - - name - ALT KEYWORD - scope - keyword.other - settings - - foreground - #0d000000 - - - - name - PUNCTUATION - scope - punctuation.definition.tag, punctuation.definition, punctuation, punctuation.definition.tag.html, punctuation.definition.tag.begin.html, punctuation.definition.tag.end.html, punctuation.section.embedded, keyword.other.template, keyword.other.substitution, meta.separator - settings - - foreground - #0e000000 - - - - name - ALT VARIABLE - scope - entity.name.variable.field, entity.name.variable.property, variable.other.property, variable.other.field - settings - - foreground - #0f000000 - - - - name - Inserted - scope - markup.inserted - settings - - foreground - #02000000 - - - - name - Deleted - scope - markup.deleted - settings - - foreground - #01000000 - - - - name - Changed - scope - markup.changed - settings - - foreground - #03000000 - - - - name - GitGutter deleted - scope - markup.deleted.git_gutter - settings - - foreground - #01000000 - - - - name - GitGutter inserted - scope - markup.inserted.git_gutter - settings - - foreground - #02000000 - - - - name - GitGutter changed - scope - markup.changed.git_gutter - settings - - foreground - #03000000 - - - - name - GitGutter untracked - scope - markup.untracked.git_gutter - settings - - foreground - #09000000 - - - - name - GitGutter ignored - scope - markup.ignored.git_gutter - settings - - foreground - #08000000 - - - - name - Values - scope - constant.other.haskell - settings - - foreground - #01000000 - - - - name - Types - scope - storage.type.haskell - settings - - fontStyle - - foreground - #03000000 - - - - name - Storage - scope - source.js storage, source.js keyword.declaration - settings - - foreground - #03000000 - - - - name - String, Symbols - scope - constant.other.key meta.group.braces.curly constant.other.object.key.js string.unquoted.label.js - settings - - fontStyle - - foreground - #02000000 - - - - name - JS import - scope - entity.name.module.js, variable.import.parameter.js, variable.other.class.js - settings - - foreground - #06000000 - - - - name - Decorators - scope - tag.decorator.js entity.name.tag.js, tag.decorator.js punctuation.definition.tag.js - settings - - fontStyle - italic - foreground - #06000000 - - - - name - JSON Key - Level 0 - scope - source.json meta.structure.dictionary.json support.type.property-name.json - settings - - foreground - #01000000 - - - - name - JSON Key - Level 1 - scope - source.json meta.structure.dictionary.json meta.structure.dictionary.value.json meta.structure.dictionary.json support.type.property-name.json - settings - - foreground - #03000000 - - - - name - JSON Key - Level 2 - scope - source.json meta.structure.dictionary.json meta.structure.dictionary.value.json meta.structure.dictionary.json meta.structure.dictionary.value.json meta.structure.dictionary.json support.type.property-name.json - settings - - foreground - #0b000000 - - - - name - JSON Key - Level 3 - scope - source.json meta.structure.dictionary.json meta.structure.dictionary.value.json meta.structure.dictionary.json meta.structure.dictionary.value.json meta.structure.dictionary.json meta.structure.dictionary.value.json meta.structure.dictionary.json support.type.property-name.json - settings - - foreground - #02000000 - - - - name - JSON Key - Level 4 - scope - source.json meta.structure.dictionary.json meta.structure.dictionary.value.json meta.structure.dictionary.json meta.structure.dictionary.value.json meta.structure.dictionary.json meta.structure.dictionary.value.json meta.structure.dictionary.json meta.structure.dictionary.value.json meta.structure.dictionary.json support.type.property-name.json - settings - - foreground - #04000000 - - - - name - JSON Key - Level 5 - scope - source.json meta.structure.dictionary.json meta.structure.dictionary.value.json meta.structure.dictionary.json meta.structure.dictionary.value.json meta.structure.dictionary.json meta.structure.dictionary.value.json meta.structure.dictionary.json meta.structure.dictionary.value.json meta.structure.dictionary.json meta.structure.dictionary.value.json meta.structure.dictionary.json support.type.property-name.json - settings - - foreground - #06000000 - - - - name - JSON Key - Level 6 - scope - source.json meta.structure.dictionary.json meta.structure.dictionary.value.json meta.structure.dictionary.json meta.structure.dictionary.value.json meta.structure.dictionary.json meta.structure.dictionary.value.json meta.structure.dictionary.json meta.structure.dictionary.value.json meta.structure.dictionary.json meta.structure.dictionary.value.json meta.structure.dictionary.json meta.structure.dictionary.value.json meta.structure.dictionary.json support.type.property-name.json - settings - - foreground - #05000000 - - - - name - JSON Key - Level 7 - scope - source.json meta.structure.dictionary.json meta.structure.dictionary.value.json meta.structure.dictionary.json meta.structure.dictionary.value.json meta.structure.dictionary.json meta.structure.dictionary.value.json meta.structure.dictionary.json meta.structure.dictionary.value.json meta.structure.dictionary.json meta.structure.dictionary.value.json meta.structure.dictionary.json meta.structure.dictionary.value.json meta.structure.dictionary.json meta.structure.dictionary.value.json meta.structure.dictionary.json support.type.property-name.json - settings - - foreground - #07000000 - - - - name - JSON Key - Level 8 - scope - source.json meta.structure.dictionary.json meta.structure.dictionary.value.json meta.structure.dictionary.json meta.structure.dictionary.value.json meta.structure.dictionary.json meta.structure.dictionary.value.json meta.structure.dictionary.json meta.structure.dictionary.value.json meta.structure.dictionary.json meta.structure.dictionary.value.json meta.structure.dictionary.json meta.structure.dictionary.value.json meta.structure.dictionary.json meta.structure.dictionary.value.json meta.structure.dictionary.json meta.structure.dictionary.value.json meta.structure.dictionary.json support.type.property-name.json - settings - - foreground - #0f000000 - - - - name - Markup - Bold - scope - markup.bold, markup.bold string - settings - - fontStyle - - foreground - #01000000 - - - - name - Markup - Italic - scope - markup.italic - settings - - fontStyle - italic - foreground - #05000000 - - - - name - Markup - Underline and Link - scope - markup.underline - settings - - fontStyle - underline - foreground - #02000000 - - - - name - Markup - Strike - scope - markup.strike - settings - - fontStyle - strike - foreground - #08000000 - - - - name - Markup - Bold-Italic - scope - markup.bold markup.italic, markup.italic markup.bold, markup.quote markup.bold, markup.bold markup.italic string, markup.italic markup.bold string, markup.quote markup.bold string - settings - - fontStyle - - foreground - #09000000 - - - - name - Markdown - Heading - scope - markdown.heading, markup.heading, markup.heading entity.name, markup.heading.markdown punctuation.definition.heading.markdown, markup.heading.setext - settings - - fontStyle - - foreground - #01000000 - - - - name - Markdown - Link Anchor and Title - scope - constant.other.reference.link.markdown, string.other.link.title.markdown - settings - - foreground - #03000000 - - - - name - Markup - Raw & Fenced Block - scope - markup.raw, markup.fenced_code, markup.inline.raw - settings - - foreground - #04000000 - - - - name - Markdown - Link Description - scope - string.other.link.description.title.markdown - settings - - foreground - #05000000 - - - - name - Markup - Table - scope - markup.table - settings - - foreground - #06000000 - - - - name - Markup - Quote - scope - markup.quote - settings - - fontStyle - italic - foreground - #06000000 - - - - name - Markdown - Plain - scope - text.html.markdown, punctuation.definition.list_item.markdown - settings - - foreground - #07000000 - - - - name - Markdown - Fenced Bode Block Variable - scope - variable.language.fenced.markdown - settings - - foreground - #0f000000 - - - - name - Types - scope - source.purescript entity.name.type - settings - - foreground - #03000000 - - - - name - Classes - scope - source.purescript entity.name.class - settings - - foreground - #0b000000 - - - - name - String types - scope - storage.type.string.python - settings - - foreground - #0e000000 - - - - name - Lifetime types - scope - source.rust entity.name.type.lifetime, source.rust punctuation.definition.lifetime - settings - - foreground - #0b000000 - - - - uuid - uuid - - diff --git a/yazi-plugin/Cargo.toml b/yazi-plugin/Cargo.toml index 5ab894be..e777ba3e 100644 --- a/yazi-plugin/Cargo.toml +++ b/yazi-plugin/Cargo.toml @@ -30,4 +30,4 @@ tokio = { version = "^1", features = [ "parking_lot", "rt-multi-thread" tokio-util = "^0" tracing = { version = "^0", features = [ "max_level_debug", "release_max_level_warn" ] } unicode-width = "^0" -yazi-prebuild = "^0" +yazi-prebuild = "0.1.2" diff --git a/yazi-plugin/src/external/highlighter.rs b/yazi-plugin/src/external/highlighter.rs index 74d68aee..1fc52154 100644 --- a/yazi-plugin/src/external/highlighter.rs +++ b/yazi-plugin/src/external/highlighter.rs @@ -1,8 +1,8 @@ -use std::{mem, path::{Path, PathBuf}, sync::{atomic::{AtomicUsize, Ordering}, OnceLock}}; +use std::{io::Cursor, mem, path::{Path, PathBuf}, sync::{atomic::{AtomicUsize, Ordering}, OnceLock}}; use anyhow::{anyhow, Result}; use ratatui::text::{Line, Span, Text}; -use syntect::{dumps::from_uncompressed_data, easy::HighlightLines, highlighting::{self, Theme, ThemeSet}, parsing::{SyntaxReference, SyntaxSet}}; +use syntect::{dumps, easy::HighlightLines, highlighting::{self, Theme, ThemeSet}, parsing::{SyntaxReference, SyntaxSet}}; use tokio::{fs::File, io::{AsyncBufReadExt, BufReader}}; use yazi_config::{PREVIEW, THEME}; use yazi_shared::PeekError; @@ -26,18 +26,14 @@ impl Highlighter { Ok(ThemeSet::load_from_reader(&mut std::io::BufReader::new(file))?) } - #[inline] - fn load_default_theme() -> Theme { - let default_theme = include_str!("../../../yazi-config/preset/ansi.tmTheme"); - let mut theme_cursor = std::io::Cursor::new(default_theme.as_bytes()); + let theme = SYNTECT_THEME.get_or_init(|| { + from_file().unwrap_or_else(|_| { + ThemeSet::load_from_reader(&mut Cursor::new(yazi_prebuild::ansi_theme())).unwrap() + }) + }); - ThemeSet::load_from_reader(&mut theme_cursor).expect("Failed to load default ANSI theme") - } - - let theme = - SYNTECT_THEME.get_or_init(|| from_file().unwrap_or_else(|_err| load_default_theme())); - let syntaxes = - SYNTECT_SYNTAX.get_or_init(|| from_uncompressed_data(yazi_prebuild::syntaxes()).unwrap()); + let syntaxes = SYNTECT_SYNTAX + .get_or_init(|| dumps::from_uncompressed_data(yazi_prebuild::syntaxes()).unwrap()); (theme, syntaxes) } @@ -139,7 +135,7 @@ impl Highlighter { } impl Highlighter { - // https://github.com/sharkdp/bat/blob/master/src/terminal.rs + // Copy from https://github.com/sharkdp/bat/blob/master/src/terminal.rs fn to_ansi_color(color: highlighting::Color) -> Option { if color.a == 0 { // Themes can specify one of the user-configurable terminal colors by @@ -198,11 +194,10 @@ impl Highlighter { Span { content: s.replace('\t', &indent).into(), style: ratatui::style::Style { - fg: Self::to_ansi_color(style.foreground), - bg: Self::to_ansi_color(style.background), - underline_color: Default::default(), - add_modifier: modifier, - sub_modifier: Default::default(), + fg: Self::to_ansi_color(style.foreground), + // bg: Self::to_ansi_color(style.background), + add_modifier: modifier, + ..Default::default() }, } })