├── .editorconfig ├── LICENSE ├── README.md ├── scripts ├── forecast.sh └── helpers.sh └── weather.tmux /.editorconfig: -------------------------------------------------------------------------------- 1 | [*] 2 | end_of_line = lf 3 | insert_final_newline = true 4 | indent_style = space 5 | indent_size = 2 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Aaron Powell 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Tmux Weather 2 | 3 | Tmux plugin that displays weather forecast using [wttr.in](http://wttr.in) in the status bar. 4 | 5 | The plugin introduces a new `#{forecast}` format. 6 | 7 | Thanks to [Rahul Rai](https://github.com/rahulrai-in) for the inspiration, I just packaged it. 😉 8 | 9 | ## Usage 10 | 11 | Add `#{forecast}` to your existing `status-right` tmux option: 12 | 13 | ```bash 14 | set -g status-right '#{forecast} | #H' 15 | ``` 16 | 17 | You'll now see some information like so: 18 | 19 | ``` 20 | Partly cloudy +13°C ↘13km/h | AaronSB2 21 | ``` 22 | 23 | ## Customisation 24 | 25 | By default the format string used is `%C+%t+%w`, but you can override this with the `@forecast-format` option: 26 | 27 | ```bash 28 | set -g @forecast-format '%C'+'|'+'Dusk:'+'%d' 29 | ``` 30 | 31 | Refer to [wttr.in](http://wttr.in) for format options. 32 | 33 | Additionally, you might want to specify the location manually, you can do so with the `@forecast-location` option: 34 | 35 | ```bash 36 | set -g @forecast-location London 37 | ``` 38 | 39 | Refer to [wttr.in](https://wttr.in/:help) for location options. 40 | 41 | Moreover a character limit is enforced, in order to avoid errors trashing your status bar. 42 | This limit defaults to 75, but you can increase/decrease it as you wish: 43 | 44 | ```bash 45 | set -g @forecast-char-limit 30 46 | ``` 47 | 48 | The default forecast language is English. You can change this using a two-letter language code 49 | 50 | ```bash 51 | set -g @forecast-language "nl" 52 | ``` 53 | 54 | It's possible to enable cache for weather data in file and use it instead of spamming wttr.in 55 | service. Weather (and forecast) doesn't change every minute (or even every second) so no need to add 56 | extra load on wttr.in in case your status-line updated quite often. 57 | To enable caching just set its duration in seconds 58 | 59 | ```bash 60 | set -g @forecast-cache-duration 900 # 15 minutes 61 | ``` 62 | 63 | There is also an option where to store the cache file 64 | 65 | ```bash 66 | # this is the default 67 | set -g @forecast-cache-path "/tmp/tmux-weather.cache" 68 | ``` 69 | 70 | ## Installation with [Tmux Plugin Manager](https://github.com/tmux-plugins/tpm) (recommended) 71 | 72 | Add plugin to the list of TPM plugins in `.tmux.conf`: 73 | 74 | ```bash 75 | set -g @plugin 'aaronpowell/tmux-weather' 76 | ``` 77 | 78 | Hit `prefix + I` to fetch the plugin and source it. 79 | 80 | `#{forecast}` interpolation should now work. 81 | 82 | ### Manual Installation 83 | 84 | Clone the repo: 85 | 86 | ```bash 87 | $ git clone https://github.com/aaronpowell/tmux-weather ~/clone/path 88 | ``` 89 | 90 | Add this line to the bottom of `.tmux.conf`: 91 | 92 | ```bash 93 | run-shell ~/clone/path/weather.tmux 94 | ``` 95 | 96 | Reload TMUX environment: 97 | 98 | ```bash 99 | # type this in terminal 100 | $ tmux source-file ~/.tmux.conf 101 | ``` 102 | 103 | `#{forecast}` interpolation should now work. 104 | 105 | ## License 106 | 107 | [MIT](LICENSE) 108 | -------------------------------------------------------------------------------- /scripts/forecast.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 4 | 5 | source "$CURRENT_DIR/helpers.sh" 6 | 7 | get_forecast() { 8 | local format=$(get_tmux_option @forecast-format "%C+%t+%w") 9 | local location=$(get_tmux_option @forecast-location "") # Let wttr.in figure out the location 10 | local language=$(get_tmux_option @forecast-language "en") 11 | local units=$(get_tmux_option @forecast-units "") 12 | curl "http://wttr.in/$location?$units&format=$format&lang=$language" 13 | } 14 | 15 | get_cached_forecast() { 16 | local cache_duration=$(get_tmux_option @forecast-cache-duration 0) # in seconds, by default cache is disabled 17 | local cache_path=$(get_tmux_option @forecast-cache-path "/tmp/tmux-weather.cache") # where to store the cached data 18 | local cache_age=$(get_file_age "$cache_path") 19 | local forecast 20 | if [ $cache_duration -gt 0 ]; then # Cache enabled branch 21 | # if file does not exist or cache age is greater then configured duration 22 | if ! [ -f "$cache_path" ] || [ $cache_age -ge $cache_duration ]; then 23 | forecast=$(get_forecast) 24 | if [ -n "$forecast" ]; then 25 | # store forecast in $cache_path 26 | mkdir -p "$(dirname "$cache_path")" 27 | echo "$forecast" > "$cache_path" 28 | fi 29 | else 30 | # otherwise try to get it from cache file 31 | forecast=$(cat "$cache_path" 2>/dev/null) 32 | fi 33 | else # Cache disabled branch 34 | forecast=$(get_forecast) 35 | fi 36 | echo "$forecast" 37 | } 38 | 39 | print_forecast() { 40 | local char_limit=$(get_tmux_option @forecast-char-limit 75) 41 | local forecast=$(get_cached_forecast) 42 | echo ${forecast:0:$char_limit} 43 | } 44 | 45 | main() { 46 | print_forecast 47 | } 48 | 49 | main 50 | -------------------------------------------------------------------------------- /scripts/helpers.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set_tmux_option() { 4 | local option="$1" 5 | local value="$2" 6 | tmux set-option -gq "$option" "$value" 7 | } 8 | 9 | get_tmux_option() { 10 | local option="$1" 11 | local default_value="$2" 12 | local option_value="$(tmux show-option -gqv "$option")" 13 | if [ -z "$option_value" ]; then 14 | echo "$default_value" 15 | else 16 | echo "$option_value" 17 | fi 18 | } 19 | 20 | # get files age in seconds 21 | get_file_age() { # $1 - cache file 22 | local file_path="${1:-}" 23 | local now=$(date +%s) 24 | 25 | if uname | grep -q "Darwin"; then 26 | local file_modification_timestamp=$(stat -f "%m" "$file_path" 2>/dev/null || echo 0) 27 | else 28 | local file_modification_timestamp=$(stat -c "%Y" "$file_path" 2>/dev/null || echo 0) 29 | fi 30 | 31 | if [ $file_modification_timestamp -ne 0 ]; then 32 | echo $((now - file_modification_timestamp)) 33 | else 34 | # return 0 if could not get cache modification time, eg. file does not exist 35 | echo 0 36 | fi 37 | } 38 | -------------------------------------------------------------------------------- /weather.tmux: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 4 | 5 | source "$CURRENT_DIR/scripts/helpers.sh" 6 | 7 | weather_details="#($CURRENT_DIR/scripts/forecast.sh)" 8 | weather_details_interpolation_string="\#{forecast}" 9 | 10 | do_interpolation() { 11 | local string="$1" 12 | local interpolated="${string/$weather_details_interpolation_string/$weather_details}" 13 | echo "$interpolated" 14 | } 15 | 16 | update_tmux_option() { 17 | local option="$1" 18 | local option_value="$(get_tmux_option "$option")" 19 | local new_option_value="$(do_interpolation "$option_value")" 20 | set_tmux_option "$option" "$new_option_value" 21 | } 22 | 23 | main() { 24 | update_tmux_option "status-right" 25 | update_tmux_option "status-left" 26 | } 27 | main 28 | --------------------------------------------------------------------------------