Loading

Paste #pvdqh8gyr

  1. # cat >capture_tmux
  2.  
  3. #!/bin/sh
  4. #
  5. # Capture content of tmux session including terminal escapes and convert to html
  6. #
  7. # Uses ansi2html script which internally calls ansifilter, make sure to have that
  8. # installed.
  9.  
  10. if [[ -z "$1" ]]; then
  11.     if [[ -z "$TMUX" ]]; then
  12.         echo "Please specify tmux session to capture" >/dev/stderr
  13.         exit 1
  14.     else
  15.         S="${TMUX##*,}"
  16.         echo "Guessing session name to be $S" >/dev/stderr
  17.     fi
  18. else
  19.     S="$1"
  20. fi
  21.  
  22. SIZE="$( tmux list-sessions | \
  23.         sed -n "/$S: /"'{s/.*\[\([0-9]\+\)x\([0-9]\+\)\].*/-x \1 -y \2/;p}'
  24.       )"
  25.  
  26. TMP="$( mktemp --directory )"
  27. mkfifo $TMP/fifo
  28.  
  29. tmux new-session -d $SIZE "
  30.    tmux set-option status off
  31.    env TMUX= TERM=xterm-256color \
  32.    script -q -c 'tmux attach -t $S \; detach' $TMP/fifo"
  33.  
  34. ansi2html <$TMP/fifo
  35. rm $TMP/fifo
  36. rmdir $TMP
  37.  
  38.  
  39. # cat >ansi2html
  40.  
  41. #!/bin/sh
  42. #
  43. # Convert captured output from tmux to html
  44. #
  45. # capture output in terminal of same size using:
  46. # TERM=xterm-256color script -q -c 'tmux attach -t 0 \; detach' outputfile
  47. # this can be done using detached tmux session with specified size
  48. sed '
  49. ## strip everyting before jump to first column (titlebar escapes etc.)
  50. s|^.*\x1b\[1;1H||;
  51.  
  52. ## convert cursor jumps into newlines (happen at fully filled lines)
  53. s|\x1b\[[0-9]\+;[0-9]\+H|\n|g;
  54. ' | sed '
  55. ## some stuff left at the end (cursor position etc.)
  56. /\x1b\[[0-9]\+;[0-9]\+r/,$d;
  57. ' | ansifilter -e utf8 -H | \
  58. sed 's/<body>/<body style="color: silver; background-color:black;">/'
  59.