Fish is a nice shell, its nice, lite, fast, and have a lot of features. One of them is universal variables
. This variables are shared across sessions. This is very useful when I want to have a nice handy information in the prompt. We don't want to execute command every time thats prompt appears. Lets use them.
This is what I'm talking about!
Widgets are refreshed in background, lets use cron
. Write a little script that gather system information and set some variable that can be displayed in prompt. Create a ~/.config/fish/functions/prompt_info_update.fish
_prompt_info_status_for
function takes 3 parameters:
I've got one for you here it is:
function _prompt_info_status_for
1]
set prompt_info_variable prompt_info_$argv[2]
set prompt_info_interval $argv[3]
set prompt_info_cmd $argv[
function _prompt_info_status_current_time
"+%s"
date
end
function _eval
result (eval $argv[1])
set if test -n result
echo $resultelse
''
echo
end
end
if set -q $prompt_info_variable
if test (math (_prompt_info_status_current_time) - $$prompt_info_variable[1][2] ) -ge $prompt_info_interval
-U $prompt_info_variable[1][2] (_prompt_info_status_current_time)
set -U $prompt_info_variable[1][1] (_eval $prompt_info_cmd)
set
endelse
-U $prompt_info_variable[1][2] (_prompt_info_status_current_time)
set -U $prompt_info_variable[1][1] (_eval $prompt_info_cmd)
set
end1][1]
echo $$prompt_info_variable[ end
Great. Lets set some widgets!!! I'm on OSX, lets use emoji as icons. We create 5 widgets:
This is so simple too. Gather information first, and glue our propry variable. prompt_info
this is a good name ;) Use some conditional, we don't want brew icon when we up to date, and display nothing when there are internet connection. Look, this is it:
function prompt_info_update --description "Update system information in prompt"
battery (math '60 * 1 * 1 ') 'pmset -g batt | egrep "([0-9]+\%).*" -o --colour=auto | cut -f1 -d";"'
_prompt_info_status_for disk (math '60 * 1 * 1 ') 'df -h / | tail -n1| awk "{ gsub(/i/,\"\"); print \$4 }"'
_prompt_info_status_for memory (math '60 * 1 * 1 ') 'top -l1 -s0 -n1 | awk "/PhysMem/ {print \$6}"'
_prompt_info_status_for brew (math '60 * 60 * 24') 'brew update > /dev/null; brew outdated | wc -l | tr -d "[[:space:]]"'
_prompt_info_status_for ping (math '60 * 1 * 1 ') 'ping -c1 google.com > /dev/null ; and echo "OK"'
_prompt_info_status_for
-U prompt_info ""
set
if test -n $prompt_info_battery[1]
-U prompt_info "$prompt_info🔋 $prompt_info_battery[1] "
set
end
-U prompt_info "$prompt_info📚 $prompt_info_memory[1] 💾 $prompt_info_disk[1] "
set
if test $prompt_info_brew[1] -gt 0
-U prompt_info "$prompt_info🍺 $prompt_info_brew[1] "
set
end
if not test -n $prompt_info_ping[1]
-U prompt_info "$prompt_info🚫 "
set
end end
Lets display in the right prompt.
function fish_right_prompt
echo $prompt_info end
At the end, we need add something that updates our widgets.
crontab -e $
and add
* * * * * /usr/local/bin/fish -c 'prompt_info_update'
And voila. Nice handy prompt widgets with about 50 LOC
Another great stuff, quick and useful.
Cheers.