1
1
use anyhow:: Result ;
2
+ use chrono:: { Timelike , Utc } ;
2
3
use colored:: {
3
4
Color :: { Blue , BrightBlack , Yellow } ,
4
5
Colorize ,
@@ -22,6 +23,7 @@ pub struct Graph {
22
23
temperatures : String ,
23
24
graph : String ,
24
25
precipitation : String ,
26
+ time_indicator_col : usize ,
25
27
}
26
28
27
29
#[ derive( Default , Serialize , Deserialize , PartialEq , Eq , Debug , Clone , Copy ) ]
@@ -65,12 +67,8 @@ impl Graph {
65
67
66
68
println ! (
67
69
"{}" ,
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) ,
74
72
) ;
75
73
76
74
println ! (
@@ -105,12 +103,8 @@ impl Graph {
105
103
106
104
println ! (
107
105
"{}" ,
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) ,
114
108
) ;
115
109
116
110
let hours = match units. time {
@@ -130,12 +124,24 @@ impl Graph {
130
124
) ;
131
125
}
132
126
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 > {
134
134
let temperatures = Self :: prepare_temperature ( weather, night, lang) . await ?;
135
135
let precipitation = Self :: prepare_precipitation ( & weather. hourly . precipitation [ ..=24 ] ) ?;
136
136
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
+ } )
139
145
}
140
146
141
147
async fn prepare_temperature ( weather : & Weather , night : bool , lang : & str ) -> Result < String > {
@@ -213,4 +219,32 @@ impl Graph {
213
219
214
220
Ok ( graph)
215
221
}
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
+ }
216
250
}
0 commit comments