feat(app): artifact viewer shows CSV and TSV files as tables
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../ui/adaptive/host.dart';
|
||||
import '../domain/artifact_table.dart';
|
||||
|
||||
/// The truncation note for a capped table preview.
|
||||
String artifactTableNote(ArtifactTable table) =>
|
||||
'Showing ${table.rows.length} of ${table.totalRows} rows. Open the file '
|
||||
'for the rest.';
|
||||
|
||||
/// A CSV/TSV preview: the first row as the header, every cell selectable, and
|
||||
/// the whole grid scrolling horizontally inside the viewer's own vertical
|
||||
/// scroll. Short rows are padded with empty cells; nothing is invented.
|
||||
class ArtifactTableView extends StatelessWidget {
|
||||
const ArtifactTableView({
|
||||
super.key,
|
||||
required this.text,
|
||||
required this.delimiter,
|
||||
});
|
||||
|
||||
final String text;
|
||||
final String delimiter;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final table = parseArtifactTable(text, delimiter: delimiter);
|
||||
if (table.rows.isEmpty) {
|
||||
return const Padding(
|
||||
padding: EdgeInsets.all(16),
|
||||
child: Text('The table is empty.'),
|
||||
);
|
||||
}
|
||||
final cupertino = usesCupertinoHost(context);
|
||||
final border = cupertino
|
||||
? CupertinoColors.separator.resolveFrom(context)
|
||||
: Theme.of(context).dividerColor;
|
||||
final header = cupertino
|
||||
? CupertinoColors.secondarySystemFill.resolveFrom(context)
|
||||
: Theme.of(context).colorScheme.surfaceContainerHighest;
|
||||
final columns = table.columnCount;
|
||||
Widget cell(String value, {bool head = false}) => Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 6),
|
||||
child: SelectableText(
|
||||
value,
|
||||
style: head ? const TextStyle(fontWeight: FontWeight.w600) : null,
|
||||
),
|
||||
);
|
||||
return Padding(
|
||||
padding: const EdgeInsetsDirectional.fromSTEB(16, 8, 16, 16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
if (table.truncated)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(bottom: 8),
|
||||
child: Text(artifactTableNote(table)),
|
||||
),
|
||||
SingleChildScrollView(
|
||||
key: const Key('artifact-table'),
|
||||
scrollDirection: Axis.horizontal,
|
||||
child: Table(
|
||||
defaultColumnWidth: const IntrinsicColumnWidth(),
|
||||
border: TableBorder.all(color: border),
|
||||
children: [
|
||||
for (var r = 0; r < table.rows.length; r++)
|
||||
TableRow(
|
||||
decoration: r == 0 ? BoxDecoration(color: header) : null,
|
||||
children: [
|
||||
for (var c = 0; c < columns; c++)
|
||||
cell(
|
||||
c < table.rows[r].length ? table.rows[r][c] : '',
|
||||
head: r == 0,
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import 'dart:typed_data';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../domain/artifact_preview.dart';
|
||||
import 'artifact_table_view.dart';
|
||||
import 'artifact_video_player.dart';
|
||||
import '../../../ui/text/monospace.dart';
|
||||
|
||||
@@ -79,6 +80,8 @@ class ArtifactPreviewBody extends StatelessWidget {
|
||||
);
|
||||
case ArtifactPreviewKind.markdown:
|
||||
return _PreviewPadding(child: markdownBuilder(context, text ?? ''));
|
||||
case ArtifactPreviewKind.table:
|
||||
return ArtifactTableView(text: text ?? '', delimiter: plan.delimiter);
|
||||
case ArtifactPreviewKind.video:
|
||||
return ArtifactVideoPlayer(
|
||||
uri: plan.resolved!.uri,
|
||||
|
||||
Reference in New Issue
Block a user