Although I work in tech, at a cloud hosting company to be precise, it is not allowed to work remotely fulltime.
So several times a week I join the flood of commuters, heading to the next big city in our region. But unlike many the next big city is not my commuting destination. Once I reached this city, I have two options. Either traverse through the center of said big city, or follow the highway and driving through a part of the highway system with an even higher traffic occurence.
Traffic induced delay may sum up to about 2 hours.
No matter which option I choose, spending that much time in traffic sucks.
To minimize the time driving, I made it a habit to check the traffic with Google Maps before I drive off.
Fortunately my company implemented the concept of flexible working hours.
This allows me to adjust the time of my departure according to the predicted traffic.
Checking Google Maps several times, every day is tedious.
Eventhough their UX is acceptable, wouldn’t it be easier to automate this?
Since a good portion of my work is within a terminal, my goal was to display the estimated travel time in my tmux powerline status bar.
To achieve my goal I wrote traveltime. Traveltime is a command line tool, written in Go.
You pass your work and home coordinates to traveltime. First traveltime will guess whether you are at home or at work and therefore know in which direction you are traveling. This guess is not like rolling a dice, but traveltime is fetching your geolocation based on your public IP.
With your location, it calculates the distance to home and work location. Shortest distance wins and becomes the starting point. Traveltime then asks the Google Maps API for the ideal traveltime and the current estimated traveltime (with traffic).
The text/template package is utilized to provide customizable output formats. Output is written to stdout and in the default configuration it may look like home: 18min +4min.
Environment variables are used to configure traveltime:
| Name | Use Case | Mandatory |
|---|---|---|
| GOOGLE_API_KEY | Key to access Google APIs | Yes |
| TRAVEL_WORK_COORD | <name>,<latitude>,<longitude> of your work | Yes |
| TRAVEL_HOME_COORD | <name>,<latitude>,<longitude> of your home | Yes |
| TRAVEL_WORK_SYMBOL | Arbitrary representation, for not using the name | No |
| TRAVEL_HOME_SYMBOL | Arbitrary representation, for not using the name | No |
I have setup a cron job that executes traveltime every 15 minutes.
Cron forks a new shell session for each execution and loads the GOOGLE_API_KEY from file into the command. TRAVEL_WORK_COORD and TRAVEL_HOME_COORD are configured in my $HOME/.profile file, that is dot sourced from the cron job.
Stdout is redirected into a file .cache/traveltime. This is also the file that powerline is configured to read and display its content.
The file acts as a cache and is reducing the amount of API calls against the Google API, helping to not hit the rate limit. Also allowing powerline to read from the local filesystem eliminates network latency from the powerline rendering process. This speeds it up and improves the user experience.