This post is inspired by the beautiful unix command
for extra fun,
$ fortune | cowsay
/ You can not get anything worthwhile \
| done without raising a sweat. |
| -- The First Law Of Thermodynamics |
| What ever you want is going to cost a |
| little more than it is worth. |
| -- The Second Law Of Thermodynamics |
| You can not win the game, and you are |
| not allowed to stop playing. |
\ -- The Third Law Of Thermodynamics /
Earlier this month, I discovered this awesome website called ComandLineFu. The idea of this website is to provide unix gurus (and n00bs) a way to learn unix tricks from masters. It has a voting mechanism so that good unix trick will surface up the rankings and land on the front pages.
Anyways, they have an API. So I thought, why can’t I have a way of showing random unix tricks from commandlinefu in a way that is similar to
$ fortune ?
First, I wanted a few requirements. Here is what I wanted:
- Show tips randomly
- Show brief explanation for each tip.
- Update tips every x seconds
The first two requirements can be fulfilled by the API. As for the last requirement, I will be using tmux status line and update that every x seconds.
Let us implement the first requirement. The way I did that was through
$ curl and
$ jq. Here is the code:
$ page=`shuf -i 0-1000 -n 1`; item=`shuf -i 0-24 -n 1`; curl -s "http://www.commandlinefu.com/commands/browse/sort-by-votes/json/$page" | jq -r ".[$item] | [.command, .summary] | @tsv" | sed "s/$/ # /g"
Let us break it down real quick. First, we initialize
item to random numbers (I picked these ranges because these are the items I am interested in). Then, we
$ curl the API with the random page as the endpoint and pipe the result into
$ jq. Then we grab the fields that we are interested in, namely, ‘command’ and ‘summary’ and do some formatting gymnastics. That is it. As simple as that.
Now that we have the
$ fortune part of the pico-project, how do we go about
$ cowsay? It is just a couple of lines in tmux.conf! Just add the following:
set -g status-interval 60
set -g status-justify centre # center align window list
set -g status-left-length 200
set -g status-right-length 140
set -g status-left '#[fg=green]#H #[fg=black]• #[fg=green,bright]#(page=`shuf -i 0-1000 -n 1`; item=`shuf -i 0-24 -n 1`; curl -s "http://www.commandlinefu.com/command s/browse/sort-by-votes/json/$page" | jq -r ".[$item] | [.command, .summary] | @tsv" | sed "s/ / # /g")#[default]'
And you are done. Now you have a tmux session that will show you random unix tips every 60 seconds. Cool right?