import '../../../core/api_gateway.dart'; import 'run_models.dart' show runStateOf; /// The longest result preview a succeeded run's row shows; a longer one is /// left to the run's detail rather than cut into a misleading fragment. const int runResultPreviewRowLimit = 140; /// Why a run ended, in one line for its list row, with the full recorded text /// for a tooltip. [failed] says the line explains a failure (drawn in the /// error colour); otherwise it is a succeeded run's short result. typedef RunOutcomeLine = ({String line, String full, bool failed}); /// The row's outcome line, or null when the run recorded nothing to show. /// /// A failed run shows its failure reason, else its diagnostic's first /// non-blank line. A succeeded run shows its result preview only when it is /// short (at most [runResultPreviewRowLimit] characters on one line). RunOutcomeLine? runOutcomeLine(JsonMap run) { final state = runStateOf(run['status']); if (state == 'failed' && run['executing'] != true) { final reason = '${run['failure_reason'] ?? ''}'.trim(); final diagnostic = '${run['diagnostic'] ?? ''}'.trim(); final full = reason.isNotEmpty ? reason : diagnostic; if (full.isEmpty) return null; return (line: firstLine(full), full: full, failed: true); } if (state == 'succeeded') { final preview = '${run['result_preview'] ?? ''}'.trim(); if (preview.isEmpty || preview.contains('\n') || preview.length > runResultPreviewRowLimit) { return null; } return (line: preview, full: preview, failed: false); } return null; } /// The first non-blank line of [text], trimmed. String firstLine(String text) => text .split('\n') .map((line) => line.trim()) .firstWhere((line) => line.isNotEmpty, orElse: () => '');