Skip to content

Instantly share code, notes, and snippets.

@Schniz
Created June 9, 2025 07:15
Show Gist options
  • Save Schniz/f397060342fbc26727d97430a8fc2007 to your computer and use it in GitHub Desktop.
Save Schniz/f397060342fbc26727d97430a8fc2007 to your computer and use it in GitHub Desktop.
use bat's --highlight-line feature, but dim rest instead of highlighting selected

use bat's --highlight-line feature, but dim rest instead of highlighting selected

example:

# dim and remove colors from unhighlighted lines
bat f.ts -H 42 | ./highlight-dim.ts

# dim colors from unhighlighted lines
bat f.ts -H 42 | ./highlight-dim.ts --keep-colors
#!/usr/bin/env bun
/// <reference lib="esnext" />
export {};
import stripAnsi from "strip-ansi";
const text = await Bun.stdin.text();
const keepColors = Bun.argv.includes("--keep-colors");
let lines = text.split("\n");
lines = lines.map((line) => {
if (line.includes("\u001b[48;5;8;37m")) {
return line.replaceAll("[48;5;8;", "[").replaceAll("\u001b[48;5;8m", "");
}
const indexOfPipe = line.indexOf("│");
if (indexOfPipe < 0) return line;
if (!/\d/.test(stripAnsi(line.slice(0, indexOfPipe)))) {
return line;
}
const dimmed = keepColors
? line.slice(indexOfPipe + 1).replaceAll("\u001b", "\u001b[2m\u001b")
: "\u001b[2m" + stripAnsi(line.slice(indexOfPipe + 1));
return line.slice(0, indexOfPipe + 1) + dimmed;
});
console.log(lines.join("\n"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment