3131//! - `tasks`
3232
3333use super :: prelude:: * ;
34- use tokio:: process:: Command ;
3534use chrono:: DateTime ;
35+ use tokio:: process:: Command ;
3636
3737#[ derive( Deserialize , Debug , SmartDefault ) ]
3838#[ serde( default ) ]
3939pub struct Config {
4040 #[ default( 30 . into( ) ) ]
41- interval : Seconds ,
41+ interval : Seconds ,
4242 format : FormatConfig ,
4343
4444 info : Option < u64 > ,
@@ -48,18 +48,10 @@ pub struct Config {
4848}
4949
5050pub async fn run ( config : Config , mut api : CommonApi ) -> Result < ( ) > {
51+ api. set_default_actions ( & [ ( MouseButton :: Left , None , "stop_continue" ) ] )
52+ . await ?;
5153
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|}" ) ?) ;
6355
6456 loop {
6557 let mut values = map ! {
@@ -80,7 +72,6 @@ pub async fn run(config: Config, mut api: CommonApi) -> Result<()> {
8072 ( & config. warning , State :: Warning ) ,
8173 ( & config. good , State :: Good ) ,
8274 ( & config. info , State :: Info ) ,
83-
8475 ] {
8576 if let Some ( value) = level {
8677 if ( elapsed. num_minutes ( ) as u64 ) >= * value {
@@ -92,9 +83,8 @@ pub async fn run(config: Config, mut api: CommonApi) -> Result<()> {
9283
9384 values. insert ( "tags" . into ( ) , Value :: text ( tw. tags . join ( " " ) ) ) ;
9485
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 ) ;
9888 values. insert ( "elapsed" . into ( ) , Value :: text ( elapsedstr) ) ;
9989
10090 if let Some ( annotation) = tw. annotation {
@@ -124,44 +114,46 @@ pub async fn run(config: Config, mut api: CommonApi) -> Result<()> {
124114/// Raw output from timew
125115#[ derive( Deserialize , Debug ) ]
126116struct 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 > ,
132122}
133123
134124/// TimeWarrior entry
135125#[ derive( Debug , PartialEq ) ]
136126struct 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 > > ,
142132}
143133
144134impl From < TimewarriorRAW > for TimewarriorData {
145- fn from ( item : TimewarriorRAW ) -> Self {
135+ fn from ( item : TimewarriorRAW ) -> Self {
146136 Self {
147137 id : item. id ,
148138 tags : item. tags ,
149139 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+ } ) ,
158150 }
159151 }
160152}
161153
162154/// Format a DateTime given a format string
163155#[ 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 {
165157 date. format ( format) . to_string ( )
166158}
167159
@@ -181,7 +173,7 @@ async fn call_timewarrior() -> Result<String> {
181173
182174/// Stop or continue a task
183175async fn stop_continue ( ) -> Result < ( ) > {
184- let mut execute_continue: bool = true ;
176+ let mut execute_continue: bool = true ;
185177 if let Some ( tw) = process_timewarrior_data ( & call_timewarrior ( ) . await ?) {
186178 // we only execute continue if the current task is stopped
187179 // i.e. has an end defined
@@ -205,10 +197,9 @@ async fn stop_continue() -> Result<()> {
205197 . map ( |_| ( ) )
206198}
207199
208-
209200/// 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 ( ) ;
212203 match t. into_iter ( ) . next ( ) {
213204 Some ( t) => Some ( TimewarriorData :: from ( t) ) ,
214205 None => None ,
@@ -221,15 +212,9 @@ mod tests {
221212
222213 #[ test]
223214 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 , ) ;
233218
234219 let a = process_timewarrior_data ( "[{\" id\" :1,\" start\" :\" 20230131T175754Z\" ,\" tags\" :[\" i3status\" ],\" annotation\" :\" timewarrior plugin\" }]" ) ;
235220 assert_eq ! ( a. is_some( ) , true ) ;
0 commit comments