@@ -8,17 +8,10 @@ use ratatui::{
8
8
text:: Span ,
9
9
widgets:: Paragraph ,
10
10
} ;
11
+ use unicode_width:: UnicodeWidthStr ;
11
12
12
13
use crate :: display:: { DisplayBandwidth , UIState } ;
13
14
14
- const SECONDS_IN_DAY : u64 = 86400 ;
15
-
16
- pub struct HeaderDetails < ' a > {
17
- pub state : & ' a UIState ,
18
- pub elapsed_time : std:: time:: Duration ,
19
- pub paused : bool ,
20
- }
21
-
22
15
pub fn elapsed_time ( last_start_time : Instant , cumulative_time : Duration , paused : bool ) -> Duration {
23
16
if paused {
24
17
cumulative_time
@@ -27,27 +20,46 @@ pub fn elapsed_time(last_start_time: Instant, cumulative_time: Duration, paused:
27
20
}
28
21
}
29
22
23
+ fn format_duration ( d : Duration ) -> String {
24
+ let s = d. as_secs ( ) ;
25
+ let days = match s / 86400 {
26
+ 0 => "" . to_string ( ) ,
27
+ 1 => "1 day, " . to_string ( ) ,
28
+ n => format ! ( "{n} days, " ) ,
29
+ } ;
30
+ format ! (
31
+ "{days}{:02}:{:02}:{:02}" ,
32
+ ( s / 3600 ) % 24 ,
33
+ ( s / 60 ) % 60 ,
34
+ s % 60 ,
35
+ )
36
+ }
37
+
38
+ pub struct HeaderDetails < ' a > {
39
+ pub state : & ' a UIState ,
40
+ pub elapsed_time : Duration ,
41
+ pub paused : bool ,
42
+ }
43
+
30
44
impl < ' a > HeaderDetails < ' a > {
31
- #[ allow( clippy:: int_plus_one) ]
32
45
pub fn render ( & self , frame : & mut Frame < impl Backend > , rect : Rect ) {
33
46
let bandwidth = self . bandwidth_string ( ) ;
34
- let mut elapsed_time = None ;
35
- let print_elapsed_time = if self . state . cumulative_mode {
36
- elapsed_time = Some ( self . elapsed_time_string ( ) ) ;
37
- bandwidth. len ( ) + elapsed_time. as_ref ( ) . unwrap ( ) . len ( ) + 1 <= rect. width as usize
38
- } else {
39
- false
40
- } ;
41
-
42
47
let color = if self . paused {
43
48
Color :: Yellow
44
49
} else {
45
50
Color :: Green
46
51
} ;
47
52
48
- if print_elapsed_time {
49
- self . render_elapsed_time ( frame, rect, elapsed_time. as_ref ( ) . unwrap ( ) , color) ;
53
+ // do not render time in tests, otherwise the output becomes non-deterministic
54
+ // see: https://github.com/imsnif/bandwhich/issues/303
55
+ if cfg ! ( not( test) ) && self . state . cumulative_mode {
56
+ let elapsed_time = format_duration ( self . elapsed_time ) ;
57
+ // only render if there is enough width
58
+ if bandwidth. width ( ) + 1 + elapsed_time. width ( ) <= rect. width as usize {
59
+ self . render_elapsed_time ( frame, rect, & elapsed_time, color) ;
60
+ }
50
61
}
62
+
51
63
self . render_bandwidth ( frame, rect, & bandwidth, color) ;
52
64
}
53
65
@@ -97,22 +109,4 @@ impl<'a> HeaderDetails<'a> {
97
109
let paragraph = Paragraph :: new ( elapsed_time_text) . alignment ( Alignment :: Right ) ;
98
110
frame. render_widget ( paragraph, rect) ;
99
111
}
100
-
101
- fn days_string ( & self ) -> String {
102
- match self . elapsed_time . as_secs ( ) / SECONDS_IN_DAY {
103
- 0 => "" . to_string ( ) ,
104
- 1 => "1 day, " . to_string ( ) ,
105
- n => format ! ( "{n} days, " ) ,
106
- }
107
- }
108
-
109
- fn elapsed_time_string ( & self ) -> String {
110
- format ! (
111
- "{}{:02}:{:02}:{:02} " ,
112
- self . days_string( ) ,
113
- ( self . elapsed_time. as_secs( ) % SECONDS_IN_DAY ) / 3600 ,
114
- ( self . elapsed_time. as_secs( ) % 3600 ) / 60 ,
115
- self . elapsed_time. as_secs( ) % 60
116
- )
117
- }
118
112
}
0 commit comments