1
1
use anyhow:: Result ;
2
2
use colored:: { Color :: BrightBlack , Colorize } ;
3
3
4
- use crate :: modules:: {
5
- localization:: WeatherLocales ,
6
- units:: { Time , Units } ,
7
- } ;
4
+ use crate :: modules:: params:: Params ;
8
5
9
6
use super :: {
10
7
border:: * ,
11
- graph:: GraphOpts ,
12
- gui_config:: { ColorOption , Gui } ,
8
+ gui_config:: ColorOption ,
13
9
hourly:: HourlyForecast ,
14
10
product:: { Product , MIN_WIDTH } ,
15
- utils:: lang_len_diff,
11
+ utils:: { lang_len_diff, Times } ,
16
12
weathercode:: WeatherCode ,
17
13
wind:: WindDirection ,
18
14
} ;
@@ -25,10 +21,9 @@ pub struct Current {
25
21
dewpoint : String ,
26
22
wind : String ,
27
23
pressure : String ,
28
- sun_rise : String ,
29
- sun_set : String ,
24
+ sunrise : String ,
25
+ sunset : String ,
30
26
wmo_code : WeatherCode ,
31
- hourly_forecast : Option < HourlyForecast > ,
32
27
dimensions : Dimensions ,
33
28
}
34
29
@@ -38,14 +33,7 @@ pub struct Dimensions {
38
33
}
39
34
40
35
impl Current {
41
- pub fn render (
42
- product : & Product ,
43
- add_hourly : bool ,
44
- units : & Units ,
45
- gui : & Gui ,
46
- lang : & str ,
47
- t : & WeatherLocales ,
48
- ) -> Result < Dimensions > {
36
+ pub fn render ( product : & Product , params : & Params , add_hourly : bool ) -> Result < Dimensions > {
49
37
let Current {
50
38
address,
51
39
temperature,
@@ -54,20 +42,17 @@ impl Current {
54
42
dewpoint,
55
43
wind,
56
44
pressure,
57
- sun_rise ,
58
- sun_set ,
45
+ sunrise ,
46
+ sunset ,
59
47
wmo_code,
60
- hourly_forecast,
61
48
dimensions,
62
- } = Self :: prepare ( product, add_hourly , units , & gui . graph , t ) ?;
49
+ } = Self :: prepare ( product, params , add_hourly ) ?;
63
50
64
51
let Dimensions { width, cell_width } = dimensions;
52
+ let ( gui, lang) = ( & params. config . gui , & params. config . language ) ;
65
53
66
54
// Border Top
67
- println ! (
68
- "{}" ,
69
- & Edge :: Top . fmt( width, & gui. border) . color_option( BrightBlack , & gui. color)
70
- ) ;
55
+ println ! ( "{}" , & Edge :: Top . fmt( width, & gui. border) . color_option( BrightBlack , & gui. color) ) ;
71
56
72
57
// Address / Title
73
58
println ! (
@@ -105,28 +90,29 @@ impl Current {
105
90
apparent_temperature,
106
91
Border :: R . fmt( & gui. border) . color_option( BrightBlack , & gui. color) ,
107
92
width = width - 2 - lang_len_diff( & apparent_temperature, lang)
93
+ // manually account for displacepment of this row until improving the lang_len_diff regex
94
+ + if & lang[ ..2 ] == "ja" || & lang[ ..2 ] == "ko" { 2 } else { 0 }
108
95
) ;
109
96
110
97
// Blank Line
111
98
println ! (
112
99
"{}" ,
113
- Separator :: Blank
114
- . fmt( width, & gui. border)
115
- . color_option( BrightBlack , & gui. color)
100
+ Separator :: Blank . fmt( width, & gui. border) . color_option( BrightBlack , & gui. color)
116
101
) ;
117
102
118
103
// Humidity & Dewpoint
119
104
println ! (
120
105
"{} {: <width$} {}" ,
121
106
Border :: L . fmt( & gui. border) . color_option( BrightBlack , & gui. color) ,
122
107
format!(
123
- "{: <cell_width$}{}" ,
108
+ "{: <cell_width$} {}" ,
124
109
humidity,
125
110
dewpoint,
126
111
cell_width = cell_width - lang_len_diff( & humidity, lang)
127
112
) ,
128
113
Border :: R . fmt( & gui. border) . color_option( BrightBlack , & gui. color) ,
129
114
width = width - 2 - lang_len_diff( & humidity, lang) - lang_len_diff( & dewpoint, lang)
115
+ + if & lang[ ..2 ] == "ja" || & lang[ ..2 ] == "ko" { 3 } else { 0 }
130
116
) ;
131
117
132
118
// Wind & Pressure
@@ -143,71 +129,45 @@ impl Current {
143
129
println ! (
144
130
"{} {: <cell_width$}{: <width$} {}" ,
145
131
Border :: L . fmt( & gui. border) . color_option( BrightBlack , & gui. color) ,
146
- sun_rise ,
147
- sun_set ,
132
+ sunrise ,
133
+ sunset ,
148
134
Border :: R . fmt( & gui. border) . color_option( BrightBlack , & gui. color) ,
149
135
width = width - 2 - cell_width
150
136
) ;
151
137
152
138
// Hourly Forecast
153
- if let Some ( hourly_forecast ) = hourly_forecast {
154
- hourly_forecast . render ( width , units , & gui . border , & gui . color , t )
139
+ if add_hourly {
140
+ HourlyForecast :: render ( & product . weather , params , 0 ) ? ;
155
141
}
156
142
157
143
// Border Bottom
158
- println ! (
159
- "{}" ,
160
- Edge :: Bottom
161
- . fmt( width, & gui. border)
162
- . color_option( BrightBlack , & gui. color)
163
- ) ;
144
+ println ! ( "{}" , Edge :: Bottom . fmt( width, & gui. border) . color_option( BrightBlack , & gui. color) ) ;
164
145
165
146
Ok ( dimensions)
166
147
}
167
148
168
- fn prepare (
169
- product : & Product ,
170
- add_hourly : bool ,
171
- units : & Units ,
172
- graph_opts : & GraphOpts ,
173
- t : & WeatherLocales ,
174
- ) -> Result < Self > {
149
+ fn prepare ( product : & Product , params : & Params , add_hourly : bool ) -> Result < Self > {
175
150
let weather = & product. weather ;
176
151
let address = Product :: trunc_address ( product. address . clone ( ) , 60 ) ;
152
+ let t = & params. texts . weather ;
177
153
178
- // Helpers
179
- let ( current_hour, sunrise_hour, sunset_hour) = (
180
- weather. current_weather . time [ 11 ..13 ]
181
- . parse :: < usize > ( )
182
- . unwrap_or_default ( ) ,
183
- weather. daily . sunrise [ 0 ] [ 11 ..13 ] . parse :: < usize > ( ) . unwrap_or_default ( ) ,
184
- weather. daily . sunset [ 0 ] [ 11 ..13 ] . parse :: < usize > ( ) . unwrap_or_default ( ) ,
185
- ) ;
186
- let sunrise_time = match units. time {
187
- Time :: am_pm => format ! ( "{}:{}am" , sunrise_hour, & weather. daily. sunrise[ 0 ] [ 14 ..16 ] ) ,
188
- _ => weather. daily . sunrise [ 0 ] [ 11 ..16 ] . to_string ( ) ,
189
- } ;
190
- let sunset_time = match units. time {
191
- Time :: am_pm => format ! ( "{}:{}pm" , sunset_hour - 12 , & weather. daily. sunset[ 0 ] [ 14 ..16 ] ) ,
192
- _ => weather. daily . sunset [ 0 ] [ 11 ..16 ] . to_string ( ) ,
193
- } ;
194
- let night = current_hour < sunrise_hour || current_hour > sunset_hour;
154
+ let Times { current_hour, sunrise, sunset, night } = product. weather . get_times ( params. config . units . time , 0 ) ;
195
155
196
156
// Display Items
197
157
let temperature = format ! (
198
- "{}{}" ,
158
+ "{:.1 }{}" ,
199
159
weather. current_weather. temperature, weather. hourly_units. temperature_2m
200
160
) ;
201
161
let apparent_temperature = format ! (
202
- "{} {}{}" ,
162
+ "{} {:.1 }{}" ,
203
163
t. feels_like, weather. hourly. apparent_temperature[ current_hour] , weather. hourly_units. temperature_2m
204
164
) ;
205
165
let humidity = format ! (
206
166
"{}: {}{}" ,
207
167
t. humidity, weather. hourly. relativehumidity_2m[ current_hour] , weather. hourly_units. relativehumidity_2m,
208
168
) ;
209
169
let dewpoint = format ! (
210
- "{}: {}{}" ,
170
+ "{}: {:.1 }{}" ,
211
171
t. dew_point, weather. hourly. dewpoint_2m[ current_hour] , weather. hourly_units. dewpoint_2m
212
172
) ;
213
173
let wind_direction = WindDirection :: get_direction ( weather. current_weather . winddirection ) ?;
@@ -222,28 +182,17 @@ impl Current {
222
182
" {}{}" ,
223
183
weather. hourly. surface_pressure[ current_hour] , weather. hourly_units. surface_pressure
224
184
) ;
225
- let sun_rise = format ! ( " {sunrise_time }" ) ;
226
- let sun_set = format ! ( " {sunset_time }" ) ;
185
+ let sunrise = format ! ( " {sunrise }" ) ;
186
+ let sunset = format ! ( " {sunset }" ) ;
227
187
let wmo_code = WeatherCode :: resolve ( weather. current_weather . weathercode , night, & t. weather_code ) ?;
228
- let hourly_forecast = match add_hourly {
229
- true => Some ( HourlyForecast :: prepare (
230
- weather,
231
- current_hour,
232
- night,
233
- graph_opts,
234
- units,
235
- & t. weather_code ,
236
- ) ?) ,
237
- _ => None ,
238
- } ;
239
188
240
189
// Dimensions
241
190
let title_width = address. chars ( ) . count ( ) ;
242
191
let title_padding = 2 * 2 ; // 2 spaces on each side
243
192
let longest_cell_width = humidity. chars ( ) . count ( ) ;
244
193
let dimensions = Dimensions {
245
194
width : if add_hourly {
246
- 72
195
+ super :: hourly :: WIDTH
247
196
} else if title_width > MIN_WIDTH {
248
197
title_width + title_padding
249
198
} else {
@@ -267,10 +216,9 @@ impl Current {
267
216
dewpoint,
268
217
wind,
269
218
pressure,
270
- sun_rise ,
271
- sun_set ,
219
+ sunrise ,
220
+ sunset ,
272
221
wmo_code,
273
- hourly_forecast,
274
222
dimensions,
275
223
} )
276
224
}
0 commit comments