Skip to content

Implement Syntax Highlighting for --inspect#131

Closed
nouritsu wants to merge 2 commits into
MilesCranmer:masterfrom
nouritsu:inspect-syntax-highlight
Closed

Implement Syntax Highlighting for --inspect#131
nouritsu wants to merge 2 commits into
MilesCranmer:masterfrom
nouritsu:inspect-syntax-highlight

Conversation

@nouritsu

@nouritsu nouritsu commented Dec 9, 2025

Copy link
Copy Markdown

Implemented syntax highlighting for the --inspect flag, fix #130

This PR will change the behavior of the --inspect flag. In addition to printing a preview, it will now also syntax highlight the preview using syntect. This crate is used by several projects like bat, delta, yazi and ...many more.

Here is a showcase of the change
image
image
image

Here, with varying themes (tested on windows)
image
image

All tests pass on my machine running NixOS on WSL.
image

It is to note that tests do NOT pass on Windows, even on master:

failures:

---- test_filetypes::file_type_4___symlink__::copy_2_true stdout ----

thread 'test_filetypes::file_type_4___symlink__::copy_2_true' (6476) panicked at tests\unit_tests.rs:93:49:
called `Result::unwrap()` on an `Err` value: Os { code: 1314, kind: Uncategorized, message: "A required privilege is not held by the client." }

---- test_filetypes::file_type_4___symlink__::copy_1_false stdout ----

thread 'test_filetypes::file_type_4___symlink__::copy_1_false' (18396) panicked at tests\unit_tests.rs:93:49:
called `Result::unwrap()` on an `Err` value: Os { code: 1314, kind: Uncategorized, message: "A required privilege is not held by the client." }
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    test_filetypes::file_type_4___symlink__::copy_1_false
    test_filetypes::file_type_4___symlink__::copy_2_true

test result: FAILED. 29 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.01s

@MilesCranmer

Copy link
Copy Markdown
Owner

Cool. What is the change in the binary size? And compilation time? Please measure before/after for a release target

@nouritsu

nouritsu commented Dec 9, 2025

Copy link
Copy Markdown
Author

Binary Size: 1.8x

  File: rip-inspect-syntax-highlight
  Size: 3258840         Blocks: 6368       IO Block: 4096   regular file
Device: 830h/2096d      Inode: 424019      Links: 1
Access: (0755/-rwxr-xr-x)  Uid: ( 1000/  aneesh)   Gid: (  100/   users)
Access: 2025-12-09 00:48:32.923172059 +0000
Modify: 2025-12-09 00:48:23.864291796 +0000
Change: 2025-12-09 00:48:32.927171724 +0000
 Birth: 2025-12-09 00:48:32.923172059 +0000
  File: rip-main
  Size: 1771720         Blocks: 3464       IO Block: 4096   regular file
Device: 830h/2096d      Inode: 418264      Links: 1
Access: (0755/-rwxr-xr-x)  Uid: ( 1000/  aneesh)   Gid: (  100/   users)
Access: 2025-12-09 00:48:12.395075211 +0000
Modify: 2025-12-09 00:48:01.682988920 +0000
Change: 2025-12-09 00:48:12.399075241 +0000
 Birth: 2025-12-09 00:48:12.395075211 +0000

stat rip-inspect-syntax-highlight rip-main # compiled from named branches

Compile Time: 2.3x D:

-------------------------------------- master --------------------------------------
Benchmark 1: cargo b --release
  Time (mean ± σ):      9.639 s ±  0.611 s    [User: 54.141 s, System: 9.098 s]
  Range (min … max):    8.829 s … 10.723 s    10 runs
------------------------------ inspect-syntax-highlight ----------------------------
Benchmark 1: cargo b --release
  Time (mean ± σ):     23.029 s ±  1.516 s    [User: 154.835 s, System: 26.878 s]
  Range (min … max):   21.433 s … 25.659 s    10 runs
------------------------------------------------------------------------------------

hyperfine --prepare "cargo clean --release" "cargo b --release" --runs 10
Executed on each branch

Execution time: 4.3x 😔

Benchmark 1: ./rip-main --inspect example.c
  Time (mean ± σ):       1.5 ms ±   0.2 ms    [User: 0.0 ms, System: 0.1 ms]
  Range (min … max):     1.3 ms …   1.9 ms    10 runs

Benchmark 2: ./rip-inspect-syntax-highlight --inspect example.c
  Time (mean ± σ):       6.6 ms ±   0.3 ms    [User: 2.7 ms, System: 2.2 ms]
  Range (min … max):     6.1 ms …   7.0 ms    10 runs

Summary
  ./rip-main --inspect example.c ran
    4.33 ± 0.49 times faster than ./rip-inspect-syntax-highlight --inspect example.c

hyperfine --warmup 5 --runs 10 "./rip-main --inspect example.c" "./rip-inspect-syntax-highlight --inspect example.c" --shell=none

This seems like a terrible change haha. I'm brainstorming on possible solutions.

@nouritsu

nouritsu commented Dec 9, 2025

Copy link
Copy Markdown
Author

Perhaps the highlighting could be locked behind a flag to ensure --inspect's performance is not hindered? As for the binary size, I am looking into somehow translating the theme file into an internal representation at compile time.

@nouritsu

nouritsu commented Dec 9, 2025

Copy link
Copy Markdown
Author

Upon benchmarking it seems the latest commit has not helped with the previously found issues. I think I will have this feature on my fork (for my NixOS config). If something comes to mind, I shall implement and test.

@MilesCranmer

Copy link
Copy Markdown
Owner

Thanks for checking. I think it's probably not worth it then. If there's a flag for it, it's probably easier to just run bat on the file and then call rip normally.

@nouritsu nouritsu closed this Dec 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: Syntax Highlighting in --inspect Previews

2 participants