@@ -11,7 +11,10 @@ use std::{
11
11
sync:: Arc ,
12
12
time:: { Duration , SystemTime } ,
13
13
} ;
14
- use tui:: text:: Span ;
14
+ use tui:: {
15
+ style:: { Color , Modifier } ,
16
+ text:: Span ,
17
+ } ;
15
18
16
19
#[ derive( Default , Debug ) ]
17
20
pub ( crate ) struct State {
@@ -122,7 +125,7 @@ impl State {
122
125
self . new_tasks . drain ( ..)
123
126
}
124
127
125
- pub ( crate ) fn update_tasks ( & mut self , update : proto:: tasks:: TaskUpdate ) {
128
+ pub ( crate ) fn update_tasks ( & mut self , styles : & view :: Styles , update : proto:: tasks:: TaskUpdate ) {
126
129
if let Some ( now) = update. now {
127
130
self . last_updated_at = Some ( now. try_into ( ) . unwrap ( ) ) ;
128
131
}
@@ -160,21 +163,13 @@ impl State {
160
163
return None ;
161
164
}
162
165
} ;
163
- let fields = task
166
+ let mut fields = task
164
167
. fields
165
168
. drain ( ..)
166
169
. filter_map ( |pb| Field :: from_proto ( pb, meta) )
167
170
. collect :: < Vec < _ > > ( ) ;
168
171
169
- let formatted_fields = fields. iter ( ) . fold ( Vec :: default ( ) , |mut acc, f| {
170
- acc. push ( vec ! [
171
- view:: bold( f. name. to_string( ) ) ,
172
- Span :: from( "=" ) ,
173
- Span :: from( format!( "{} " , f. value) ) ,
174
- ] ) ;
175
- acc
176
- } ) ;
177
-
172
+ let formatted_fields = Field :: make_formatted ( styles, & mut fields) ;
178
173
let id = task. id ?. id ;
179
174
let stats = stats_update. remove ( & id) ?. into ( ) ;
180
175
let mut task = Task {
@@ -459,6 +454,9 @@ impl TryFrom<usize> for SortBy {
459
454
// === impl Field ===
460
455
461
456
impl Field {
457
+ const SPAWN_LOCATION : & ' static str = "spawn.location" ;
458
+ const NAME : & ' static str = "name" ;
459
+
462
460
/// Converts a wire-format `Field` into an internal `Field` representation,
463
461
/// using the provided `Metadata` for the task span that the field came
464
462
/// from.
@@ -521,12 +519,59 @@ impl Field {
521
519
// if the value is an empty string, just skip it.
522
520
. ensure_nonempty ( ) ?;
523
521
524
- if & * name == "spawn.location" {
522
+ if & * name == Field :: SPAWN_LOCATION {
525
523
value = value. truncate_registry_path ( ) ;
526
524
}
527
525
528
526
Some ( Self { name, value } )
529
527
}
528
+
529
+ fn make_formatted ( styles : & view:: Styles , fields : & mut Vec < Field > ) -> Vec < Vec < Span < ' static > > > {
530
+ use std:: cmp:: Ordering ;
531
+
532
+ let key_style = styles. fg ( Color :: LightBlue ) . add_modifier ( Modifier :: BOLD ) ;
533
+ let delim_style = styles. fg ( Color :: LightBlue ) . add_modifier ( Modifier :: DIM ) ;
534
+ let val_style = styles. fg ( Color :: Yellow ) ;
535
+
536
+ fields. sort_unstable_by ( |left, right| {
537
+ if & * left. name == Field :: NAME {
538
+ return Ordering :: Less ;
539
+ }
540
+
541
+ if & * right. name == Field :: NAME {
542
+ return Ordering :: Greater ;
543
+ }
544
+
545
+ if & * left. name == Field :: SPAWN_LOCATION {
546
+ return Ordering :: Greater ;
547
+ }
548
+
549
+ if & * right. name == Field :: SPAWN_LOCATION {
550
+ return Ordering :: Less ;
551
+ }
552
+
553
+ left. name . cmp ( & right. name )
554
+ } ) ;
555
+
556
+ let mut formatted = Vec :: with_capacity ( fields. len ( ) ) ;
557
+ let mut fields = fields. iter ( ) ;
558
+ if let Some ( field) = fields. next ( ) {
559
+ formatted. push ( vec ! [
560
+ Span :: styled( field. name. to_string( ) , key_style) ,
561
+ Span :: styled( "=" , delim_style) ,
562
+ Span :: styled( field. value. to_string( ) , val_style) ,
563
+ ] ) ;
564
+ for field in fields {
565
+ formatted. push ( vec ! [
566
+ Span :: styled( ", " , delim_style) ,
567
+ Span :: styled( field. name. to_string( ) , key_style) ,
568
+ Span :: styled( "=" , delim_style) ,
569
+ Span :: styled( field. value. to_string( ) , val_style) ,
570
+ ] )
571
+ }
572
+ }
573
+ formatted
574
+ }
530
575
}
531
576
532
577
// === impl FieldValue ===
@@ -586,6 +631,7 @@ impl TaskState {
586
631
const COMPLETED_UTF8 : & str = "\u{23F9} " ;
587
632
match self {
588
633
Self :: Running => styles. if_utf8 ( RUNNING_UTF8 , ">" ) ,
634
+
589
635
Self :: Idle => styles. if_utf8 ( IDLE_UTF8 , ":" ) ,
590
636
Self :: Completed => styles. if_utf8 ( COMPLETED_UTF8 , "!" ) ,
591
637
}
0 commit comments