feat(app): artifact viewer shows CSV and TSV files as tables
This commit is contained in:
@@ -198,12 +198,12 @@ bool isMarkdownArtifact(String contentType, String name) {
|
||||
|
||||
/// How the viewer presents an artifact in-app, or [none] when it only offers
|
||||
/// Open/Download.
|
||||
enum ArtifactPreviewFormat { image, video, markdown, text, none }
|
||||
enum ArtifactPreviewFormat { image, video, markdown, text, table, none }
|
||||
|
||||
/// The one rule for in-app previews, shared by the gallery's paging and the
|
||||
/// viewer's fetch plan. Images and videos come from the content type; markdown
|
||||
/// from `text/markdown` or an md/markdown name; plain text, logs, JSON and
|
||||
/// source/config files are shown as selectable monospace text.
|
||||
/// from `text/markdown` or an md/markdown name; CSV/TSV as a table; plain
|
||||
/// text, logs, JSON and source/config files as selectable monospace text.
|
||||
ArtifactPreviewFormat artifactPreviewFormat(String contentType, String name) {
|
||||
if (isImageContentType(contentType)) return ArtifactPreviewFormat.image;
|
||||
if (isVideoContentType(contentType)) return ArtifactPreviewFormat.video;
|
||||
@@ -212,6 +212,12 @@ ArtifactPreviewFormat artifactPreviewFormat(String contentType, String name) {
|
||||
if (mime == 'text/markdown' || ext == 'md' || ext == 'markdown') {
|
||||
return ArtifactPreviewFormat.markdown;
|
||||
}
|
||||
if (mime == 'text/csv' ||
|
||||
mime == 'text/tab-separated-values' ||
|
||||
ext == 'csv' ||
|
||||
ext == 'tsv') {
|
||||
return ArtifactPreviewFormat.table;
|
||||
}
|
||||
final kind = artifactKind(contentType, name);
|
||||
if (kind == ArtifactKind.json ||
|
||||
kind == ArtifactKind.log ||
|
||||
@@ -224,6 +230,12 @@ ArtifactPreviewFormat artifactPreviewFormat(String contentType, String name) {
|
||||
return ArtifactPreviewFormat.none;
|
||||
}
|
||||
|
||||
/// Whether a delimited artifact is tab-separated rather than comma-separated.
|
||||
bool isTabSeparatedArtifact(String contentType, String name) =>
|
||||
contentType.split(';').first.trim().toLowerCase() ==
|
||||
'text/tab-separated-values' ||
|
||||
_extensionOf(name) == 'tsv';
|
||||
|
||||
/// Whether an artifact opens inside the viewer (see [artifactPreviewFormat]).
|
||||
bool isViewerArtifact(String contentType, String name) =>
|
||||
artifactPreviewFormat(contentType, name) != ArtifactPreviewFormat.none;
|
||||
|
||||
@@ -12,7 +12,10 @@ enum ArtifactPreviewKind {
|
||||
/// fetched as bytes, never autoplayed), as the web viewer does.
|
||||
video,
|
||||
|
||||
/// A recorded file the viewer cannot draw (CSV, archives, ...).
|
||||
/// A CSV/TSV drawn as a table from its fetched text.
|
||||
table,
|
||||
|
||||
/// A recorded file the viewer cannot draw (archives, PDFs, ...).
|
||||
unsupported,
|
||||
|
||||
/// No safe same-origin url to fetch: absent, unsafe or another origin.
|
||||
@@ -31,6 +34,7 @@ class ArtifactPreviewPlan {
|
||||
required this.kind,
|
||||
required this.resolved,
|
||||
required this.message,
|
||||
this.delimiter = ',',
|
||||
});
|
||||
|
||||
final ArtifactPreviewKind kind;
|
||||
@@ -40,12 +44,16 @@ class ArtifactPreviewPlan {
|
||||
/// image, text or markdown document that may be fetched.
|
||||
final String message;
|
||||
|
||||
/// The field separator of a [ArtifactPreviewKind.table] preview.
|
||||
final String delimiter;
|
||||
|
||||
bool get canFetch =>
|
||||
resolved != null &&
|
||||
resolved!.external == false &&
|
||||
(kind == ArtifactPreviewKind.image ||
|
||||
kind == ArtifactPreviewKind.text ||
|
||||
kind == ArtifactPreviewKind.markdown);
|
||||
kind == ArtifactPreviewKind.markdown ||
|
||||
kind == ArtifactPreviewKind.table);
|
||||
}
|
||||
|
||||
const String artifactPreviewNoUrl = 'No artifact URL recorded.';
|
||||
@@ -94,10 +102,18 @@ ArtifactPreviewPlan planArtifactPreview({
|
||||
ArtifactPreviewFormat.video => ArtifactPreviewKind.video,
|
||||
ArtifactPreviewFormat.markdown => ArtifactPreviewKind.markdown,
|
||||
ArtifactPreviewFormat.text => ArtifactPreviewKind.text,
|
||||
ArtifactPreviewFormat.table => ArtifactPreviewKind.table,
|
||||
ArtifactPreviewFormat.none => null,
|
||||
};
|
||||
if (kind != null) {
|
||||
return ArtifactPreviewPlan(kind: kind, resolved: resolved, message: '');
|
||||
return ArtifactPreviewPlan(
|
||||
kind: kind,
|
||||
resolved: resolved,
|
||||
message: '',
|
||||
delimiter: isTabSeparatedArtifact(artifact.contentType, artifact.name)
|
||||
? '\t'
|
||||
: ',',
|
||||
);
|
||||
}
|
||||
return ArtifactPreviewPlan(
|
||||
kind: ArtifactPreviewKind.unsupported,
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
/// A delimited text artifact (CSV/TSV) parsed for the viewer's table preview.
|
||||
///
|
||||
/// RFC 4180 quoting: a field in double quotes may contain the delimiter, line
|
||||
/// breaks and doubled quotes (`""` is one quote). Rows may be ragged; the table
|
||||
/// pads short rows when drawn rather than inventing values here.
|
||||
class ArtifactTable {
|
||||
const ArtifactTable({required this.rows, required this.totalRows});
|
||||
|
||||
/// At most [artifactTableRowLimit] parsed rows, the first being the header.
|
||||
final List<List<String>> rows;
|
||||
|
||||
/// Every record in the file, so a capped preview can say what it omits.
|
||||
final int totalRows;
|
||||
|
||||
bool get truncated => totalRows > rows.length;
|
||||
|
||||
int get columnCount =>
|
||||
rows.fold(0, (widest, row) => row.length > widest ? row.length : widest);
|
||||
}
|
||||
|
||||
/// Rows drawn in the viewer; larger files say how many were omitted.
|
||||
const int artifactTableRowLimit = 500;
|
||||
|
||||
/// Parses [text] separated by [delimiter] (`,` for CSV, a tab for TSV).
|
||||
ArtifactTable parseArtifactTable(
|
||||
String text, {
|
||||
String delimiter = ',',
|
||||
int limit = artifactTableRowLimit,
|
||||
}) {
|
||||
final rows = <List<String>>[];
|
||||
var total = 0;
|
||||
var row = <String>[];
|
||||
final field = StringBuffer();
|
||||
var quoted = false;
|
||||
var fieldStarted = false;
|
||||
|
||||
void endField() {
|
||||
row.add(field.toString());
|
||||
field.clear();
|
||||
fieldStarted = false;
|
||||
}
|
||||
|
||||
void endRow() {
|
||||
endField();
|
||||
// A trailing line break does not start an empty record.
|
||||
if (row.length == 1 && row.first.isEmpty) {
|
||||
row = <String>[];
|
||||
return;
|
||||
}
|
||||
total++;
|
||||
if (rows.length < limit) rows.add(row);
|
||||
row = <String>[];
|
||||
}
|
||||
|
||||
for (var i = 0; i < text.length; i++) {
|
||||
final char = text[i];
|
||||
if (quoted) {
|
||||
if (char == '"') {
|
||||
if (i + 1 < text.length && text[i + 1] == '"') {
|
||||
field.write('"');
|
||||
i++;
|
||||
} else {
|
||||
quoted = false;
|
||||
}
|
||||
} else {
|
||||
field.write(char);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (char == '"' && !fieldStarted) {
|
||||
quoted = true;
|
||||
fieldStarted = true;
|
||||
} else if (char == delimiter) {
|
||||
endField();
|
||||
} else if (char == '\n') {
|
||||
endRow();
|
||||
} else if (char == '\r') {
|
||||
if (i + 1 < text.length && text[i + 1] == '\n') i++;
|
||||
endRow();
|
||||
} else {
|
||||
field.write(char);
|
||||
fieldStarted = true;
|
||||
}
|
||||
}
|
||||
if (fieldStarted || field.isNotEmpty || row.isNotEmpty) endRow();
|
||||
return ArtifactTable(rows: rows, totalRows: total);
|
||||
}
|
||||
@@ -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