Advertisement
devinteske

ansi2escp2.sh

Nov 6th, 2023
788
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.48 KB | None | 0 0
  1. #!/bin/sh
  2. exec 9<<-'EOF'
  3.     # Convert compound ANSI to simple ANSI
  4.     : 9
  5.     s/\(\x1b\[\)\([^;]*\);/\1\2m\1/g
  6.     t 9
  7.  
  8.     # Remove leading/implied zeroes
  9.     s/\(\x1b\[\)0\([^m]\{0,\}m\)/\1\2/g
  10.  
  11.     # Translate ANSI to ESC-P2
  12.     s/\[1m/P/g
  13.     s/\[3m/4/g
  14.     s/\[4m/-1/g
  15.     s/\(\x1b\)\[m/\1F\15\1-0\1r\x0f\1r\x10/g
  16.  
  17.     # Strip remaining unconverted ANSI
  18.     s/\x1b\[[0-9]\{1,\}m//g
  19. EOF
  20. ANSI2ESCP2=$( cat <&9 )
  21. if [ -t 0 -a $# -lt 1 ]; then
  22.     script -aqt0 /dev/null
  23. else
  24.     cat "$@"
  25. fi | sed -e "$ANSI2ESCP2"
Tags: ansi sed
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement