| 1 |
#!/bin/bash |
| 2 |
set -euo pipefail |
| 3 |
. "$(dirname "$0")"/setup_standard_config.sh |
| 4 |
|
| 5 |
# NOTE ON TOOLS: the only two nice tools I'm aware of that support our use-case |
| 6 |
# (non-animated svgs) are https://github.com/slowli/term-transcript and |
| 7 |
# https://github.com/FHPythonUtils/AnsiToImg. They have slightly different |
| 8 |
# limitations and advantages. If one of them stops being developed, we could |
| 9 |
# look at the other one. |
| 10 |
# |
| 11 |
# AnsiToImg can also generate PNGs, but that is currently harder to setup than |
| 12 |
# `magick`. `magick` supports different backends. I do not completely |
| 13 |
# understand them. On Debian, it did not work well without `sudo apt install |
| 14 |
# inkscape`. It's unclear to me whether `magick` used Inkscape or one of its |
| 15 |
# dependencies. Inkscape can be also used manually for SVG -> PNG conversion. |
| 16 |
which term-transcript > /dev/null \ |
| 17 |
|| (echo '`term-transcript` must be installed with e.g.'\ |
| 18 |
'`cargo binstall term-transcript-cli`.' \ |
| 19 |
'See also https://github.com/slowli/term-transcript' >&2; |
| 20 |
false) |
| 21 |
which magick > /dev/null \ |
| 22 |
|| echo '`magick` from ImageMagick needs to be installed to create pngs.' \ |
| 23 |
'Only svgs will be created.' >&2 |
| 24 |
|
| 25 |
echo "jj --version: (set PATH to change)" |
| 26 |
jj --version |
| 27 |
|
| 28 |
# Make `jj` wrap text as opposed to `term-transcript`. `term-transcript` wraps |
| 29 |
# at 80 columns. Also, 80 seems to be the maximum number of columns that's |
| 30 |
# somewhat readable on mobile devices. |
| 31 |
# |
| 32 |
# Note that `bash` likes to reset the value of $COLUMNS, so we use a different |
| 33 |
# variable here that is interpreted by `run_command()` in `helpers.sh`. |
| 34 |
RUN_COMMAND_COLUMNS=80 |
| 35 |
export RUN_COMMAND_COLUMNS |
| 36 |
|
| 37 |
run_script_through_term_transcript_and_pipe_result_to_stderr() { |
| 38 |
script="$1" |
| 39 |
script_base="${script%.sh}" |
| 40 |
script_base="${script_base#demo_}" |
| 41 |
outfile=$(mktemp --tmpdir "$script_base"-output-XXXX.ansi) |
| 42 |
# We use `term-transcript capture` instead of `term-transcript exec` so that |
| 43 |
# we can show the output of the script via `tee`. |
| 44 |
(bash "$script" || (echo "SCRIPT FAILED WITH EXIT CODE $?"; false)) 2>&1 | \ |
| 45 |
tee "$outfile" |
| 46 |
term-transcript capture \ |
| 47 |
--no-inputs --pure-svg --palette powershell \ |
| 48 |
--font "Fira Code, Liberation Mono, SFMono-Regular, Consolas, Menlo" \ |
| 49 |
--out "$script_base".svg "$script_base" < "$outfile" |
| 50 |
# The default font choice term-transcript would make is: |
| 51 |
# SFMono-Regular, Consolas, Liberation Mono, Menlo |
| 52 |
# We add the fonts that were checked and seem to contain all the relevant |
| 53 |
# unicode in front. |
| 54 |
rm "$outfile" |
| 55 |
} |
| 56 |
|
| 57 |
for script in "$@"; do |
| 58 |
run_script_through_term_transcript_and_pipe_result_to_stderr "$script" 2>&1 |
| 59 |
# By default, 1 SVG unit becomes 1 pixel. The term-transcript output |
| 60 |
# defaults to 720 SVG units width. |
| 61 |
# |
| 62 |
# `-background black` is important because the SVGs use transparency to make |
| 63 |
# rounded corners, and the transparent portion becomes white by default. |
| 64 |
# TODO(ilyagr): Figure out if `magick` can make PNGs have transparency. |
| 65 |
# |
| 66 |
# `-resize 100%` is a no-op. `-resize 700x10000`` would make the width 700 px |
| 67 |
# and preserve aspect ratio. |
| 68 |
# |
| 69 |
# (!) The order of operations is important and confusingly different from what |
| 70 |
# would happen to a PNG with transparency. If editing this, it might help to |
| 71 |
# look at https://github.com/ImageMagick/ImageMagick/discussions/7600 |
| 72 |
which magick > /dev/null \ |
| 73 |
&& magick -background black "$script_base".svg \ |
| 74 |
-type Palette -colors 63 -resize 100% \ |
| 75 |
"$script_base".png \ |
| 76 |
|| true |
| 77 |
done |