merge(app): land diff colours

This commit is contained in:
2026-10-23 18:36:03 +01:00
6 changed files with 92 additions and 14 deletions
@@ -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);
@@ -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'(?<![\w-])([+-]\d+)(?![\w])');
/// [text] with every `+N` in green and `-N` in red; the rest unchanged.
class ChangeStatText extends StatelessWidget {
const ChangeStatText(this.text, {super.key, this.style});
final String text;
final TextStyle? style;
@override
Widget build(BuildContext context) {
final spans = <TextSpan>[];
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;
@@ -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<ChangesViewer> {
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
ChangeStatText(
runChangesHeader(run),
style: Theme.of(context).textTheme.titleMedium,
),
@@ -114,7 +114,8 @@ class _FileDiffViewerState extends State<FileDiffViewer> {
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(' · '),
@@ -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<RichText>(find.byType(RichText)).text;
final spans = <String, Color?>{};
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);
});
}
@@ -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);
}
},