11import "dart:math" ;
22
3+ import "package:protobuf/protobuf.dart" ;
34import "package:rover_dashboard/data.dart" ;
45
56export "package:protobuf/protobuf.dart" show GeneratedMessageGenericExtensions;
@@ -40,6 +41,16 @@ String getDataName(Device device) => switch (device) {
4041 _ => "Unknown" ,
4142};
4243
44+ /// Utilities for a list of Protobuf enums.
45+ extension UndefinedFilter <T extends ProtobufEnum > on List <T > {
46+ /// Filters out `_UNDEFINED` values from the list.
47+ List <T > get filtered => [
48+ for (final value in this )
49+ if (value.value != 0 )
50+ value,
51+ ];
52+ }
53+
4354/// Utilities for [Timestamp] s.
4455extension TimestampUtils on Timestamp {
4556 /// The [Timestamp] version of [DateTime.now] .
@@ -65,6 +76,7 @@ extension RoverStatusHumanName on RoverStatus {
6576 case RoverStatus .MANUAL : return "Manual" ;
6677 case RoverStatus .AUTONOMOUS : return "Autonomous" ;
6778 case RoverStatus .POWER_OFF : return "Off" ;
79+ case RoverStatus .RESTART : return "Restart" ;
6880 }
6981 // Do not use default or else you'll lose exhaustiveness checking.
7082 throw ArgumentError ("Unrecognized rover status: $this " );
@@ -131,7 +143,7 @@ extension DeviceUtils on Device {
131143 /// Gets a user-friendly name for a [Device] .
132144 String get humanName {
133145 switch (this ) {
134- case Device .DEVICE_UNDEFINED : return "" ;
146+ case Device .DEVICE_UNDEFINED : return "Unknown device " ;
135147 case Device .DASHBOARD : return "Dashboard" ;
136148 case Device .SUBSYSTEMS : return "Subsystems" ;
137149 case Device .VIDEO : return "Video" ;
@@ -252,3 +264,43 @@ extension MotorDirectionUtils on MotorDirection {
252264 throw ArgumentError ("Unrecognized MotorDirection: $this " );
253265 }
254266}
267+
268+ /// More human-friendly fields for [BurtLogLevel] s.
269+ extension LogLevelUtils on BurtLogLevel {
270+ /// The human-readable name of this level.
271+ String get humanName => switch (this ) {
272+ BurtLogLevel .critical => "Critical" ,
273+ BurtLogLevel .error => "Error" ,
274+ BurtLogLevel .warning => "Warning" ,
275+ BurtLogLevel .info => "Info" ,
276+ BurtLogLevel .debug => "Debug" ,
277+ BurtLogLevel .trace => "Trace" ,
278+ _ => "Unknown" ,
279+ };
280+
281+ /// The label to represent this log.
282+ String get label => switch (this ) {
283+ BurtLogLevel .critical => "[C]" ,
284+ BurtLogLevel .error => "[E]" ,
285+ BurtLogLevel .warning => "[W]" ,
286+ BurtLogLevel .info => "[I]" ,
287+ BurtLogLevel .debug => "[D]" ,
288+ BurtLogLevel .trace => "[T]" ,
289+ _ => "?" ,
290+ };
291+ }
292+
293+ /// Fomats [BurtLog] messages in plain-text. For the UI, use widgets.
294+ extension LogFormat on BurtLog {
295+ /// Fomats [BurtLog] messages in plain-text. For the UI, use widgets.
296+ String format () {
297+ final result = StringBuffer ()
298+ ..write (level.label)
299+ ..write (" " )
300+ ..write (title);
301+ if (body.isNotEmpty) {
302+ result..write ("\n " )..write (body);
303+ }
304+ return result.toString ();
305+ }
306+ }
0 commit comments