Skip to content

Commit 1661d13

Browse files
committed
Initial commit: Add weather script for i3blocks
Add i3blocks-weather script with the following features: - Weather display from wttr.in for i3blocks status bar - Configurable caching (default 15 minutes) for performance - Interactive rofi popup with detailed weather report - Automatic color coding based on weather conditions - Auto-detection of i3bar colors and fonts - Lock file support to prevent concurrent executions - Configuration file support via ~/.config/i3blocks-weather/config - Comprehensive error handling and fallback to cache - Structured output (full/short/color lines) for i3blocks - Debug mode for troubleshooting - Full README with installation and configuration instructions Signed-off-by: Beau Hastings <beau@saweet.net>
1 parent 78340ef commit 1661d13

File tree

3 files changed

+639
-0
lines changed

3 files changed

+639
-0
lines changed

.github/workflows/shellcheck.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: ShellCheck CI
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
11+
jobs:
12+
shellcheck:
13+
name: Run ShellCheck
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
# Step 1: Check out the code
18+
- name: Checkout code
19+
uses: actions/checkout@v4
20+
21+
# Step 2: Install ShellCheck
22+
- name: Install ShellCheck
23+
run: sudo apt-get install -y shellcheck
24+
25+
# Step 3: Run ShellCheck on all shell scripts
26+
- name: Run ShellCheck
27+
run: |
28+
shellcheck weather

README.md

Lines changed: 277 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
# i3blocks-weather
2+
3+
Weather display script for [i3blocks] status bar. Fetches weather information from [wttr.in] and displays it in your status bar with an interactive popup for detailed forecasts.
4+
5+
[![License: GPL v2][license-badge]][license]
6+
7+
## Installation
8+
9+
1. Clone this repository:
10+
```bash
11+
git clone https://github.com/hastinbe/i3blocks-weather.git
12+
cd i3blocks-weather
13+
```
14+
15+
2. Make the script executable:
16+
```bash
17+
chmod +x weather
18+
```
19+
20+
3. Configure your `i3blocks` config file (typically `~/.config/i3blocks/config`):
21+
```ini
22+
[weather]
23+
full_text=☂ฺ ☀ฺ Weather ☂ฺ ☀ฺ
24+
command=~/i3blocks-weather/weather
25+
interval=1800
26+
color=#8ec07c
27+
#separator=true
28+
separator_block_width=20
29+
```
30+
31+
Replace `~/i3blocks-weather/weather` with the actual path to the `weather` script.
32+
33+
### Dependencies
34+
35+
The script requires the following dependencies:
36+
- `curl` - For fetching weather data from wttr.in
37+
- `rofi` - For displaying the interactive weather popup
38+
- `jq` - For parsing JSON configuration from i3wm
39+
40+
Install dependencies on:
41+
- **Debian/Ubuntu**: `sudo apt install curl rofi jq`
42+
- **Arch Linux**: `sudo pacman -S curl rofi jq`
43+
- **Fedora**: `sudo dnf install curl rofi jq`
44+
45+
## Usage
46+
47+
### i3blocks Integration
48+
49+
Add the weather block to your i3blocks configuration as shown in the installation section. The script will:
50+
- Display weather information (temperature, wind, condition icon, humidity) in your status bar
51+
- Update automatically every 30 minutes (1800 seconds) as specified by `interval`
52+
- Show a detailed ASCII weather report when you click on the block (left-click)
53+
54+
#### Display Format
55+
56+
The script displays weather information in the following format:
57+
- **Full line**: `ICON TEMPERATURE WIND 💧HUMIDITY`
58+
- **Short line**: `TEMPERATURE`
59+
- **Color**: Dynamically set based on weather conditions (sunny, cloudy, rainy, snowy, foggy)
60+
61+
### Interactive Popup
62+
63+
Click on the weather block in your status bar (left-click) to open an interactive rofi popup showing:
64+
- Full ASCII weather report from wttr.in
65+
- Detailed forecast information
66+
- Current conditions and extended forecast
67+
68+
The popup automatically matches your i3bar colors and font settings.
69+
70+
## Comparison with rofi-wttr
71+
72+
This script was designed as an enhanced alternative to [rofi-wttr] from i3blocks-contrib. Below is a feature comparison:
73+
74+
| Feature | i3blocks-weather | [rofi-wttr] |
75+
|---------|-----------------|-------------|
76+
| **Caching** | ✅ Yes (15 min default, configurable) | ❌ No |
77+
| **Lock file** | ✅ Yes (prevents concurrent runs) | ❌ No |
78+
| **Color coding** | ✅ Yes (automatic based on conditions) | ❌ No |
79+
| **Config file** | ✅ Yes (`~/.config/i3blocks-weather/config`) | ❌ No |
80+
| **Error handling** | ✅ Yes (fallback to cache, dependency checks) | ❌ No |
81+
| **Auto-detect i3 colors** | ✅ Yes (matches bar colors/font) | ❌ No |
82+
| **Customizable format** | ✅ Yes (via `FORMAT` variable) | ⚠️ Limited |
83+
| **Units selection** | ✅ Yes (US/metric via `UNITS`) | ⚠️ Hardcoded |
84+
| **Debug mode** | ✅ Yes | ❌ No |
85+
| **Structured output** | ✅ Yes (full/short/color lines) | ⚠️ Single line |
86+
| **Complexity** | Higher (~335 lines) | Lower (~70 lines) |
87+
| **Dependencies** | curl, rofi, jq | curl, rofi |
88+
| **Offline support** | ✅ Yes (uses cache) | ❌ No |
89+
| **Cache control** | ✅ Yes (configurable duration, set to 0 to disable) | ❌ No |
90+
91+
### Key Advantages
92+
93+
**i3blocks-weather** provides a more robust solution with:
94+
- **Performance**: Caching reduces API calls and provides instant responses
95+
- **Reliability**: Lock files prevent race conditions; error handling with fallbacks
96+
- **User Experience**: Automatic color coding, color/font matching with i3bar
97+
- **Flexibility**: Extensive configuration options via config file or environment variables
98+
- **Offline capability**: Works with cached data when network is unavailable
99+
100+
**rofi-wttr** is better for:
101+
- **Simplicity**: Easier to understand and modify (~70 lines)
102+
- **Minimal dependencies**: Doesn't require `jq`
103+
104+
## Configuration
105+
106+
`i3blocks-weather` looks for a configuration file located at either `~/.config/i3blocks-weather/config`, or `$XDG_CONFIG_HOME/i3blocks-weather/config`. You can use this file to set any variables that are not set in the command line or environment.
107+
108+
Create the configuration directory and file:
109+
```bash
110+
mkdir -p ~/.config/i3blocks-weather
111+
```
112+
113+
Then edit `~/.config/i3blocks-weather/config` with your settings. For example, if you want to always use metric units and set your location:
114+
115+
```bash
116+
LOCATION="Bismarck,ND"
117+
UNITS="m"
118+
CACHE_DURATION=1800
119+
```
120+
121+
Or if you want to customize the popup appearance and use a custom location:
122+
123+
```bash
124+
LOCATION="Paris,France"
125+
UNITS="m"
126+
ROFI_WIDTH="70%"
127+
ROFI_CONFIG_FILE="$HOME/.config/rofi/config.rasi"
128+
DEBUG=0
129+
```
130+
131+
Now every invocation of the script will use these settings, unless overridden by environment variables or command line options.
132+
133+
### Configuration Variables
134+
135+
| Variable | Description | Default |
136+
|----------|-------------|---------|
137+
| `LOCATION` | Weather location (city name, postal code, or coordinates) | `58501` |
138+
| `FORMAT` | wttr.in format string for weather data | `%t+%w+%c+%h` |
139+
| `UNITS` | Temperature units: `u` (US), `m` (metric), `M` (metric with wind in m/s) | `u` |
140+
| `CACHE_DURATION` | Cache duration in seconds | `900` (15 minutes) |
141+
| `CACHE_FILE` | Path to cache file | `/tmp/weather-wttr-cache.txt` |
142+
| `POPUP_CACHE_FILE` | Path to popup cache file | `/tmp/weather-wttr-popup-cache.txt` |
143+
| `BAR_ID` | i3bar identifier (auto-detected if not set) | Auto-detected |
144+
| `ROFI_CONFIG_FILE` | Path to rofi config file | `/dev/null` |
145+
| `ROFI_WIDTH` | Width of rofi popup window | `65%` |
146+
| `ROFI_LOCATION` | Location of rofi popup window | `northwest` |
147+
| `FONT` | Font for rofi popup (auto-detected from i3bar if not set) | Auto-detected |
148+
| `WEATHER_BG` | Background color for rofi popup (auto-detected from i3bar) | Auto-detected |
149+
| `WEATHER_FG` | Foreground color for rofi popup (auto-detected from i3bar) | Auto-detected |
150+
| `DEBUG` | Enable debug output (set to `1` to enable) | `0` |
151+
| `LABEL` | Optional label prefix for the weather display | Empty |
152+
153+
### Location Configuration
154+
155+
You can set the location in several ways:
156+
157+
1. **Using a configuration file** (recommended):
158+
```bash
159+
# ~/.config/i3blocks-weather/config
160+
LOCATION="Bismarck,ND"
161+
# or
162+
LOCATION="58501" # Postal code
163+
# or
164+
LOCATION="48.8566,2.3522" # Coordinates
165+
```
166+
167+
2. **Using environment variables**:
168+
```bash
169+
export LOCATION="Bismarck,ND"
170+
```
171+
172+
3. **In your i3blocks config**:
173+
```ini
174+
[weather]
175+
full_text=☂ฺ ☀ฺ Weather ☂ฺ ☀ฺ
176+
command=env LOCATION="Bismarck,ND" /path/to/this/repo/weather
177+
interval=1800
178+
```
179+
180+
### Units Configuration
181+
182+
- `u` - US units (Fahrenheit, mph)
183+
- `m` - Metric units (Celsius, km/h)
184+
- `M` - Metric units with wind in m/s
185+
186+
Set in your config file:
187+
```bash
188+
# ~/.config/i3blocks-weather/config
189+
UNITS="m"
190+
```
191+
192+
Or using environment variables:
193+
```bash
194+
export UNITS="m" # Use metric units
195+
```
196+
197+
### Custom Format
198+
199+
The `FORMAT` variable controls which weather data is fetched. Default format `%t+%w+%c+%h` includes:
200+
- `%t` - Temperature
201+
- `%w` - Wind
202+
- `%c` - Condition emoji
203+
- `%h` - Humidity
204+
205+
See [wttr.in format documentation](https://github.com/chubin/wttr.in#one-line-output) for all available format options.
206+
207+
## Weather Icons and Colors
208+
209+
The script automatically assigns colors based on weather conditions:
210+
211+
| Condition | Icon | Color |
212+
|-----------|------|-------|
213+
| Sunny | ☀️ | `#FFCC66` |
214+
| Partly Cloudy | 🌤️ ⛅ 🌥️ | `#ADD8E6` |
215+
| Rainy | 🌦️ 🌧️ ⛈️ | `#4169E1` |
216+
| Snowy | ❄️ 🌨️ | `#E0FFFF` |
217+
| Foggy | 🌫️ | `#D3D3D3` |
218+
| Unknown | (none) | `#ffffff` |
219+
220+
## Troubleshooting
221+
222+
### Script not displaying weather
223+
224+
1. Check that all dependencies are installed:
225+
```bash
226+
which curl rofi jq
227+
```
228+
229+
2. Test the script manually:
230+
```bash
231+
DEBUG=1 /path/to/weather
232+
```
233+
234+
3. Verify your location is valid by testing with curl:
235+
```bash
236+
curl "https://wttr.in/YourLocation?format=%t+%w+%c+%h"
237+
```
238+
239+
### Popup not showing
240+
241+
1. Ensure rofi is installed and working:
242+
```bash
243+
rofi -version
244+
```
245+
246+
2. Check that i3wm is running (required for bar config detection):
247+
```bash
248+
i3-msg -t get_version
249+
```
250+
251+
3. Enable debug mode to see what's happening:
252+
```bash
253+
DEBUG=1 /path/to/weather
254+
```
255+
256+
### Cache issues
257+
258+
If you're seeing stale weather data, you can:
259+
1. Manually delete the cache files:
260+
```bash
261+
rm /tmp/weather-wttr-cache.txt /tmp/weather-wttr-popup-cache.txt
262+
```
263+
264+
2. Reduce the `CACHE_DURATION` in the script (line 24)
265+
266+
## License
267+
268+
`i3blocks-weather` is released under [GNU General Public License v2][license]
269+
270+
Copyright (C) 2024 Beau Hastings
271+
272+
[i3blocks]: https://github.com/vivien/i3blocks
273+
[rofi-wttr]: https://github.com/vivien/i3blocks-contrib/blob/master/rofi-wttr/rofi-wttr
274+
[wttr.in]: https://wttr.in
275+
[license]: https://www.gnu.org/licenses/gpl-2.0.en.html
276+
[license-badge]: https://img.shields.io/badge/License-GPL%20v2-blue.svg
277+

0 commit comments

Comments
 (0)