Making Bash prompt useful - S01E01
Publish date: Dec 27, 2020
Introduction
I like knowing about weather. Discussing weather is good old technique which always gets one out of an awkward silent “conversation” π.
In the very first episode, we will try show weather information in the bash prompt. The idea is simple:
- Find some API to get current weather data - preferably free!
- Learn how bash prompt work. Ideally, this should have been the first thing to do, but nevermind…
- Write code to fetch weather data.
- Maintain a local cache of weather data and decide expiration period.
- Write more code…
Demo
I want to use it straightaway, I don’t want to read your stupid blogpost
Sure, why not π’.
$ git clone https://github.com/return007/weather-prompt
$ cd weather-prompt
$ ./install.sh
βββ βββββββββββ ββββββ ββββββββββββ ββββββββββββββββββ βββββββ βββββββ βββββββ ββββ βββββββββββ βββββββββ
βββ βββββββββββββββββββββββββββββββ βββββββββββββββββββ ββββββββββββββββββββββββββββββ ββββββββββββββββββββββ
βββ ββ βββββββββ ββββββββ βββ ββββββββββββββ ββββββββ βββββββββββββββββββ ββββββββββββββββββββββ βββ
ββββββββββββββββ ββββββββ βββ ββββββββββββββ ββββββββ βββββββ βββββββββββ βββββββββββββββββββββ βββ
βββββββββββββββββββββ βββ βββ βββ ββββββββββββββ βββ βββ βββ βββββββββββββββ βββ ββββββ βββ
ββββββββ βββββββββββ βββ βββ βββ ββββββββββββββ βββ βββ βββ βββ βββββββ βββ ββββββ βββ
Enter the API key to access your OpenWeather account.
Press enter if you don't have an API key:
No issues! Let me help you in getting your free API key...
Please follow the below steps:
1. Go to the following link to setup your account
https://home.openweathermap.org/users/sign_up
2. After signup, go to the following page to access your API key
https://home.openweathermap.org/api_keys
Enter the API key to access your OpenWeather account: XXXXXXXXXXXX
Enter the name of your city: XXXXXX
$
Oh you are still reading, thanks!
- Bash prompt can be modified by over-writing
PS1
environment variable. - The relevant data is fetched once in an hour from OpenWeather’s website (https://openweathermap.org).
- This weather data is stored in
~/.weatherinfo
file. - The bash prompt calls this dedicated function, which first refers to this file.
- If the file is not present, then it tried to fetch data from the website.
- If that works, then awesome! The newly fetched data is stored in the
~/.weatherinfo
cache. - If that fails, then we display error accordingly (like Unauthorized, etc.)
- If that works, then awesome! The newly fetched data is stored in the
- If the file is present, then it checks if it is stale or not
- If the file is stale (i.e. written more than an hour back), then it proceeds like step (4a.)
- If not, then the contents of the file (cache) are read and shown in the prompt.
- If the file is not present, then it tried to fetch data from the website.
- Done!