📜 ⬆️ ⬇️

zsh :: set up the “right” command line prompt

Tinkering with the zsh settings, I came across a very interesting solution for inviting the command line, which is located in this shell on the right . Usually, it is customary to put a clock there, but I quite liked the idea of ​​placing a battery charge notifier there as well. Depending on the level of charge, the notifier is shown in green, yellow or red. The author of the idea for some reason dragged to the solution of python, I rewrote everything in my native shell.
It looks like this:
Command line prompt, zsh

The code below was tested on ubuntu, except for the colors ( %F{…} ) everything is required to work on any distributions for zsh . So let's get started.
The right command line prompt in zsh is set by the RPROMPT environment RPROMPT . Here is a piece of code that makes sense to put in ~/.zshrc (or ~/.oh-my-zsh/themes/_ , if you already know about oh-my-zsh ):
 function battery_charge { # Battery 0: Discharging, 94%, 03:46:34 remaining bat_percent=`acpi | awk -F ':' {'print $2;'} | awk -F ',' {'print $2;'} | sed -e "s/\s//" -e "s/%.*//"` if [ $bat_percent -lt 20 ]; then cl='%F{red}' elif [ $bat_percent -lt 50 ]; then cl='%F{yellow}' else cl='%F{green}' fi filled=${(l:`expr $bat_percent / 10`::▸:)} empty=${(l:`expr 10 - $bat_percent / 10`::▹:)} echo $cl$filled$empty'%F{default}' } RPROMPT='[%*] $(battery_charge)' 

The algorithm works as simple as an amoeba in the extramarital period:
- received and parsed the battery from acpi (you may need to install sudo apt-get install acpi );
- determined the color of the display (red, if less than 20%, yellow - up to 50%, otherwise - green);
- draw how many shaded triangles you need, then the remainder - unfilled;
- brought to the right command line prompt.

This note is simply an illustration of the principle of “spend three minutes and make your life more comfortable forever.” If you, of course, are the same adept console as I am. I know that the code is not licked to the state “can be on the exhibition” - but why do I need it?

As another example, we display the return code of the previous command on the right:
 RPROMPT='%{$fg[red]%} ⏎ $? %{$reset_color%} '$RPROMPT 

')

Source: https://habr.com/ru/post/132599/


All Articles