@@ -7,6 +7,7 @@ use crate::{
7
7
use console_api as proto;
8
8
use std:: {
9
9
cell:: RefCell ,
10
+ cmp:: Ordering ,
10
11
collections:: HashMap ,
11
12
convert:: { TryFrom , TryInto } ,
12
13
fmt,
@@ -51,13 +52,13 @@ pub(crate) struct Metadata {
51
52
//TODO: add more metadata as needed
52
53
}
53
54
54
- #[ derive( Debug ) ]
55
+ #[ derive( Debug , Eq , PartialEq ) ]
55
56
pub ( crate ) struct Field {
56
57
pub ( crate ) name : InternedStr ,
57
58
pub ( crate ) value : FieldValue ,
58
59
}
59
60
60
- #[ derive( Debug ) ]
61
+ #[ derive( Debug , Eq , PartialEq , Ord , PartialOrd ) ]
61
62
pub ( crate ) enum FieldValue {
62
63
Bool ( bool ) ,
63
64
Str ( String ) ,
@@ -72,7 +73,7 @@ enum Temporality {
72
73
Paused ,
73
74
}
74
75
75
- #[ derive( Debug ) ]
76
+ #[ derive( Debug , Eq , PartialEq ) ]
76
77
pub ( crate ) struct Attribute {
77
78
field : Field ,
78
79
unit : Option < String > ,
@@ -333,31 +334,11 @@ impl Field {
333
334
}
334
335
335
336
fn make_formatted ( styles : & view:: Styles , fields : & mut Vec < Field > ) -> Vec < Vec < Span < ' static > > > {
336
- use std:: cmp:: Ordering ;
337
-
338
337
let key_style = styles. fg ( Color :: LightBlue ) . add_modifier ( Modifier :: BOLD ) ;
339
338
let delim_style = styles. fg ( Color :: LightBlue ) . add_modifier ( Modifier :: DIM ) ;
340
339
let val_style = styles. fg ( Color :: Yellow ) ;
341
340
342
- fields. sort_unstable_by ( |left, right| {
343
- if & * left. name == Field :: NAME {
344
- return Ordering :: Less ;
345
- }
346
-
347
- if & * right. name == Field :: NAME {
348
- return Ordering :: Greater ;
349
- }
350
-
351
- if & * left. name == Field :: SPAWN_LOCATION {
352
- return Ordering :: Greater ;
353
- }
354
-
355
- if & * right. name == Field :: SPAWN_LOCATION {
356
- return Ordering :: Less ;
357
- }
358
-
359
- left. name . cmp ( & right. name )
360
- } ) ;
341
+ fields. sort_unstable ( ) ;
361
342
362
343
let mut formatted = Vec :: with_capacity ( fields. len ( ) ) ;
363
344
let mut fields = fields. iter ( ) ;
@@ -380,6 +361,29 @@ impl Field {
380
361
}
381
362
}
382
363
364
+ impl Ord for Field {
365
+ fn cmp ( & self , other : & Self ) -> Ordering {
366
+ match ( & * self . name , & * other. name ) {
367
+ // the `NAME` field should always come first
368
+ ( Field :: NAME , Field :: NAME ) => Ordering :: Equal ,
369
+ ( Field :: NAME , _) => Ordering :: Less ,
370
+ ( _, Field :: NAME ) => Ordering :: Greater ,
371
+
372
+ // the `SPAWN_LOCATION` field should always come last (it's long)
373
+ ( Field :: SPAWN_LOCATION , Field :: SPAWN_LOCATION ) => Ordering :: Equal ,
374
+ ( Field :: SPAWN_LOCATION , _) => Ordering :: Greater ,
375
+ ( _, Field :: SPAWN_LOCATION ) => Ordering :: Less ,
376
+ ( this, that) => this. cmp ( that) ,
377
+ }
378
+ }
379
+ }
380
+
381
+ impl PartialOrd for Field {
382
+ fn partial_cmp ( & self , other : & Self ) -> Option < Ordering > {
383
+ Some ( self . cmp ( other) )
384
+ }
385
+ }
386
+
383
387
// === impl FieldValue ===
384
388
385
389
impl fmt:: Display for FieldValue {
@@ -417,6 +421,38 @@ impl FieldValue {
417
421
}
418
422
}
419
423
424
+ impl From < proto:: field:: Value > for FieldValue {
425
+ fn from ( pb : proto:: field:: Value ) -> Self {
426
+ match pb {
427
+ proto:: field:: Value :: BoolVal ( v) => Self :: Bool ( v) ,
428
+ proto:: field:: Value :: StrVal ( v) => Self :: Str ( v) ,
429
+ proto:: field:: Value :: I64Val ( v) => Self :: I64 ( v) ,
430
+ proto:: field:: Value :: U64Val ( v) => Self :: U64 ( v) ,
431
+ proto:: field:: Value :: DebugVal ( v) => Self :: Debug ( v) ,
432
+ }
433
+ }
434
+ }
435
+
436
+ impl Ord for Attribute {
437
+ fn cmp ( & self , other : & Self ) -> Ordering {
438
+ self . field
439
+ . cmp ( & other. field )
440
+ // TODO(eliza): *maybe* this should compare so that larger units are
441
+ // greater than smaller units (e.g. `ms` > `us`), rather than
442
+ // alphabetically?
443
+ // but, meh...
444
+ . then_with ( || self . unit . cmp ( & other. unit ) )
445
+ }
446
+ }
447
+
448
+ impl PartialOrd for Attribute {
449
+ fn partial_cmp ( & self , other : & Self ) -> Option < Ordering > {
450
+ Some ( self . cmp ( other) )
451
+ }
452
+ }
453
+
454
+ // === impl Attribute ===
455
+
420
456
impl Attribute {
421
457
fn make_formatted (
422
458
styles : & view:: Styles ,
@@ -427,6 +463,8 @@ impl Attribute {
427
463
let val_style = styles. fg ( Color :: Yellow ) ;
428
464
let unit_style = styles. fg ( Color :: LightBlue ) ;
429
465
466
+ attributes. sort_unstable ( ) ;
467
+
430
468
let mut formatted = Vec :: with_capacity ( attributes. len ( ) ) ;
431
469
let attributes = attributes. iter ( ) ;
432
470
for attr in attributes {
@@ -446,18 +484,6 @@ impl Attribute {
446
484
}
447
485
}
448
486
449
- impl From < proto:: field:: Value > for FieldValue {
450
- fn from ( pb : proto:: field:: Value ) -> Self {
451
- match pb {
452
- proto:: field:: Value :: BoolVal ( v) => Self :: Bool ( v) ,
453
- proto:: field:: Value :: StrVal ( v) => Self :: Str ( v) ,
454
- proto:: field:: Value :: I64Val ( v) => Self :: I64 ( v) ,
455
- proto:: field:: Value :: U64Val ( v) => Self :: U64 ( v) ,
456
- proto:: field:: Value :: DebugVal ( v) => Self :: Debug ( v) ,
457
- }
458
- }
459
- }
460
-
461
487
fn truncate_registry_path ( s : String ) -> String {
462
488
use once_cell:: sync:: OnceCell ;
463
489
use regex:: Regex ;
0 commit comments