简体   繁体   中英

terminal escape sequences for fonts

What I want to develop : Terminal which can use at least 2 fonts in the same time. One font I will use for shell input lines, another font for command output. For example:

user@host$ ls /home
user user1 user2 user3

Why : More readable terminal/shell

How : Here I have problem. Probably shell needs to generate some new escape sequences. And terminal need to load different fonts and handle those sequences. Where to start? How to define new escaping sequence, where are standards?

Future : Maybe somebody want to join me in this project?

The standard for control sequences is pretty much the Xterm Control Sequences document ctlseqs.ms in the XTerm source code . (You can turn it into a PDF with the command groff -ms -Tps ctlseqs.ms | ps2pdf - ctlseqs.pdf , though the -ms option seems to be broken on Ubuntu 12.04).

XTerm already supports control sequences to change the font, but for the entire terminal at once. Open xterm and type into your shell—

echo -e "\033[?35h\033]50;#+1^G\033\\" # aka CSI ? 35 h OSC 50 ; #+1 BEL ST

the font for the entire terminal should change. This control sequence actually supports the names of True-Type fonts as well; see page 21.

If you'd like to change an existing terminal to support changing the font inline, you're welcome to choose pretty much any control sequences not already allocated in ctrlseqs.ms and use them. However, it's a good idea to choose new control sequences similar to the control sequences for functionality that already exists.

Your next step is to get the source code for an existing terminal and start digging. What terminal do you use right now? The source code for Konsole or gnome-terminal is probably going to be easier to work with than that for XTerm.

There is a standard sequence for swapping fonts.

SGR 11

Also known as

CSI 11m
ESC [ 11m

Similarly

SGR 10

will switch back to the default font.

However, as has been commented, almost no terminal actually supports this. You'd likely be better off using some other rendering attribute, such as bold/underline/italics. Though note also not many terminals support italics.

For reference

SGR 1   = bold
SGR 4   = underline
SGR 3   = italics

If you are happy with just different font attributes (and not different fonts) you can even implement something similar without writing your own terminal emulator if you use zsh. You can just set up your shell to emit the right escape sequences to set the correct terminal attribute (italics, bold, color, ...) before and after the prompt and before command execution.

Let's assume you use Xterm and want your prompt to be bold, the typed command line to be italics and the command output to be normal.

Then the setup looks like this:

# bold is \e[1m and italics is \e[3m , \e[0m resets the attributes
PS1=$'\e[1m'$PS1$'\e[0;3m' # I assume you have set PS1 already
function reset-terminal-attributes { printf '\e[0m'; }
autoload add-zsh-hook
add-zsh-hook preexec reset-terminal-attributes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM