Unicode and 256 colors in Raspberry Pi Terminal Environment - linux

I'm working with a bunch of Raspberry Pi 2s running Raspbian, building interactive puzzles for a performance installation. The puzzles are meant to be played on the command line. My background is in web development, so while I'm very comfortable on the Linux command line, I don't have a lot of experience tailoring a local terminal environment. I have a number of distinct requirements and I can never quite fulfill all of them at once. Namely:
I would like the game to be loaded from the kernel console, without starting LXDE. This is mostly because in a terminal environment, I can catch any keystrokes that someone would use to try to exit the game, and put a password up to prevent it. Also, I have not yet determined whether I could start a terminal window in LXDE and hide all the windowing chrome to make the game appear to be CLI only.
I need 256 color display.
The game is played in English, but contains some Cyrillic characters, so I need unicode support. A sample of the characters used:
0x1440
0x1437
0x143d
I can get the 256 color display in xterm using TERM=xterm-256color, but ONLY if I launch it in a window in LXDE. Same goes for Unicode support. When I try starting xterm from the kernel console, I can pass a number of options to customize the display of the terminal (-fg, -bg, -fa), but can't make it display 256 colors or unicode characters. I've tried using a .Xresource file and loading it with xrdb -merge ~/.Xresource, but it doesn't seem to have any effect at all. Currently, I have a .xinitrc file that contains
`which xterm` \
-fg white \
-bg black \
-fa *-fixed-*-*-*-18-*
-en en_US.UTF-8
-tn xterm-256color
and I launch xterm by simply running xinit. When I do so, xterm starts and respects the foreground, background, and font properties, but only displays 8 colors and won't display unicode characters. Can anyone offer any insight for how to meet all three of my requirements above? I would also accept an answer that shows how I can simply start the game from an LXDE window, but full-screen that window with absolutely no chrome, and prevent the user from being able to exit that environement (there will not be a mouse attached to the machine)
Thanks!

It's a shame, someone posted an answer here and pointed out that I had a typo in the .xinitrc that I posted (missing \ characters after a couple of the lines). After going back and double checking, that did turn out to be part of the problem. He or she or someone else apparently took down the answer though, so I can't give credit.
The other part of the problem was that the unicode characters I'm trying to display are not Cyrillic, but are rather part of the Unified Canadian Aboriginal Syllabics block, which simply wasn't part of any of the terminal fonts I was using. After some digging, I found what appears to be the only monospaced font that contains these characters, Everson Mono. I installed that to /usr/local/share/fonts, and amended my .xinitrc file thusly:
`which xterm` \
-fg white \
-bg black \
-fa 'Everson Mono' \
-en en_US.UTF-8 \
-tn xterm-256color
...and now I can load xterm with xinit and run my app and all is right with the world.

Related

Python ANSI Color Codes

Python 3.7, on Windows print does not work as expected for ANSI color codes until shell=True once in subprocess.call().
In the below links it appears to imply that the ANSI color codes should work using the "print" command out of the box.
How to print colour/color in python?
Print in terminal with colors using Python?
the second one mentions VT100 emulation... not sure what exactly that means. I am able to write a batch file that outputs the color fine so I would think (naively) that it should work the same way in Python.
However I am not able to use the ANSI color codes as it seems that the ESC character is being "commented out"(?) because for instance when I
print(u"\u001b[31mHelloWorld")
I am not able to see the colored output, as the ESC character seems to be necessary in Windows and prints in the python shell as "[?]" (a box with a question mark)
Is there something I am missing here?
I found myself an answer. As often happens I just did not look far enough.
the Colorama module can be installed with
py -m pip install colorama
and comes with a method definition at the root of the module called init
colorama.init()
This is a cross platform function in that it is only useful on windows (it saves the active Terminal state for reversal and writes the Terminal to preprocess ANSI codes), it does nothing for other operating systems.
I am thinking about implementing an even more lightweight solution using ctypes and setting the Interpret flags on the active terminal myself.
If you are interested in more information, see here:
https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences
Output Sequences
The following terminal sequences are intercepted by the console host when written into the output stream, if the ENABLE_VIRTUAL_TERMINAL_PROCESSING flag is set on the screen buffer handle using the SetConsoleMode flag. Note that the DISABLE_NEWLINE_AUTO_RETURN flag may also be useful in emulating the cursor positioning and scrolling behavior of other terminal emulators in relation to characters written to the final column in any row.
Emphasis mine.

How can make screen sessions to use same settings as original terminal

When I run 'screen' command after logging into the linux terminal from putty,
I see font color of folders got changed.
earlier folders are shown with different color(blue)'
Now all files are in same color.
How can I make sure even screen sessions also use same font settings as original terminal?
There really isn't enough information to answer this question, but here's a couple things you could do to troubleshoot.
Make sure that the $SHELL in screen is the same as the $SHELL in the original terminal (Run echo $SHELL before and after starting screen and compare the results). Perhaps screen is running a different shell and thus not reading your preferred .*rc file.
Also, try starting screen with a specific TERM value. Set screen's term value to $TERM before when starting screen like so:
screen -T $TERM
I would also recommend trying to replace $TERM with xterm if $TERM doesn't solve it.

Display big numbers in terminal

tmux can display big time like the picture below,
is there any way to display big numbers like that,
for example I want to show number of users in real time.
To the extent of my knowledge, there is no such feature in tmux. You could use watch (part of the procps tools) and figlet to implement such a feature:
watch runs a command periodically and prints the output on the screen. It always clears the screen before running the command.
figlet renders a string in a ASCII-Art style font. There a several fonts availiable.
So for instance you could show the number of users on a system (at least on Linux) using the following command line:
watch "who | wc -l | figlet -f big"
Unfortunatly the text won't be centered, as it is with clock-mode.

Ubuntu terminal in windows ? Can anyone explain the 'col' command in plain English for me?

Next week I am getting an exam on using basic commands and shell scripting using terminal in Ubuntu. Please help me out with two quick questions:
-Does a practice environment for the Ubuntu terminal exist? I don't want to dual boot with Linux, so I want to find somewhere I could practice using some basic terminal commands as Cygwin seems to be quite different from the ubuntu terminal.
-What does the col command in Linux do? The manual page is here: Link, but I find that hard to understand, and since it doesn't work in Cygwin, it's hard to interpret! Also, why would one use man piped to col -b -x?
Thank you very much and sorry for the probably silly questions!
You can use the Ubuntu LiveCD boot (boots up an Ubuntu system without actually installing it) and experiment with the Ubuntu terminal shell. Its actually the same CD as the standard Ubuntu installtion CD... you just chose "Try Ubuntu" instead of "Install Ubuntu" once it boots up:
https://help.ubuntu.com/community/LiveCD
col is program to filter out reverse-linefeeds (i.e. the backspace character) from text input. In the olden days of line printers, a common method to achieve boldface print was to print a character, then print a backspace character, then print the character again. This would make the printer strike the character twice in the same place. Some programs would emit text files formatted in this way (man is one such program)-- but if you then tried to display that text file to your terminal screen, you might end up seeing something like this: "here is b^Hbo^Hol^Hld^Hd text".
col -b simply filters text input to strip out those extra backspace and double-strike characters. The -x option converts tab characters to space characters, which might be useful if the output was formatted for a device with a particular tab width, but then displayed on a different device.
man pages often have the backspace/double-strike text embedded in them, so man piped into col was often useful.
Nowadays, most terminal emulators actually know how to handle the backspace/double-strike, so col doesn't get used as much.
Far better (easier to use, install, maintain, etc.) than Cygwin, and perhaps less resource-using than a virtual machine, is http://andlinux.org . That will give you a shell on your Windows desktop, and you can play with the col command to better understand it.
Win-bash is essentially the same shell as linux, but on windows. You can use this to experiment outside Linux but I'm not sure how effective it is.
Other options are virtual machines, and if you don't mind a reboot, you can install ubuntu with wubi which means it can be deleted from windows' add/remove programs when you are done with it. (Or just use the live disc)
Col has very limited use for most people, it only affects programs that write lines asynchronously as opposed to line by line...

How can I change the color of my prompt in zsh (different from normal text)?

To recognize better the start and the end of output on a commandline, I want to change the color of my prompt, so that it is visibly different from the programs output. As I use zsh, can anyone give me a hint?
Put this in ~/.zshrc:
autoload -U colors && colors
PS1="%{$fg[red]%}%n%{$reset_color%}#%{$fg[blue]%}%m %{$fg[yellow]%}%~ %{$reset_color%}%% "
Supported Colors:
red, blue, green, cyan, yellow, magenta, black, & white (from this answer) although different computers may have different valid options.
Surround color codes (and any other non-printable chars) with %{....%}. This is for the text wrapping to work correctly.
Additionally, here is how you can get this to work with the directory-trimming from here.
PS1="%{$fg[red]%}%n%{$reset_color%}#%{$fg[blue]%}%m %{$fg[yellow]%}%(5~|%-1~/.../%3~|%4~) %{$reset_color%}%% "
Zsh comes with colored prompts builtin. Try
autoload -U promptinit && promptinit
and then prompt -l lists available prompts, -p fire previews the "fire" prompt, -s fire sets it.
When you are ready to add a prompt add something like this below the autoload line above:
prompt fade red
Here's an example of how to set a red prompt:
PS1=$'\e[0;31m$ \e[0m'
The magic is the \e[0;31m (turn on red foreground) and \e[0m (turn off character attributes). These are called escape sequences. Different escape sequences give you different results, from absolute cursor positioning, to color, to being able to change the title bar of your window, and so on.
For more on escape sequences, see the wikipedia entry on ANSI escape codes
The answer by Bryan Oakley above has a glitch as it has already been pointed out and the solution offered by Andrew Marshall though it does not carry the glitch, nevertheless it does not make it obvious for too much customization on the colors used.
As macOS Catalina asks for zsh to be the default shell from now on, I think several more people may want to customize their prompt and might be coming here for an answer. So, I thought I would try to give a broader summary and touch upon other very closely-related notions that allow more customization.
3-Digit Codes for Various Colors.
First of all, here we can find 3-digit codes for various colors: https://unix.stackexchange.com/a/124409/194343.
For example, 214 is some kind of orange color.
Foreground and Background.
The other key information is that for Foreground and bacKground colors one can define what they want with F and K respectively. Source is zsh manual on visual effects: http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html#Visual-effects
So, for example, the following two commands
autoload -U colors && colors
export PS1="%F{214}%K{000}%m%F{015}%K{000}:%F{039}%K{000}%~%F{015}%K{000}\$ "
present the hostname in orange with black background, followed by a colon in white with black background, followed by the current working directory in bright blue with black background, followed by the dollar sign in white with black background.
More related information is found below.
Prompt information on the right-hand side. For example, adding a timestamp. See https://superuser.com/a/1251045/290299. Of course, this can be color-coded, for example with some light blue/purple-ish color, like this:
RPROMPT="%F{111}%K{000}[%D{%f/%m/%y}|%#]"
Colors for ls. After reading the manual for ls, one for example can activate the colors for ls using the following two commands:
export CLICOLOR=1
export LSCOLORS=gafacadabaegedabagacad
Finally, as a last remark that I have not tested as I am happy with my configuration, another avenue might be for someone to install the port coreutils from MacPorts and then use gdircolors (source: https://unix.stackexchange.com/a/174596/194343). (I may edit this last part in the future as all the above are related pieces that make every-day life much more fun and easier to cope with.)
I don't think the autoload -U colors && colors is needed anymore and one can simply do:
PS1="%{%F{red}%}%n%{%f%}#%{%F{blue}%}%m %{%F{yellow}%}%~ %{$%f%}%% "
to achieve the same result as FireDude's answer. See the ZSH documentation for more info.
Try my favorite:
put in
~/.zshrc
this line:
PROMPT='%F{240}%n%F{red}#%F{green}%m:%F{141}%d$ %F{reset}'
don't forget
source ~/.zshrc
to test the changes
you can change the colors/color codes, of course :-)
In order to not waste space in the terminal with special characters to pieces of information, you can differentiate information using multiple colors.
Eg. in order to get this desired effect:
You can do the following to your prompt:
PROMPT='%F{magenta}${PWD/#$HOME/~} %F{green}${vcs_info_msg_0_} %F{cyan}$%F{reset_color} '
The way it works is every time you set a color using $F{myColor} the color from that point onward will stick to that. It's important to add in %{reset_color} at the end so that the input text goes back to the original color (or you could set it to something else if you'd like).
I have found that, with zsh5 (the default one on Debian Jessie), all those solutions works:
$'\e[00m
$fg[white]
$fg{white}
Now, they have a problem: they will move the cursor, resulting in ugly decal when tabbing (for auto-completion). The solution is simply to surround the escape sequences with %{FOOBAR%}. Took me a while to figure this.
For 2nd and 3rd solutions loading colors module is mandatory.
To keep the 1st solution readable, just define variables for the colors you use.
Debian/Ubuntu/Raspberry Pi OS style bash prompt:
autoload -U colors && colors
PS1="%B%F{034}%n#%m%f%b:%B%F{019}%~ %#%f%b "
Not entirely sure if autoload -U colors && colors is necessary but I did need it when I was using $fg[...] instead of %F{...}.
In my example;
%F(%f) starts(stops) foreground color - I've used 256COLOR values but you could also use hex for example, I've used normal green %F{034} but %F{#00aa00} works too
%B(%b) starts(stops) bold
%n $USERNAME
%m short machine name
%~ logged in/working directory with ~ if in home directory
%# display % if normal user or # if su
Edit: Tested on macOS Big Sur, Monterey and Ventura. Unsure of earlier zsh versions but Ventura 13.0 is zsh 5.8.1 (x86_64-apple-darwin22.0)
To get a prompt with the color depending on the last command’s exit status, you could use this:
PS1='%(?.%F{green}.%F{red})%n#%m:%~%# %f'
Just add this line to your ~/.zshrc.
The documentation lists possible placeholders.
To complement all of the above answers another convenient trick is to place the coloured prompt settings into a zsh function. There you may define local variables to alias longer commands, e.g. rc=$reset_color or define your own colour variables. Don't forget to place it into your .zshrc file and call the function you have defined:
# Coloured prompt
autoload -U colors && colors
function myprompt {
local rc=$reset_color
export PS1="%F{cyan}%n%{$rc%}#%F{green}%m%{$rc%}:%F{magenta}%~%{$rc%}%# "
}
myprompt
man zshall and search for PROMPT EXPANSION
After reading the existing answers here, several of them are conflicting. I've tried the various approaches on systems running zsh 4.2 and 5+ and found that the reason these answers are conflicting is that they do not say which version of ZSH they are targeted at. Different versions use different syntax for this and some of them require various autoloads.
So, the best bet is probably to man zshall and search for PROMPT EXPANSION to find out all the rules for your particular installation of zsh. Note in the comments, things like "I use Ubuntu 11.04 or 10.4 or OSX" Are not very meaningful as it's unclear which version of ZSH you are using. Ubuntu 11.04 does not imply a newer version of ZSH than ubuntu 10.04. There may be any number of reasons that an older version was installed. For that matter a newer version of ZSH does not imply which syntax to use without knowing which version of ZSH it is.

Resources