Skip to content

A visual git client, simple to use yet powerful. Light, fast and debloated.

Rust

feat: colour TypeScript, Vue, Dockerfile and more in diffs

Jonas Kamsker39422dbf5fa7
diff --git a/Cargo.lock b/Cargo.lock
index ef9dc61..280efe8 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1463,6 +1463,7 @@ dependencies = [
"serde",
"serde_json",
"syntect",
+ "two-face",
"ureq",
"vt100",
"winresource",
@@ -3970,6 +3971,17 @@ dependencies = [
"once_cell",
]
+[[package]]
+name = "two-face"
+version = "0.5.2+bat-0.26.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "915be7adc2ff6f4338acbf71f3eda0a146f46003b992a1669c674308610efdac"
+dependencies = [
+ "serde",
+ "serde_derive",
+ "syntect",
+]
+
[[package]]
name = "type-map"
version = "0.5.1"
diff --git a/Cargo.toml b/Cargo.toml
index 6c1581b..e73617a 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -23,6 +23,8 @@ rfd = "0.17"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
syntect = { version = "5", default-features = false, features = ["default-fancy"] }
+# bat's syntax set: TypeScript, Vue, TOML, Dockerfile, PowerShell… beyond syntect's own.
+two-face = { version = "0.5.2", default-features = false, features = ["syntect-default-fancy"] }
ureq = "2"
vt100 = "0.18"
diff --git a/README.md b/README.md
index b7c79de..e1ea275 100644
--- a/README.md
+++ b/README.md
@@ -76,8 +76,9 @@ so your config, credentials and hooks apply as usual.
**Diffs and conflicts** (centre)
-- Unified or side by side, syntax colours, the changed part of each line marked, whitespace
- changes optional, images shown before / after. Esc closes.
+- Unified or side by side, syntax colours (bat's languages: TypeScript, Vue, TOML, Dockerfile…),
+ the changed part of each line marked, whitespace changes optional, images shown before / after.
+ Esc closes.
- Code takes the colours of the theme VS Code (or Insiders, VSCodium) is using, read from its
settings and extensions, when it is dark or light like gitcraft's; a setting turns it off.
- Conflicts open in a three-pane editor: each conflict's two sides with _Ours_, _Theirs_ and
@@ -185,7 +186,7 @@ was made this way, on a demo repository.
| `src/repo/graph_view.rs` | commit graph, pills, search bar, commit menu |
| `src/repo/detail.rs` | commit / stash detail, changes and commit box |
| `src/repo/diff.rs` | diff loading, pairing and the diff view (unified, split, images) |
-| `src/repo/highlight.rs` | syntax colours (syntect) |
+| `src/repo/highlight.rs` | syntax colours (syntect, with bat's syntaxes from `two-face`) |
| `src/vscode_theme.rs` | VS Code's current colour theme as a syntect theme |
| `src/repo/conflict.rs` | three-pane conflict editor |
| `src/repo/rebase.rs` | interactive rebase editor and its todo list |
diff --git a/src/repo/highlight.rs b/src/repo/highlight.rs
index fa3bbcc..a753b69 100644
--- a/src/repo/highlight.rs
+++ b/src/repo/highlight.rs
@@ -1,4 +1,5 @@
-//! Syntax colouring for diffs (syntect, pure-Rust regex engine), chosen by file extension.
+//! Syntax colouring for diffs (syntect with bat's syntaxes, pure-Rust regex engine), chosen by
+//! file extension or name.
use std::sync::OnceLock;
@@ -15,7 +16,7 @@ const MAX_LINES: usize = 8000;
fn syntaxes() -> &'static SyntaxSet {
static SET: OnceLock<SyntaxSet> = OnceLock::new();
- SET.get_or_init(SyntaxSet::load_defaults_newlines)
+ SET.get_or_init(two_face::syntax::extra_newlines)
}
fn themes() -> &'static ThemeSet {
@@ -34,10 +35,13 @@ pub fn highlight<'a>(
return None;
}
let ss = syntaxes();
- let ext = std::path::Path::new(path)
+ let file = std::path::Path::new(path);
+ let name = file.file_name()?.to_string_lossy();
+ // By extension, or by the whole name for files like `Dockerfile` and `Makefile`.
+ let syntax = file
.extension()
- .map(|e| e.to_string_lossy().to_lowercase())?;
- let syntax = ss.find_syntax_by_extension(&ext)?;
+ .and_then(|e| ss.find_syntax_by_extension(&e.to_string_lossy().to_lowercase()))
+ .or_else(|| ss.find_syntax_by_extension(&name))?;
if syntax.name == "Plain Text" {
return None;
}
@@ -93,4 +97,35 @@ mod tests {
assert!(highlight("notes.txt", lines.iter().copied(), true).is_none());
assert!(highlight("Makefile-without-ext", lines.iter().copied(), true).is_none());
}
+
+ /// The languages of a typical web or .NET repository each get more than one colour.
+ #[test]
+ fn colours_common_languages() {
+ let cases: &[(&str, &[&str])] = &[
+ ("Program.cs", &["public class A { private int x = 1; }"]),
+ ("app.js", &["const x = require('y');"]),
+ ("app.ts", &["const x: number = 1;"]),
+ ("App.tsx", &["export const A = (): JSX.Element => <div />;"]),
+ (
+ "Details.vue",
+ &[
+ "<script setup lang=\"ts\">",
+ "import { ref } from 'vue'",
+ "</script>",
+ ],
+ ),
+ ("Cargo.toml", &["[package]", "name = \"x\""]),
+ ("Dockerfile", &["FROM alpine:3", "RUN echo hi"]),
+ ("styles.scss", &[".a { color: red; }"]),
+ ];
+ let plain: Vec<&str> = cases
+ .iter()
+ .filter(|(path, lines)| {
+ !highlight(path, lines.iter().copied(), true)
+ .is_some_and(|spans| spans.iter().any(|line| line.len() > 1))
+ })
+ .map(|(path, _)| *path)
+ .collect();
+ assert!(plain.is_empty(), "not coloured: {plain:?}");
+ }
}