Skip to content

Commit c72d408

Browse files
authored
add current time indicator to graph (#44)
1 parent 76ed8b7 commit c72d408

File tree

4 files changed

+52
-18
lines changed

4 files changed

+52
-18
lines changed

src/modules/display/border.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ impl Separator {
106106
Border::R.fmt(border_variant)
107107
),
108108
Self::Dashed => format!("├{:┈>width$}┤", ""),
109-
Self::Single => format!("{}{:─>width$}{}", '├', "", '┤'),
109+
Self::Single => format!("├{:─>width$}", ""),
110110
Self::Solid => format!("┠{:─>width$}┨", ""),
111111
Self::Double => format!("╟{:─>width$}╢", ""),
112112
}

src/modules/display/current.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ impl Current {
256256
};
257257

258258
let hourly_forecast = match add_hourly {
259-
true => Some(Graph::prepare(weather, night, graph_variant, lang).await?),
259+
true => Some(Graph::prepare(weather, current_hour, night, graph_variant, lang).await?),
260260
_ => None,
261261
};
262262

src/modules/display/forecast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ impl Forecast {
121121
&match border_variant {
122122
BorderVariant::double => Separator::Double.fmt(width, border_variant),
123123
BorderVariant::solid => Separator::Solid.fmt(width, border_variant),
124-
_ => Separator::Single.fmt(width, border_variant),
124+
_ => Separator::Dashed.fmt(width, border_variant),
125125
}
126126
.color_option(BrightBlack, color_variant)
127127
)

src/modules/display/graph.rs

+49-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use anyhow::Result;
2+
use chrono::{Timelike, Utc};
23
use colored::{
34
Color::{Blue, BrightBlack, Yellow},
45
Colorize,
@@ -22,6 +23,7 @@ pub struct Graph {
2223
temperatures: String,
2324
graph: String,
2425
precipitation: String,
26+
time_indicator_col: usize,
2527
}
2628

2729
#[derive(Default, Serialize, Deserialize, PartialEq, Eq, Debug, Clone, Copy)]
@@ -65,12 +67,8 @@ impl Graph {
6567

6668
println!(
6769
"{}",
68-
&match border_variant {
69-
BorderVariant::double => Separator::Double.fmt(width, border_variant),
70-
BorderVariant::solid => Separator::Solid.fmt(width, border_variant),
71-
_ => Separator::Dashed.fmt(width, border_variant),
72-
}
73-
.color_option(BrightBlack, color_variant)
70+
self.prepare_separator(border_variant, width, '╤')
71+
.color_option(BrightBlack, color_variant),
7472
);
7573

7674
println!(
@@ -105,12 +103,8 @@ impl Graph {
105103

106104
println!(
107105
"{}",
108-
match border_variant {
109-
BorderVariant::double => Separator::Double.fmt(width, border_variant),
110-
BorderVariant::solid => Separator::Solid.fmt(width, border_variant),
111-
_ => Separator::Dashed.fmt(width, border_variant),
112-
}
113-
.color_option(BrightBlack, color_variant)
106+
self.prepare_separator(border_variant, width, '╧')
107+
.color_option(BrightBlack, color_variant),
114108
);
115109

116110
let hours = match units.time {
@@ -130,12 +124,24 @@ impl Graph {
130124
);
131125
}
132126

133-
pub async fn prepare(weather: &Weather, night: bool, graph_variant: &GraphVariant, lang: &str) -> Result<Self> {
127+
pub async fn prepare(
128+
weather: &Weather,
129+
current_hour: usize,
130+
night: bool,
131+
graph_variant: &GraphVariant,
132+
lang: &str,
133+
) -> Result<Self> {
134134
let temperatures = Self::prepare_temperature(weather, night, lang).await?;
135135
let precipitation = Self::prepare_precipitation(&weather.hourly.precipitation[..=24])?;
136136
let graph = Self::prepare_graph(&weather.hourly.temperature_2m[..=24], graph_variant)?;
137-
138-
Ok(Graph { temperatures, graph, precipitation })
137+
let time_indicator_col = current_hour * 3 + (Timelike::minute(&Utc::now()) / 20) as usize;
138+
139+
Ok(Graph {
140+
temperatures,
141+
graph,
142+
time_indicator_col,
143+
precipitation,
144+
})
139145
}
140146

141147
async fn prepare_temperature(weather: &Weather, night: bool, lang: &str) -> Result<String> {
@@ -213,4 +219,32 @@ impl Graph {
213219

214220
Ok(graph)
215221
}
222+
223+
fn prepare_separator(&self, border_variant: &BorderVariant, width: usize, time_indicator: char) -> String {
224+
let col_correction = 3;
225+
226+
match border_variant {
227+
BorderVariant::double => format!(
228+
"╟{:─>current_hour$}{:─>width$}╢",
229+
time_indicator,
230+
"",
231+
current_hour = self.time_indicator_col + col_correction,
232+
width = width - self.time_indicator_col - col_correction
233+
),
234+
BorderVariant::solid => format!(
235+
"┠{:─>current_hour$}{:─>width$}┨",
236+
time_indicator,
237+
"",
238+
current_hour = self.time_indicator_col + col_correction,
239+
width = width - self.time_indicator_col - col_correction
240+
),
241+
_ => format!(
242+
"├{:┈>current_hour$}{:┈>width$}┤",
243+
time_indicator,
244+
"",
245+
current_hour = self.time_indicator_col + col_correction,
246+
width = width - self.time_indicator_col - col_correction
247+
),
248+
}
249+
}
216250
}

0 commit comments

Comments
 (0)