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);
|
||||
}
|
||||
Reference in New Issue
Block a user