31
31
//! - `tasks`
32
32
33
33
use super :: prelude:: * ;
34
- use tokio:: process:: Command ;
35
34
use chrono:: DateTime ;
35
+ use tokio:: process:: Command ;
36
36
37
37
#[ derive( Deserialize , Debug , SmartDefault ) ]
38
38
#[ serde( default ) ]
39
39
pub struct Config {
40
40
#[ default( 30 . into( ) ) ]
41
- interval : Seconds ,
41
+ interval : Seconds ,
42
42
format : FormatConfig ,
43
43
44
44
info : Option < u64 > ,
@@ -48,18 +48,10 @@ pub struct Config {
48
48
}
49
49
50
50
pub async fn run ( config : Config , mut api : CommonApi ) -> Result < ( ) > {
51
+ api. set_default_actions ( & [ ( MouseButton :: Left , None , "stop_continue" ) ] )
52
+ . await ?;
51
53
52
- api. set_default_actions ( & [
53
- ( MouseButton :: Left , None , "stop_continue" ) ,
54
- ] )
55
- . await ?;
56
-
57
- let widget = Widget :: new ( ) . with_format (
58
- config
59
- . format
60
- . with_default ( " $icon {$elapsed|}" ) ?,
61
- ) ;
62
-
54
+ let widget = Widget :: new ( ) . with_format ( config. format . with_default ( " $icon {$elapsed|}" ) ?) ;
63
55
64
56
loop {
65
57
let mut values = map ! {
@@ -80,7 +72,6 @@ pub async fn run(config: Config, mut api: CommonApi) -> Result<()> {
80
72
( & config. warning , State :: Warning ) ,
81
73
( & config. good , State :: Good ) ,
82
74
( & config. info , State :: Info ) ,
83
-
84
75
] {
85
76
if let Some ( value) = level {
86
77
if ( elapsed. num_minutes ( ) as u64 ) >= * value {
@@ -92,9 +83,8 @@ pub async fn run(config: Config, mut api: CommonApi) -> Result<()> {
92
83
93
84
values. insert ( "tags" . into ( ) , Value :: text ( tw. tags . join ( " " ) ) ) ;
94
85
95
- let elapsedstr = format ! ( "{}:{:0>2}" ,
96
- elapsed. num_hours( ) ,
97
- elapsed. num_minutes( ) %60 ) ;
86
+ let elapsedstr =
87
+ format ! ( "{}:{:0>2}" , elapsed. num_hours( ) , elapsed. num_minutes( ) % 60 ) ;
98
88
values. insert ( "elapsed" . into ( ) , Value :: text ( elapsedstr) ) ;
99
89
100
90
if let Some ( annotation) = tw. annotation {
@@ -124,44 +114,46 @@ pub async fn run(config: Config, mut api: CommonApi) -> Result<()> {
124
114
/// Raw output from timew
125
115
#[ derive( Deserialize , Debug ) ]
126
116
struct TimewarriorRAW {
127
- pub id : u32 ,
128
- pub start : String ,
129
- pub tags : Vec < String > ,
130
- pub annotation : Option < String > ,
131
- pub end : Option < String > ,
117
+ pub id : u32 ,
118
+ pub start : String ,
119
+ pub tags : Vec < String > ,
120
+ pub annotation : Option < String > ,
121
+ pub end : Option < String > ,
132
122
}
133
123
134
124
/// TimeWarrior entry
135
125
#[ derive( Debug , PartialEq ) ]
136
126
struct TimewarriorData {
137
- pub id : u32 ,
138
- pub start : DateTime < chrono:: offset:: Utc > ,
139
- pub tags : Vec < String > ,
140
- pub annotation : Option < String > ,
141
- pub end : Option < DateTime < chrono:: offset:: Utc > > ,
127
+ pub id : u32 ,
128
+ pub start : DateTime < chrono:: offset:: Utc > ,
129
+ pub tags : Vec < String > ,
130
+ pub annotation : Option < String > ,
131
+ pub end : Option < DateTime < chrono:: offset:: Utc > > ,
142
132
}
143
133
144
134
impl From < TimewarriorRAW > for TimewarriorData {
145
- fn from ( item : TimewarriorRAW ) -> Self {
135
+ fn from ( item : TimewarriorRAW ) -> Self {
146
136
Self {
147
137
id : item. id ,
148
138
tags : item. tags ,
149
139
annotation : item. annotation ,
150
- start : DateTime :: from_utc (
151
- chrono:: NaiveDateTime :: parse_from_str ( & item. start , "%Y%m%dT%H%M%SZ" )
152
- . unwrap ( ) ,
153
- chrono:: Utc ) ,
154
- end : item. end . map ( |v| DateTime :: from_utc (
155
- chrono:: NaiveDateTime :: parse_from_str ( & v, "%Y%m%dT%H%M%SZ" )
156
- . unwrap ( ) ,
157
- chrono:: Utc ) ) ,
140
+ start : DateTime :: from_utc (
141
+ chrono:: NaiveDateTime :: parse_from_str ( & item. start , "%Y%m%dT%H%M%SZ" ) . unwrap ( ) ,
142
+ chrono:: Utc ,
143
+ ) ,
144
+ end : item. end . map ( |v| {
145
+ DateTime :: from_utc (
146
+ chrono:: NaiveDateTime :: parse_from_str ( & v, "%Y%m%dT%H%M%SZ" ) . unwrap ( ) ,
147
+ chrono:: Utc ,
148
+ )
149
+ } ) ,
158
150
}
159
151
}
160
152
}
161
153
162
154
/// Format a DateTime given a format string
163
155
#[ allow( dead_code) ]
164
- fn format_datetime ( date : & DateTime < chrono:: Utc > , format : & str ) -> String {
156
+ fn format_datetime ( date : & DateTime < chrono:: Utc > , format : & str ) -> String {
165
157
date. format ( format) . to_string ( )
166
158
}
167
159
@@ -181,7 +173,7 @@ async fn call_timewarrior() -> Result<String> {
181
173
182
174
/// Stop or continue a task
183
175
async fn stop_continue ( ) -> Result < ( ) > {
184
- let mut execute_continue: bool = true ;
176
+ let mut execute_continue: bool = true ;
185
177
if let Some ( tw) = process_timewarrior_data ( & call_timewarrior ( ) . await ?) {
186
178
// we only execute continue if the current task is stopped
187
179
// i.e. has an end defined
@@ -205,10 +197,9 @@ async fn stop_continue() -> Result<()> {
205
197
. map ( |_| ( ) )
206
198
}
207
199
208
-
209
200
/// Process the output from "timew export" and return the first entry
210
- fn process_timewarrior_data ( input : & str ) -> Option < TimewarriorData > {
211
- let t : Vec < TimewarriorRAW > = serde_json:: from_str ( input) . unwrap_or_default ( ) ;
201
+ fn process_timewarrior_data ( input : & str ) -> Option < TimewarriorData > {
202
+ let t: Vec < TimewarriorRAW > = serde_json:: from_str ( input) . unwrap_or_default ( ) ;
212
203
match t. into_iter ( ) . next ( ) {
213
204
Some ( t) => Some ( TimewarriorData :: from ( t) ) ,
214
205
None => None ,
@@ -221,15 +212,9 @@ mod tests {
221
212
222
213
#[ test]
223
214
fn test_process_timewarrior_data ( ) {
224
- assert_eq ! (
225
- process_timewarrior_data( "" ) ,
226
- None ,
227
- ) ;
228
-
229
- assert_eq ! (
230
- process_timewarrior_data( "[]" ) ,
231
- None ,
232
- ) ;
215
+ assert_eq ! ( process_timewarrior_data( "" ) , None , ) ;
216
+
217
+ assert_eq ! ( process_timewarrior_data( "[]" ) , None , ) ;
233
218
234
219
let a = process_timewarrior_data ( "[{\" id\" :1,\" start\" :\" 20230131T175754Z\" ,\" tags\" :[\" i3status\" ],\" annotation\" :\" timewarrior plugin\" }]" ) ;
235
220
assert_eq ! ( a. is_some( ) , true ) ;
0 commit comments