diff --git a/app/lib/features/changes/widgets/capture_section.dart b/app/lib/features/changes/widgets/capture_section.dart index 8d096497..7f6e3eb2 100644 --- a/app/lib/features/changes/widgets/capture_section.dart +++ b/app/lib/features/changes/widgets/capture_section.dart @@ -118,7 +118,7 @@ class ChangeFileRow extends StatelessWidget { final cupertino = usesCupertino(context); final leading = Icon(_kindIcon(context, file.kind)); final title = Text(file.path, style: mono); - final subtitle = Text(changeFileMeta(file)); + final subtitle = ChangeStatText(changeFileMeta(file)); final trailing = cupertino ? const CupertinoListTileChevron() : const Icon(Icons.chevron_right); diff --git a/app/lib/features/changes/widgets/change_style.dart b/app/lib/features/changes/widgets/change_style.dart index 2bb6cdab..60a3690f 100644 --- a/app/lib/features/changes/widgets/change_style.dart +++ b/app/lib/features/changes/widgets/change_style.dart @@ -22,20 +22,64 @@ ThemeData hostTextTheme(BuildContext context) => ThemeData( ChangeLineKind kind, ) => switch (kind) { ChangeLineKind.add => ( - text: usesCupertino(context) - ? CupertinoColors.systemGreen.resolveFrom(context) - : Theme.of(context).colorScheme.primary, - background: null, + text: addedColor(context), + background: addedColor(context).withValues(alpha: 0.12), ), ChangeLineKind.remove => ( - text: usesCupertino(context) - ? CupertinoColors.destructiveRed.resolveFrom(context) - : Theme.of(context).colorScheme.error, - background: null, + text: removedColor(context), + background: removedColor(context).withValues(alpha: 0.12), ), ChangeLineKind.context => (text: labelColor(context), background: null), }; +/// Added lines and counts: the platform's green, readable in both themes. +Color addedColor(BuildContext context) => usesCupertino(context) + ? CupertinoColors.systemGreen.resolveFrom(context) + : (Theme.of(context).brightness == Brightness.dark + ? const Color(0xFF81C995) + : const Color(0xFF1E8E3E)); + +/// Removed lines and counts: the platform's red. +Color removedColor(BuildContext context) => usesCupertino(context) + ? CupertinoColors.systemRed.resolveFrom(context) + : Theme.of(context).colorScheme.error; + +final _statToken = RegExp(r'(?[]; + var at = 0; + for (final match in _statToken.allMatches(text)) { + if (match.start > at) { + spans.add(TextSpan(text: text.substring(at, match.start))); + } + final token = match.group(1)!; + spans.add( + TextSpan( + text: token, + style: TextStyle( + color: token.startsWith('+') + ? addedColor(context) + : removedColor(context), + fontWeight: FontWeight.w600, + ), + ), + ); + at = match.end; + } + if (at < text.length) spans.add(TextSpan(text: text.substring(at))); + return Text.rich(TextSpan(children: spans), style: style); + } +} + Color labelColor(BuildContext context) => usesCupertino(context) ? CupertinoColors.label.resolveFrom(context) : Theme.of(context).colorScheme.onSurface; diff --git a/app/lib/features/changes/widgets/changes_viewer.dart b/app/lib/features/changes/widgets/changes_viewer.dart index 77520636..bfadc478 100644 --- a/app/lib/features/changes/widgets/changes_viewer.dart +++ b/app/lib/features/changes/widgets/changes_viewer.dart @@ -6,6 +6,7 @@ import '../domain/change_wording.dart'; import '../domain/file_change.dart'; import '../domain/run_changes.dart'; import 'capture_section.dart'; +import 'change_style.dart'; import 'file_diff_viewer.dart'; import 'visit_pager.dart'; @@ -112,7 +113,7 @@ class _ChangesViewerState extends State { child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ - Text( + ChangeStatText( runChangesHeader(run), style: Theme.of(context).textTheme.titleMedium, ), diff --git a/app/lib/features/changes/widgets/file_diff_viewer.dart b/app/lib/features/changes/widgets/file_diff_viewer.dart index bbbae7e2..bfbfdd51 100644 --- a/app/lib/features/changes/widgets/file_diff_viewer.dart +++ b/app/lib/features/changes/widgets/file_diff_viewer.dart @@ -114,7 +114,8 @@ class _FileDiffViewerState extends State { changeKindTitle(file.kind), style: Theme.of(context).textTheme.titleMedium, ), - if (stat.isNotEmpty) Text(stat, style: mono), + if (stat.isNotEmpty) + ChangeStatText(stat, style: mono), if (tags.isNotEmpty) Text( tags.join(' · '), diff --git a/app/test/features/changes/change_stat_text_test.dart b/app/test/features/changes/change_stat_text_test.dart new file mode 100644 index 00000000..977c0420 --- /dev/null +++ b/app/test/features/changes/change_stat_text_test.dart @@ -0,0 +1,31 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/features/changes/widgets/change_style.dart'; + +void main() { + testWidgets('+N is green and -N red; other words keep the text colour', ( + tester, + ) async { + await tester.pumpWidget( + const MaterialApp( + home: Scaffold( + body: ChangeStatText('2 files changed · +12 -3 · run-4 kept'), + ), + ), + ); + final rich = tester.widget(find.byType(RichText)).text; + final spans = {}; + rich.visitChildren((span) { + if (span is TextSpan && span.text != null) { + spans[span.text!] = span.style?.color; + } + return true; + }); + final context = tester.element(find.byType(ChangeStatText)); + expect(spans['+12'], addedColor(context)); + expect(spans['-3'], removedColor(context)); + // A hyphenated word is not a count. + expect(spans.keys.where((key) => key.contains('run-4')), isNotEmpty); + expect(spans['-4'], isNull); + }); +} diff --git a/app/test/features/changes/diff_presentation_test.dart b/app/test/features/changes/diff_presentation_test.dart index cf131ce0..7971e200 100644 --- a/app/test/features/changes/diff_presentation_test.dart +++ b/app/test/features/changes/diff_presentation_test.dart @@ -94,10 +94,11 @@ void main() { final context = tester.element(find.byType(Scaffold)); final add = diffLineColors(context, ChangeLineKind.add); final remove = diffLineColors(context, ChangeLineKind.remove); - expect(add.background, isNull); - expect(remove.background, isNull); + // Additions are green and removals red, each line on a light tint. + expect(add.background, isNotNull); + expect(remove.background, isNotNull); expect(add.text, isNot(remove.text), reason: '$brightness text'); - expect(add.text, Theme.of(context).colorScheme.primary); + expect(add.text.g, greaterThan(add.text.r), reason: 'green addition'); expect(remove.text, Theme.of(context).colorScheme.error); } },