Skip to content

Instantly share code, notes, and snippets.

@rhaberkorn
Last active January 28, 2024 14:26
Show Gist options
  • Save rhaberkorn/6534ecf1b05de6216d0a9c33f31ab5f8 to your computer and use it in GitHub Desktop.
Save rhaberkorn/6534ecf1b05de6216d0a9c33f31ab5f8 to your computer and use it in GitHub Desktop.
Pretty-print TECO macros
#!/usr/local/bin/lua52
-- Replace all control characters with printable representations as in SciTECO.
-- These characters are printed in reverse using ANSI escape sequences.
-- This is especially useful as the diff textconv filter for Git as in
-- git config --global diff.teco.textconv tecat
local file = #arg > 0 and io.open(arg[1], 'rb') or io.stdin
while true do
local buf = file:read(1024)
if not buf then break end
io.write(buf:gsub("[\00-\08\11\12\14-\31]", function(c)
c = c:byte()
return "\27[7m"..(c == 27 and '$' or '^'..string.char(c+0x40)).."\27[27m"
end))
end
#!/bin/sh
# FIXME: POSIX sed cannot replace ASCII 0 (^@).
for c in `seq 1 8` 11 12 `seq 14 26` `seq 28 31`; do
patterns="$patterns`printf 's/\\\\x%x/\\\\x1B[7m^\\\\x%x\\\\x1B[27m/g;' $c $(($c|0x40))`"
done
exec sed 's/\x1B/\x1B[7m$\x1B[27m/g;'$patterns "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment