Skip to content

Commit

Permalink
Added support for nerd fonts and include a weather class (#70)
Browse files Browse the repository at this point in the history
* Added support for nerd font symbols, with added argument --nerd. Updated README to include. :)

* Forgot to add the tooltips. Oops

* Another small fix. Had a wrong symbol.

* Optional, but I also added a css class which is whatever the current condition is.

* Removed repetition, removed redundency from README, and fixed bug in css class as some conditions had spaces.

* cleaned up min/max temp

* Fix for css classes

* optimize class setting

* fix icons inside tooltip

* formatting

* remove the weather_codes variable

* bump version

---------

Co-authored-by: Yo'av Moshe <gitlab@yoavmoshe.com>
  • Loading branch information
ashtray127 and Yo'av Moshe authored Dec 1, 2024
1 parent 68c1336 commit b76d302
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 77 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wttrbar"
version = "0.11.2"
version = "0.12.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ For NixOS, use the [NixPkg](https://search.nixos.org/packages?channel=24.05&show
- `--location STRING` - pass a specific location to wttr.in
- `--main-indicator` - decide which [`current_conditions` key](https://wttr.in/?format=j1) will be shown on waybar. defaults to `temp_C`
- `--date-format` - defaults to `%Y-%m-%d`, formats the date next to the days. see [reference](https://docs.rs/chrono/latest/chrono/format/strftime/index.html)
- `--nerd` - use [nerd font](https://www.nerdfonts.com/) symbols instead of emojis
- `--hide-conditions` - show a shorter descrpition next to each hour, like `7° Mist` instead of `7° Mist, Overcast 81%, Sunshine 17%, Frost 15%`
- `--fahrenheit` - use fahrenheit instead of celsius
- `--mph` - use mph instead of km/h for wind speed
Expand Down
3 changes: 3 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ pub struct Args {
#[arg(long, help = "display time in AM/PM format")]
pub ampm: bool,

#[arg(long, help = "use nerd font symbols instead of emojis")]
pub nerd: bool,

#[arg(long, help = "use fahrenheit instead of celsius")]
pub fahrenheit: bool,

Expand Down
162 changes: 113 additions & 49 deletions src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@
pub const WEATHER_CODES: &[(i32, &str)] = &[
(113, "☀️"),
(116, "🌤️"),
(119, "☁️"),
(122, "🌥️"),
(143, "🌫️"),
(176, "🌦️"),
(179, "🌧️"),
(182, "🌨️"),
(185, "🌨️"),
(200, "🌩️"),
(227, "❄️"),
(230, "❄️"),
(248, "🌫️"),
(260, "🌫️"),
(263, "🌧️"),
(266, "🌧️"),
(281, "🌦️"),
(284, "🌦️"),
(293, "🌧️"),
(296, "🌧️"),
(299, "🌧️"),
(302, "🌧️"),
(305, "🌧️"),
(308, "🌧️"),
(311, "🌧️"),
(314, "🌧️"),
(317, "🌧️"),
(320, "🌨️"),
(323, "🌨️"),
(326, "🌨️"),
(329, "🌨️"),
(332, "🌨️"),
(335, "🌨️"),
(338, "🌨️"),
(350, "🌨️"),
(353, "🌧️"),
(356, "🌧️"),
(359, "🌧️"),
(362, "🌨️"),
(365, "🌨️"),
(368, "🌨️"),
(371, "🌨️"),
(374, "🌨️"),
(377, "🌨️"),
(386, "🌩️"),
(389, "🌨️"),
(392, "🌨️"),
(395, "🌨️"),
(398, "🌨️"),
(113, "☀️"), // Sunny
(116, "🌤️"), // Partly cloudly
(119, "☁️"), // Cloudy
(122, "🌥️"), // Very cloudy
(143, "🌫️"), // Fog
(176, "🌦️"), // Light showers
(179, "🌧️"), // Light sleet showers
(182, "🌧️"), // Light sleet
(185, "🌧️"), // Light sleet
(200, "🌩️"), // Thundery showers
(227, "❄️"), // Light snow
(230, "❄️"), // Heavy snow
(248, "🌫️"), // Fog
(260, "🌫️"), // Fog
(263, "🌧️"), // Light showers
(266, "🌧️"), // Light rain
(281, "🌦️"), // Light sleet
(284, "🌦️"), // Light sleet
(293, "🌧️"), // Light rain
(296, "🌧️"), // Light rain
(299, "🌧️"), // Heavy showers
(302, "🌧️"), // Heavy rain
(305, "🌧️"), // Heavy showers
(308, "🌧️"), // Heavy rain
(311, "🌧️"), // Light sleet
(314, "🌧️"), // Light sleet
(317, "🌧️"), // Light sleet
(320, "🌨️"), // Light snow
(323, "🌨️"), // Light snow showers
(326, "🌨️"), // Light snow showers
(329, "🌨️"), // Heavy Snow
(332, "🌨️"), // Heavy Snow
(335, "🌨️"), // Heavy snow showers
(338, "🌨️"), // Heavy snow
(350, "🌨️"), // Light sleet
(353, "🌧️"), // Light showers
(356, "🌧️"), // Heavy showers
(359, "🌧️"), // Heavy rain
(362, "🌨️"), // Light sleet showers
(365, "🌨️"), // Light sleet showers
(368, "🌨️"), // Light snow showers
(371, "🌨️"), // Heavy snow showers
(374, "🌨️"), // Light sleet showers
(377, "🌨️"), // Light sleet
(386, "🌩️"), // Thundery showers
(389, "🌨️"), // Thundery heavy rain
(392, "🌨️"), // Thundery snow showers
(395, "🌨️"), // Heavy snow showers
(398, "🌨️"), // This is all the ones defined in the wttr.in source code, not sure what these are, but apparently some sort of rain.
(401, "🌨️"),
(404, "🌨️"),
(407, "🌨️"),
Expand All @@ -61,4 +61,68 @@ pub const WEATHER_CODES: &[(i32, &str)] = &[
(431, "🌨️"),
];

pub const WEATHER_CODES_NERD: &[(i32, &str)] = &[
(113, "󰖙"), // Sunny
(116, "󰖕"), // Partly cloudly
(119, "󰼰"), // Cloudy
(122, "󰖐"), // Very cloudy
(143, "󰖑"), // Fog
(176, "󰖗"), // Light showers
(179, "󰙿"), // Light sleet showers
(182, "󰙿"), // Light sleet
(185, "󰙿"), // Light sleet
(200, "󰙾"), // Thundery showers
(227, "󰖘"), // Light snow
(230, "󰼶"), // Heavy snow
(248, "󰖑"), // Fog
(260, "󰖑"), // Fog
(263, "󰖗"), // Light showers
(266, "󰖗"), // Light rain
(281, "󰙿"), // Light sleet
(284, "󰙿"), // Light sleet
(293, "󰖗"), // Light rain
(296, "󰖗"), // Light rain
(299, "󰖖"), // Heavy showers
(302, "󰖖"), // Heavy rain
(305, "󰖖"), // Heavy showers
(308, "󰖖"), // Heavy rain
(311, "󰙿"), // Light sleet
(314, "󰙿"), // Light sleet
(317, "󰙿"), // Light sleet
(320, "󰖘"), // Light snow
(323, "󰖘"), // Light snow showers
(326, "󰖘"), // Light snow showers
(329, "󰼶"), // Heavy Snow
(332, "󰼶"), // Heavy Snow
(335, "󰼶"), // Heavy snow showers
(338, "󰼶"), // Heavy snow
(350, "󰙿"), // Light sleet
(353, "󰖗"), // Light showers
(356, "󰖖"), // Heavy showers
(359, "󰖖"), // Heavy rain
(362, "󰙿"), // Light sleet showers
(365, "󰙿"), // Light sleet showers
(368, "󰖘"), // Light snow showers
(371, "󰼶"), // Heavy snow showers
(374, "󰙿"), // Light sleet showers
(377, "󰙿"), // Light sleet
(386, "󰙾"), // Thundery showers
(389, "󰙾"), // Thundery heavy rain
(392, "󰙾"), // Thundery snow showers
(395, "󰼶"), // Heavy snow showers
(398, "󰖗"), // This is all the ones defined in the wttr.in source code, not sure what these are, but apparently some sort of rain.
(401, "󰖗"),
(404, "󰖗"),
(407, "󰖗"),
(410, "󰖗"),
(413, "󰖗"),
(416, "󰖗"),
(419, "󰖗"),
(422, "󰖗"),
(425, "󰖗"),
(428, "󰖗"),
(431, "󰖗"),
];


pub const ICON_PLACEHOLDER: &str = "{ICON}";
81 changes: 56 additions & 25 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use reqwest::blocking::Client;
use serde_json::{json, Value};

use crate::cli::Args;
use crate::constants::{ICON_PLACEHOLDER, WEATHER_CODES};
use crate::constants::{ICON_PLACEHOLDER, WEATHER_CODES, WEATHER_CODES_NERD};
use crate::format::{format_ampm_time, format_chances, format_indicator, format_temp, format_time};
use crate::lang::Lang;

Expand Down Expand Up @@ -97,11 +97,19 @@ fn main() {
current_condition["FeelsLikeC"].as_str().unwrap()
};
let weather_code = current_condition["weatherCode"].as_str().unwrap();
let weather_icon = WEATHER_CODES
.iter()
.find(|(code, _)| *code == weather_code.parse::<i32>().unwrap())
.map(|(_, symbol)| symbol)
.unwrap();

let weather_icon = {
if args.nerd {
WEATHER_CODES_NERD
} else {
WEATHER_CODES
}
}
.iter()
.find(|(code, _)| *code == weather_code.parse::<i32>().unwrap())
.map(|(_, symbol)| symbol)
.unwrap();

let text = match args.custom_indicator {
None => {
let main_indicator_code = if args.fahrenheit && args.main_indicator == "temp_C" {
Expand Down Expand Up @@ -195,25 +203,34 @@ fn main() {
let date = NaiveDate::parse_from_str(day["date"].as_str().unwrap(), "%Y-%m-%d").unwrap();
tooltip += &format!("{}</b>\n", date.format(args.date_format.as_str()));

if args.fahrenheit {
tooltip += &format!(
"⬆️ {}° ⬇️ {}° ",
let (max_temp, min_temp) = if args.fahrenheit {
(
day["maxtempF"].as_str().unwrap(),
day["mintempF"].as_str().unwrap(),
);
)
} else {
tooltip += &format!(
"⬆️ {}° ⬇️ {}° ",
(
day["maxtempC"].as_str().unwrap(),
day["mintempC"].as_str().unwrap(),
);
)
};

tooltip += &format!(
"🌅 {} 🌇 {}\n",
"{} {}° {} {}° ",
if args.nerd { "󰳡" } else { "⬆️" },
max_temp,
if args.nerd { "󰳛" } else { "⬇️" },
min_temp
);

tooltip += &format!(
"{} {} {} {}\n",
if args.nerd { "󰖜" } else { "🌅" },
format_ampm_time(day, "sunrise", args.ampm),
format_ampm_time(day, "sunset", args.ampm),
if args.nerd { "󰖛" } else { "🌇" },
format_ampm_time(day, "sunset", args.ampm)
);

for hour in day["hourly"].as_array().unwrap() {
let hour_time = hour["time"].as_str().unwrap();
let formatted_hour_time = if hour_time.len() >= 2 {
Expand All @@ -231,16 +248,20 @@ fn main() {
let mut tooltip_line = format!(
"{} {} {} {}",
format_time(hour["time"].as_str().unwrap(), args.ampm),
WEATHER_CODES
.iter()
.find(|(code, _)| *code
== hour["weatherCode"]
.as_str()
.unwrap()
.parse::<i32>()
.unwrap())
.map(|(_, symbol)| symbol)
.unwrap(),
if args.nerd {
WEATHER_CODES_NERD
} else {
WEATHER_CODES
}
.iter()
.find(|(code, _)| *code
== hour["weatherCode"]
.as_str()
.unwrap()
.parse::<i32>()
.unwrap())
.map(|(_, symbol)| symbol)
.unwrap(),
if args.fahrenheit {
format_temp(hour["FeelsLikeF"].as_str().unwrap())
} else {
Expand All @@ -257,6 +278,16 @@ fn main() {
}
data.insert("tooltip", tooltip);

let css_class = current_condition[lang.weather_desc()][0]["value"]
.as_str()
.unwrap()
.to_lowercase()
.split(',')
.next()
.map(|s| s.trim().replace(' ', "_"))
.unwrap_or_default();
data.insert("class", css_class);

let json_data = json!(data);
println!("{}", json_data);
}

0 comments on commit b76d302

Please sign in to comment.