How can I create simple text-based graphics in bas

2019-08-29 02:33发布

问题:

Example:

Is it possible to create such lines and the two little things as the image shows? If so, how can I do this?

回答1:

Yes, it's possible to build an image, with use of external tools (bash has no built-in graphics APIs). As examples, you could:

  • Use XMLStarlet to build a SVG file describing the graphic you want, and invoke Inkscape in command-line mode to render it to a file (.png, etc).
  • Use GraphicsMagick in command-line mode (with the -draw argument to the convert command).

If, by contrast, you want to render that image to the terminal, you can use your terminal font's available line-drawing characters and color support, but will not have the same level of detailed control (ability to select fonts, select pixel-specific locations, etc).



回答2:

Look at the list of characters in http://en.wikipedia.org/wiki/Box-drawing_character

If your user supports a Unicode-aware terminal, then, you could do something like:

printf '\u2500\u252c\u2500\nf%sO%so\n\u2500\u2534\u2500\n' \
  "$(tput setf red)" "$(tput setf black)"

The \u2500s get replaced with horizontal bars, the \u252c with a horizontal bar with a tee pointing down, the \u2534 with a horizontal bar with a tee pointing up, etc.

The %ss get replaced with corresponding arguments -- "$(tput setf red)" to make the foreground text red (or no character at all, if the current terminal doesn't support color), the "$(tput setf black)"... well, does the obvious thing.



标签: bash shell