Fishy Prompt Widgets

Michał `fazibear` Kalbarczyk
2 min readApr 22, 2015

--

Fish is a nice shell, its nice, lite, fast, and have a lot of features. One of them is universal variables. These 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 the command every time that prompt appears. Let’s use them.

This is what I’m talking about!

Widgets are refreshed in the background, let’s use cron. Write a little script that gathers 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:

  • a name of widget
  • how often we want to refresh widget
  • command line

I’ve got one for you here it is:

Great. Let’s set some widgets!!! I’m on OSX, let’s use emoji as icons. We create 5 widgets:

  • battery status
  • free disk space
  • free memory
  • number of outdated brew package (refreshed once a day)
  • internet connection check

This is so simple too. Gather information first, and glue our prompt 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 is internet connection. Look, this is it:

Let’s display in the right prompt.

function fish_right_prompt
echo $prompt_info
end

At the end, we need to 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.

Originally published here.

--

--