diff --git a/packages/core/src/filter/complete.ts b/packages/core/src/filter/complete.ts index f07e507..1eed770 100644 --- a/packages/core/src/filter/complete.ts +++ b/packages/core/src/filter/complete.ts @@ -47,11 +47,11 @@ export interface CompletionResult { const SHORTHAND = new Set(['=', '==', '!=', '<>', '<', '<=', '>', '>=', '~', '!~']) const SHORTHAND_OP: Record = { '=': 'EQUALS', '==': 'EQUALS', '!=': 'NOT_EQUALS', '<>': 'NOT_EQUALS', '<': 'LESS_THAN', '<=': 'LESS_THAN_OR_EQUALS', '>': 'GREATER_THAN', '>=': 'GREATER_THAN_OR_EQUALS', '~': 'CONTAINS', '!~': 'NOT_CONTAINS' } -const TEXT_OPS: Operator[] = ['EQUALS', 'NOT_EQUALS', 'CONTAINS', 'NOT_CONTAINS', 'STARTS_WITH', 'NOT_STARTS_WITH', 'ENDS_WITH', 'NOT_ENDS_WITH', 'IN', 'NOT_IN', 'LIKE', 'NOT_LIKE', 'IS_BLANK', 'IS_NOT_BLANK'] -const NUMBER_OPS: Operator[] = ['EQUALS', 'NOT_EQUALS', 'LESS_THAN', 'LESS_THAN_OR_EQUALS', 'GREATER_THAN', 'GREATER_THAN_OR_EQUALS', 'BETWEEN', 'NOT_BETWEEN', 'IN', 'NOT_IN', 'IS_BLANK', 'IS_NOT_BLANK'] -const DATE_OPS: Operator[] = ['EQUALS', 'NOT_EQUALS', 'LESS_THAN', 'LESS_THAN_OR_EQUALS', 'GREATER_THAN', 'GREATER_THAN_OR_EQUALS', 'BETWEEN', 'NOT_BETWEEN', 'IS_BLANK', 'IS_NOT_BLANK'] -const BOOLEAN_OPS: Operator[] = ['TRUE', 'FALSE', 'IS_BLANK', 'IS_NOT_BLANK'] -const CHOICE_OPS: Operator[] = ['EQUALS', 'NOT_EQUALS', 'IN', 'NOT_IN', 'IS_NULL_OR_IN', 'IS_BLANK', 'IS_NOT_BLANK'] +const TEXT_OPS: Operator[] = ['EQUALS', 'NOT_EQUALS', 'NOT_EQUALS_OR_IS_NULL', 'CONTAINS', 'NOT_CONTAINS', 'STARTS_WITH', 'NOT_STARTS_WITH', 'ENDS_WITH', 'NOT_ENDS_WITH', 'IN', 'NOT_IN', 'IS_NULL_OR_IN', 'LIKE', 'NOT_LIKE', 'IS_BLANK', 'IS_NOT_BLANK'] +const NUMBER_OPS: Operator[] = ['EQUALS', 'NOT_EQUALS', 'NOT_EQUALS_OR_IS_NULL', 'LESS_THAN', 'LESS_THAN_OR_EQUALS', 'GREATER_THAN', 'GREATER_THAN_OR_EQUALS', 'BETWEEN', 'NOT_BETWEEN', 'IN', 'NOT_IN', 'IS_NULL_OR_IN', 'IS_BLANK', 'IS_NOT_BLANK'] +const DATE_OPS: Operator[] = ['EQUALS', 'NOT_EQUALS', 'NOT_EQUALS_OR_IS_NULL', 'LESS_THAN', 'LESS_THAN_OR_EQUALS', 'GREATER_THAN', 'GREATER_THAN_OR_EQUALS', 'BETWEEN', 'NOT_BETWEEN', 'IN', 'NOT_IN', 'IS_BLANK', 'IS_NOT_BLANK'] +const BOOLEAN_OPS: Operator[] = ['TRUE', 'FALSE', 'EQUALS', 'NOT_EQUALS', 'IS_BLANK', 'IS_NOT_BLANK'] +const CHOICE_OPS: Operator[] = ['EQUALS', 'NOT_EQUALS', 'NOT_EQUALS_OR_IS_NULL', 'IN', 'NOT_IN', 'IS_NULL_OR_IN', 'IS_BLANK', 'IS_NOT_BLANK'] /** The operators that make sense for a field, honouring `allowedFilterOperators`. */ export function operatorsForField(field: Pick | undefined): Operator[] { diff --git a/packages/core/src/process/runner.ts b/packages/core/src/process/runner.ts index 1a6fd81..472ce5f 100644 --- a/packages/core/src/process/runner.ts +++ b/packages/core/src/process/runner.ts @@ -48,6 +48,11 @@ export interface RunnerOptions { records?: { ids: string[] } | { filter: unknown } /** Files for BLOB fields, by field name: sent as multipart parts with the first step that asks for the field. */ files?: Record + /** + * Files a step's provider chooses while the process runs (a TUI file dialog): the provider adds + * entries here by field name and they are sent with that step, like `files`. + */ + stepFiles?: Record tableVariant?: string /** How long the server may run a step before backgrounding it (0: always background). */ stepTimeoutMillis?: number @@ -143,6 +148,12 @@ export async function runProcess(options: RunnerOptions): Promise { const timeout = String(options.stepTimeoutMillis ?? 0) const pendingFiles = { ...(options.files ?? {}) } + const takeStepFiles = () => { + for (const [k, v] of Object.entries(options.stepFiles ?? {})) { + pendingFiles[k] = v + delete options.stepFiles![k] + } + } const form = (values: FieldValues, extra: Record = {}, fileFields: string[] = []): Record => { const out: Record = { values: JSON.stringify(values), stepTimeoutMillis: timeout, ...extra } for (const name of fileFields) { @@ -211,6 +222,7 @@ export async function runProcess(options: RunnerOptions): Promise { throw new ProcessError(`step "${step.label ?? step.name}" needs ${missing.map((f) => f.name).join(', ')}`, processUUID) } if (isLast) return { response, values: { ...response.values, ...known }, processUUID: processUUID!, steps, lastStep: step, lastScreen: options.present ? screen : undefined } + takeStepFiles() const fileFields = fields.filter((f) => f.type === 'BLOB' && pendingFiles[f.name]).map((f) => f.name) for (const name of fileFields) delete values[name] response = (await callOperation(baseUrl, 'stepProcess', { path: { processName: meta.name, processUUID: processUUID!, stepName: next }, query, form: form(values, {}, fileFields) }, fetchImpl)).data as StepResponse diff --git a/packages/core/src/test/complete.test.ts b/packages/core/src/test/complete.test.ts index 66f4e1a..5e6dd75 100644 --- a/packages/core/src/test/complete.test.ts +++ b/packages/core/src/test/complete.test.ts @@ -20,7 +20,8 @@ const fields = [ test('operators follow the field type and allowedFilterOperators', () => { assert.ok(operatorsForField(fields[1]).includes('STARTS_WITH')) assert.ok(!operatorsForField(fields[3]).includes('STARTS_WITH')) - assert.deepEqual(operatorsForField(fields[4]), ['TRUE', 'FALSE', 'IS_BLANK', 'IS_NOT_BLANK']) + assert.deepEqual(operatorsForField(fields[4]), ['TRUE', 'FALSE', 'EQUALS', 'NOT_EQUALS', 'IS_BLANK', 'IS_NOT_BLANK']) + assert.ok(operatorsForField(fields[1]).includes('NOT_EQUALS_OR_IS_NULL') && operatorsForField(fields[3]).includes('IN')) assert.ok(operatorsForField(fields[5]).includes('IN') && !operatorsForField(fields[5]).includes('GREATER_THAN')) assert.deepEqual(operatorsForField({ type: 'STRING', allowedFilterOperators: ['EQUALS', 'IN'] }), ['EQUALS', 'IN']) }) diff --git a/packages/core/src/test/runner.test.ts b/packages/core/src/test/runner.test.ts index 2751088..bb6140b 100644 --- a/packages/core/src/test/runner.test.ts +++ b/packages/core/src/test/runner.test.ts @@ -248,3 +248,24 @@ test('a server that never stops answering with a next step is an error, not a co ]) await assert.rejects(runProcess({ baseUrl: base, fetch: server.fetch, meta: { name: 'loop', frontendSteps: loop } }), (error: unknown) => error instanceof ProcessError && /did not finish after 200 steps/.test(error.message)) }) + +test('stepFiles: a file the provider chooses during the run goes with that step as a multipart part', async () => { + const parts: string[] = [] + const server = fakeServer([ + [/POST .*\/init$/, () => ({ type: 'COMPLETE', processUUID: 'u1', nextStep: 'upload', values: {} })], + [/POST .*\/step\/upload$/, async (request) => { + const form = await request.formData() + const file = form.get('theFile') as File | null + parts.push(file ? `${file.name}:${await file.text()}` : 'none') + return { type: 'COMPLETE', processUUID: 'u1', nextStep: 'done', values: { ok: true } } + }], + ]) + const meta: ProcessMeta = { name: 'loader', frontendSteps: [{ name: 'upload', components: [{ type: 'EDIT_FORM' }], formFields: [{ name: 'theFile', label: 'File', type: 'BLOB', isRequired: true }] }, { name: 'done', components: [{ type: 'VIEW_FORM' }] }] } + const stepFiles: Record = {} + await runProcess({ baseUrl: base, fetch: server.fetch, meta, stepFiles, provide: async () => { + stepFiles.theFile = { name: 'a.csv', bytes: new TextEncoder().encode('x,y') } + return { theFile: 'a.csv' } + } }) + assert.deepEqual(parts, ['a.csv:x,y']) + assert.deepEqual(Object.keys(stepFiles), [], 'taken once sent') +}) diff --git a/packages/tui/scripts/fake-big-server.ts b/packages/tui/scripts/fake-big-server.ts index c3b72e0..c848bce 100644 --- a/packages/tui/scripts/fake-big-server.ts +++ b/packages/tui/scripts/fake-big-server.ts @@ -6,7 +6,7 @@ /** * A fake UI API server with a generated large instance (default 1000 tables, 500 processes, 3000 * widgets, 300 reports) for trying qqq ui at scale. MOCK sign-in; every table has id and name with - * 40 rows; every widget is a statistics widget answered after 150 ms. + * 40 rows (rows after 20 have much longer names, for the fixed-width check); every widget is a statistics widget answered after 150 ms. * bun run packages/tui/scripts/fake-big-server.ts [port] [tables] [processes] * qqq ui --server http://localhost:8099 */ @@ -36,7 +36,7 @@ Bun.serve({ const filter = ((await req.json()) as { filter?: { skip?: number; limit?: number } }).filter ?? {} if (m[2] === 'count') return json({ count: 40 }) const skip = filter.skip ?? 0 - return json({ records: Array.from({ length: Math.max(0, Math.min(filter.limit ?? 50, 40 - skip)) }, (_, i) => ({ values: { id: skip + i + 1, name: `${m![1]} ${skip + i + 1}` }, recordLabel: `${m![1]} ${skip + i + 1}` })) }) + return json({ records: Array.from({ length: Math.max(0, Math.min(filter.limit ?? 50, 40 - skip)) }, (_, i) => ({ values: { id: skip + i + 1, name: skip + i + 1 > 20 ? `${m![1]} ${skip + i + 1}, a much longer value than on the first page` : `${m![1]} ${skip + i + 1}` }, recordLabel: `${m![1]} ${skip + i + 1}` })) }) } m = /^\/widget\/([^/]+)$/.exec(path) if (m) { diff --git a/packages/tui/scripts/smoke-widths.sh b/packages/tui/scripts/smoke-widths.sh new file mode 100755 index 0000000..aeb3202 --- /dev/null +++ b/packages/tui/scripts/smoke-widths.sh @@ -0,0 +1,36 @@ +#!/usr/bin/env bash +# Copyright 2026 QRun.IO, Inc. +# Licensed under the Apache License, Version 2.0 (see LICENSE). +# +# tmux check that grid column widths stay fixed across pages: starts the fake large-instance server +# (rows after 20 have much longer values), opens a table, and compares the header and the column +# offsets of page 1 and page 2. packages/tui/scripts/smoke-widths.sh [port] +set -euo pipefail +here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +pkg="$(cd "$here/.." && pwd)" +port="${1:-8099}" +s="qqq-widths-$$" +cfg="$(mktemp -d)" +out="${OUT:-$cfg}" +bun run "$pkg/scripts/fake-big-server.ts" "$port" > "$cfg/server.log" 2>&1 & +server=$! +cleanup() { tmux kill-session -t "$s" 2>/dev/null || true; kill "$server" 2>/dev/null || true; } +trap cleanup EXIT +for _ in $(seq 1 40); do curl -sf "http://localhost:$port/qqq/v1/version" > /dev/null && break; sleep 0.25; done +tmux new-session -d -s "$s" -x 120 -y 30 "cd '$pkg' && QQQ_CONFIG_DIR='$cfg' QQQ_KEYRING=file QQQ_CACHE_DIR='$cfg/cache' bun run src/main.tsx --no-check --server http://localhost:$port; sleep 5" +keys() { for k in "$@"; do if [[ $k == T:* ]]; then tmux send-keys -t "$s" -l -- "${k#T:}"; else tmux send-keys -t "$s" "$k"; fi; sleep 0.25; done; } +waitfor() { for _ in $(seq 1 40); do tmux capture-pane -t "$s" -p > "$out/$1.txt"; grep -qF -- "$2" "$out/$1.txt" && return 0; sleep 0.25; done; echo "FAIL waiting for $2"; exit 1; } +waitfor home 'Applications' +keys T:: 'T:table query order_0' Enter +waitfor page1 'order_0 1 ' +keys End +waitfor page2 'a much longer' +h1="$(grep -m1 ' Id ' "$out/page1.txt" | sed 's/.*││//')" +h2="$(grep -m1 ' Id ' "$out/page2.txt" | sed 's/.*││//')" +r1="$(grep -m1 'order_0 1 ' "$out/page1.txt")" +r2="$(grep -m1 'order_0 40' "$out/page2.txt")" +# offset of the name column inside the grid pane (after the pane border), so the Apps pane does not count +off() { local pane="${1#*││}"; local before="${pane%%order_0*}"; echo "${#before}"; } +echo "page 1 header: $h1" +echo "page 2 header: $h2" +if [[ "$h1" == "$h2" && "$(off "$r1")" == "$(off "$r2")" && "$r2" == *"…"* ]]; then echo "widths: ok (header and offsets identical; long values cut with …)"; else echo "widths: FAIL"; exit 1; fi diff --git a/packages/tui/src/overlays/filter.tsx b/packages/tui/src/overlays/filter.tsx index 61365a0..91c232e 100644 --- a/packages/tui/src/overlays/filter.tsx +++ b/packages/tui/src/overlays/filter.tsx @@ -14,7 +14,7 @@ */ import { - FilterSyntaxError, LIST_OPERATORS, UNARY_OPERATORS, applyCompletion, completeFilter, describeFilter, expressionCandidates, locateFilterError, + FilterSyntaxError, LIST_OPERATORS, OPERATORS, UNARY_OPERATORS, applyCompletion, completeFilter, describeFilter, expressionCandidates, locateFilterError, operatorsForField, parseFilter, parseValue, printCriterion, printFilter, printValue, resolvePossibleValue, searchPossibleValues, type Completion, type CompletionResult, type Criterion, type FilterValue, type Operator, type QueryFilter, } from '@qrun-io/qqq-core' @@ -128,7 +128,10 @@ export class FilterOverlay implements Overlay { pickField((f) => { const pickOp = (next: (op: Operator) => void) => { if (start.operator && operatorsForField(f).includes(start.operator)) return next(start.operator) - this.store.open(new ChoiceOverlay(this.store, `${f.label}: which comparison?`, operatorsForField(f).map((o) => ({ label: o.toLowerCase().replace(/_/g, ' '), value: o, detail: o })), next)) + const ops = operatorsForField(f) + const more = OPERATORS.filter((o) => !ops.includes(o)) + const all = (pickAll: boolean) => this.store.open(new ChoiceOverlay(this.store, `${f.label}: which comparison?`, [...(pickAll ? OPERATORS : ops).map((o) => ({ label: o.toLowerCase().replace(/_/g, ' '), value: o as Operator | '', detail: o })), ...(!pickAll && more.length ? [{ label: 'More operators…', value: '' as const, detail: `${more.length} for other field types` }] : [])], (o) => (o ? next(o) : all(true)))) + all(false) } pickOp((op) => { if (UNARY_OPERATORS.has(op)) return done({ fieldName: f.name, operator: op, values: [] }) diff --git a/packages/tui/src/overlays/view-settings.tsx b/packages/tui/src/overlays/view-settings.tsx index 431f12b..ec49f20 100644 --- a/packages/tui/src/overlays/view-settings.tsx +++ b/packages/tui/src/overlays/view-settings.tsx @@ -11,7 +11,7 @@ import type { AppStore } from '../store' import { glyphs, theme } from '../theme' import type { Overlay } from '../types' import { pad } from '../util/text' -import { PromptOverlay, overlayId } from './basic' +import { ChoiceOverlay, PromptOverlay, overlayId } from './basic' import { Frame, Line } from './frame' /** @@ -34,7 +34,9 @@ export class GridViewSettingsOverlay implements Overlay { options(): Array<{ label: string; value: string; toggle: () => void }> { const t = theme() + const sort = this.view.sort as { field: string; asc: boolean } | undefined return [ + { label: 'Sort', value: sort ? `${this.meta.fields[sort.field]?.label ?? sort.field} ${sort.asc ? 'ascending' : 'descending'}` : 'default order', toggle: () => this.chooseSort() }, { label: 'Width use', value: this.view.fit === 'fill' ? 'fill the terminal width' : 'fit to content', toggle: () => { this.view.fit = this.view.fit === 'fill' ? 'content' : 'fill' } }, { label: 'Zebra stripes', value: (this.view.zebra ?? t.zebraByDefault) ? 'on' : 'off', toggle: () => { this.view.zebra = !(this.view.zebra ?? t.zebraByDefault) } }, { label: 'Header rule', value: (this.view.headerRule ?? true) ? 'on' : 'off', toggle: () => { this.view.headerRule = !(this.view.headerRule ?? true) } }, @@ -42,6 +44,17 @@ export class GridViewSettingsOverlay implements Overlay { ] } + chooseSort(): void { + const fields = Object.values(this.meta.fields).filter((f) => !f.isHidden) + this.store.open(new ChoiceOverlay(this.store, 'Sort by', [{ label: '(default order)', value: '' }, ...fields.map((f) => ({ label: f.label, value: f.name, detail: f.name }))], (field) => { + if (!field) { + delete this.view.sort + return + } + this.store.open(new ChoiceOverlay(this.store, 'Direction', [{ label: 'Ascending', value: true }, { label: 'Descending', value: false }], (asc) => { this.view.sort = { field, asc } })) + })) + } + save(): void { const columns = this.order.filter((n) => this.visible.has(n)) writeViewSettings(this.key, { ...this.view, columns: columns.length ? columns : undefined }) @@ -123,7 +136,7 @@ export class GridViewSettingsOverlay implements Overlay { return {pad(` ${it.text}`, inner)} })} - + ) diff --git a/packages/tui/src/screens/form.tsx b/packages/tui/src/screens/form.tsx index 3a4df12..1b7b2e1 100644 --- a/packages/tui/src/screens/form.tsx +++ b/packages/tui/src/screens/form.tsx @@ -3,8 +3,11 @@ * Licensed under the Apache License, Version 2.0 (see LICENSE). */ -import { FormInputError, FormSession, ResultCursor, copyRecordValues, dateTimesToUtc, defaultTimeZone, isInstant, isMissingValue, utcToLocalInput, validateValue, type FieldMeta, type FieldValues } from '@qrun-io/qqq-core' +import { FormInputError, FormSession, ResultCursor, copyRecordValues, dateTimesToUtc, formatSize, defaultTimeZone, isInstant, isMissingValue, utcToLocalInput, validateValue, type FieldMeta, type FieldValues } from '@qrun-io/qqq-core' import { UiApiError } from '@qrun-io/qqq-ui-api-client' +import { readFileSync } from 'node:fs' +import { basename } from 'node:path' +import { openFile } from '../overlays/files-helpers' import type { TableMetaData } from '../client' import type { Key } from '../keys' import { charOf, isPrintable } from '../keys' @@ -35,6 +38,22 @@ function hintFor(f: FieldMeta): string { } } +/** A field that takes a file: BLOB, or a FILE_UPLOAD adornment. */ +export function isFileField(f: FieldMeta): boolean { + return f.type === 'BLOB' || Boolean(f.adornments?.some((a) => a.type === 'FILE_UPLOAD')) +} + +/** The multipart record body (as the web UI's recordFormData): text values as strings, null as empty, files as parts. */ +export function multipartRecord(values: FieldValues, files: Map): Record { + const form: Record = {} + for (const [k, v] of Object.entries(values)) { + if (v === undefined || files.has(k)) continue + form[k] = v === null ? '' : typeof v === 'object' ? JSON.stringify(v) : String(v) + } + for (const [k, f] of files) form[k] = new File([f.bytes as BlobPart], f.name) + return form +} + function textOf(value: unknown): string { return value === null || value === undefined ? '' : typeof value === 'object' ? JSON.stringify(value) : String(value) } @@ -54,12 +73,14 @@ export class FormEditor { formError?: string saving = false readonly initial: FieldValues + /** Files chosen for BLOB and FILE_UPLOAD fields (sent as multipart parts). */ + files = new Map() constructor(private store: AppStore, readonly session: FormSession, readonly order: string[], readonly options: { bulkEdit?: boolean; readOnly?: string[] } = {}) { this.initial = { ...session.values } for (const name of order) { const f = session.fields[name] - if (f && TEXT_TYPES.has(f.type) && !session.hasChoices(name)) this.editors.set(name, new LineEditor(textOf(session.values[name]), { masked: f.type === 'PASSWORD' })) + if (f && TEXT_TYPES.has(f.type) && !session.hasChoices(name) && !isFileField(f)) this.editors.set(name, new LineEditor(textOf(session.values[name]), { masked: f.type === 'PASSWORD' })) } } @@ -72,7 +93,25 @@ export class FormEditor { } dirty(): boolean { - return this.names.some((n) => textOf(this.valueFor(n)) !== textOf(this.initial[n])) + return this.files.size > 0 || this.names.some((n) => textOf(this.valueFor(n)) !== textOf(this.initial[n])) + } + + /** Chooses a file for a file field through the file navigator. */ + chooseFile(name: string): void { + const f = this.session.fields[name]! + openFile(this.store, `File for ${f.label}`, (path) => { + try { + const bytes = new Uint8Array(readFileSync(path)) + const fileName = basename(path) + this.files.set(name, { name: fileName, bytes, path }) + this.session.values[name] = fileName + delete this.session.errors[name] + if (this.options.bulkEdit) this.enabled.add(name) + this.store.bump() + } catch (e) { + this.session.errors[name] = errorText(e) + } + }) } /** The value a field has now (the editor text for text fields not yet committed). */ @@ -166,6 +205,18 @@ export class FormEditor { return true } if (readOnly) return key.name === 'return' ? (this.move(1), true) : false + if (isFileField(f)) { + if (key.name === 'return' || key.name === 'space') { + this.chooseFile(name) + return true + } + if (key.name === 'backspace' || key.name === 'delete') { + this.files.delete(name) + this.session.values[name] = null + return true + } + return false + } if (this.session.hasChoices(name)) { if (key.name === 'return' || key.name === 'space') { this.openPicker(name) @@ -232,13 +283,14 @@ export class FormEditor { if (ed && !disabled) editor = else { let shown: string - if (f.type === 'BOOLEAN') shown = this.session.values[name] === true ? `${g.check} yes` : this.session.values[name] === false ? `${g.uncheck} no` : `${g.uncheck} (not set)` + if (isFileField(f)) shown = this.files.get(name) ? `${this.files.get(name)!.name} (${formatSize(this.files.get(name)!.bytes.length)})` : isMissingValue(this.session.values[name]) ? '(choose a file…)' : '(stored file; Enter replaces)' + else if (f.type === 'BOOLEAN') shown = this.session.values[name] === true ? `${g.check} yes` : this.session.values[name] === false ? `${g.uncheck} no` : `${g.uncheck} (not set)` else if (this.session.hasChoices(name)) shown = isMissingValue(this.session.values[name]) ? (state.disabledBecause ? '' : '(choose…)') : `${this.session.displayValues[name] ?? textOf(this.session.values[name])}` else shown = ed ? ed.value : textOf(this.session.values[name]) - const pickGlyph = this.session.hasChoices(name) && !disabled ? ' ▾' : '' + const pickGlyph = (this.session.hasChoices(name) || isFileField(f)) && !disabled ? ' ▾' : '' editor = {pad(trunc(shown, editorWidth - pickGlyph.length) + pickGlyph, editorWidth)} } - const msg = error ? `! ${error}` : state.disabledBecause ? state.disabledBecause : idx === this.index ? (this.session.hasChoices(name) ? 'Enter or type to choose · Backspace clears' : hintFor(f)) : '' + const msg = error ? `! ${error}` : state.disabledBecause ? state.disabledBecause : idx === this.index ? (isFileField(f) ? 'Enter chooses a file · Backspace clears' : this.session.hasChoices(name) ? 'Enter or type to choose · Backspace clears' : hintFor(f)) : '' return ( {`${sel ? g.pointer : ' '}${bulk}${pad(f.label, labelWidth - 1)}${required} `} @@ -360,18 +412,22 @@ export class FormScreen implements Screen { } try { if (this.options.mode === 'edit') { - const changed = editor.names.filter((n) => textOf(editor.session.values[n]) !== textOf(editor.initial[n])) + const changed = editor.names.filter((n) => editor.files.has(n) || textOf(editor.session.values[n]) !== textOf(editor.initial[n])) if (!changed.length) { this.store.flash('nothing changed') this.store.back() return } const values = dateTimesToUtc(Object.values(editor.session.fields), Object.fromEntries(changed.map((n) => [n, editor.session.values[n] ?? null])), { originals: this.originals }) - await client.update(this.tableName, this.options.primaryKey, values) + if (editor.files.size) await client.call('updateRecord', { path: { tableName: this.tableName, primaryKey: String(this.options.primaryKey) }, form: multipartRecord(values, editor.files) }) + else await client.update(this.tableName, this.options.primaryKey, values) this.store.flash(`saved ${meta.label} ${this.options.primaryKey}`) this.store.back() } else { - const record = await client.insert(this.tableName, dateTimesToUtc(Object.values(editor.session.fields), editor.session.submission())) + const values = dateTimesToUtc(Object.values(editor.session.fields), editor.session.submission()) + const record = editor.files.size + ? ((await client.data>('insertRecord', { path: { tableName: this.tableName }, form: multipartRecord(values, editor.files) })).record ?? {}) as { values?: Record } + : await client.insert(this.tableName, values) const pk = record.values?.[meta.primaryKeyField] this.store.flash(`created ${meta.label} ${String(pk ?? '')}`) replaceWithRecord(this.store, this.tableName, String(pk)) diff --git a/packages/tui/src/screens/process.tsx b/packages/tui/src/screens/process.tsx index 4d64507..6849459 100644 --- a/packages/tui/src/screens/process.tsx +++ b/packages/tui/src/screens/process.tsx @@ -51,6 +51,8 @@ export class ProcessScreen implements Screen { log: string[] = [] processUUID?: string private cancelled = false + /** Files chosen in a step's form, handed to the kernel runner (sent with that step). */ + private stepFiles: Record = {} private pending?: { resolve: (v: FieldValues) => void; reject: (e: Error) => void } constructor(private store: AppStore, readonly processName: string, readonly options: ProcessOptions = {}) { @@ -119,6 +121,7 @@ export class ProcessScreen implements Screen { meta: this.meta, values: this.options.values, records: this.options.recordIds?.length ? { ids: this.options.recordIds } : this.options.filter ? { filter: this.options.filter } : undefined, + stepFiles: this.stepFiles, stepTimeoutMillis: 0, pollMillis: 250, always: true, @@ -207,6 +210,10 @@ export class ProcessScreen implements Screen { const enabled = editor.options.bulkEdit ? editor.enabled : undefined for (const n of editor.names) if (editor.session.values[n] !== undefined && (!enabled || enabled.has(n))) out[n] = editor.session.values[n] if (enabled) out.bulkEditEnabledFields = [...enabled].join(',') + for (const [name, file] of editor.files) { + this.stepFiles[name] = { name: file.name, bytes: file.bytes } + out[name] = file.name + } this.pending = undefined this.phase = 'running' this.stepStarted = Date.now() diff --git a/packages/tui/src/screens/record.tsx b/packages/tui/src/screens/record.tsx index 2daf8d7..ecc971b 100644 --- a/packages/tui/src/screens/record.tsx +++ b/packages/tui/src/screens/record.tsx @@ -3,7 +3,7 @@ * Licensed under the Apache License, Version 2.0 (see LICENSE). */ -import { ResultCursor, associationGroups, childFilter, childListFields, readViewSettings, relatedRecordTarget, saveDownload, valueText, widgetToBlocks, writeViewSettings, type AssociationGroup, type FieldMeta, type TagEntry, type QueryRecord, type RenderBlock, type TableMetaLike } from '@qrun-io/qqq-core' +import { ResultCursor, associationGroups, childFilter, childListFields, formatSize, readViewSettings, relatedRecordTarget, saveDownload, valueText, widgetToBlocks, writeViewSettings, type AssociationGroup, type FieldMeta, type TagEntry, type QueryRecord, type RenderBlock, type TableMetaLike } from '@qrun-io/qqq-core' import { saveFile } from '../overlays/files-helpers' import { actionsOverlay } from '../overlays/actions' import { TagOverlay, collaborationOf } from '../overlays/tags' @@ -32,6 +32,27 @@ export function linkTarget(store: AppStore, field: FieldMetaData, record?: Query return pvs && tables.includes(pvs) ? pvs : undefined } +/** A BLOB value stored in the record itself (base64), not behind a FILE_DOWNLOAD route. */ +export function isBlob(record: QueryRecord | undefined, f: FieldMetaData): boolean { + const raw = record?.values?.[f.name] + return f.type === 'BLOB' && typeof raw === 'string' && raw !== '' && !f.adornments?.some((a) => a.type === 'FILE_DOWNLOAD') +} + +export function blobText(record: QueryRecord, field: string): string { + const raw = record.values?.[field] + if (typeof raw !== 'string' || !raw) return '' + return `file, ${formatSize(Math.floor((raw.length * 3) / 4) - (raw.endsWith('==') ? 2 : raw.endsWith('=') ? 1 : 0))}` +} + +/** Saves a BLOB value (base64 in the record) through the save dialog. */ +export function saveBlob(store: AppStore, record: QueryRecord, table: string, pk: string, f: FieldMetaData): void { + const bytes = new Uint8Array(Buffer.from(String(record.values?.[f.name] ?? ''), 'base64')) + saveFile(store, `Save ${f.label}`, `${table}-${pk}-${f.name}`, (path) => { + const out = saveDownload(path, bytes, { explicit: true }) + store.message(`Saved ${out} (${bytes.length} bytes).`) + }) +} + /** Sections in tier order (T1, then T2, then T3, then the rest), hidden ones left out. */ export function orderedSections(meta: TableMetaData) { const rank = (tier?: string) => (tier === 'T1' ? 0 : tier === 'T2' ? 1 : tier === 'T3' ? 2 : 3) @@ -178,7 +199,7 @@ export class RecordScreen implements Screen { rows.push({ text: '' }) rows.push({ text: `${s.label}${s.tier ? ` (${s.tier})` : ''} ${g.rule.repeat(Math.max(0, Math.min(60, inner - s.label.length - 8)))}`, fg: t.acc, bold: true }) const tableNames = Object.keys(this.store.meta?.tables ?? {}) - const valueOf = (f: FieldMetaData) => valueText(f as FieldMeta, rec as never, { reveal: this.revealed.has(f.name), tableNames, tableName: this.tableName, primaryKey: this.primaryKey, widgets: this.store.meta?.widgets as never }).text + const valueOf = (f: FieldMetaData) => f.type === 'BLOB' && !f.adornments?.some((a) => a.type === 'FILE_DOWNLOAD') ? blobText(rec, f.name) : valueText(f as FieldMeta, rec as never, { reveal: this.revealed.has(f.name), tableNames, tableName: this.tableName, primaryKey: this.primaryKey, widgets: this.store.meta?.widgets as never }).text // the value column is sized once per section (first record shown) and kept while stepping records const frozenKey = `${s.name}|${inner}` if (!this.sectionWidths.has(frozenKey)) this.sectionWidths.set(frozenKey, Math.max(...fields.map((f) => valueOf(f).length + (linkTarget(this.store, f) ? 2 : 0)), 12)) @@ -187,12 +208,12 @@ export class RecordScreen implements Screen { const two = (view.recordColumns ?? (inner >= 2 * colWidth + 6 ? 2 : 1)) === 2 && inner >= 2 * Math.min(colWidth, 30) + 6 const fieldText = (f: FieldMetaData) => { const v = valueText(f as FieldMeta, rec as never, { tableNames, widgets: this.store.meta?.widgets as never }) - const link = v.link ? ` ${g.link}` : v.file ? ' [file: Enter saves]' : v.masked ? ' [Enter reveals]' : '' + const link = v.link ? ` ${g.link}` : v.file || isBlob(rec, f) ? ' [file: Enter saves]' : v.masked ? ' [Enter reveals]' : '' return `${pad(f.label, labelWidth)} ${trunc(valueOf(f) + link, (two ? colWidth : Math.min(inner - 4, 100)) - labelWidth - 2)}` } for (let i = 0; i < fields.length; i += two ? 2 : 1) { const pair = two ? fields.slice(i, i + 2) : [fields[i]!] - const links = pair.filter((f) => { const v = valueText(f as FieldMeta, rec as never, { tableNames }); return Boolean(v.link || v.file || v.masked) }) + const links = pair.filter((f) => { const v = valueText(f as FieldMeta, rec as never, { tableNames }); return Boolean(v.link || v.file || v.masked || isBlob(rec, f)) }) const text = ' ' + (two ? pad(fieldText(pair[0]!), colWidth) + ' ' + (pair[1] ? fieldText(pair[1]) : '') : fieldText(pair[0]!)) rows.push({ text, key: `f:${pair[0]!.name}`, action: () => this.follow(links) }) } @@ -284,6 +305,7 @@ export class RecordScreen implements Screen { follow(fields: FieldMetaData[]): void { const go = (f: FieldMetaData) => { + if (isBlob(this.record, f)) return saveBlob(this.store, this.record!, this.tableName, String(this.primaryKey), f) const v = valueText(f as FieldMeta, this.record as never, { tableNames: Object.keys(this.store.meta?.tables ?? {}) }) if (v.file) return this.download(f) if (v.masked) { @@ -405,6 +427,17 @@ export class RecordScreen implements Screen { { section: 'Context', label: 'Copy as a CLI command', detail: `qqq ${this.cliEquivalent()}`, run: () => this.store.message(`qqq ${this.cliEquivalent()}`, 'info', 'CLI equivalent') }, ] for (const group of this.groups) { + const child = this.childMeta.get(group.table) + for (const r of (group.records as QueryRecord[]).slice(0, 30)) { + for (const f of Object.values(child?.fields ?? {})) { + const v = valueText(f as FieldMeta, r as never, {}) + if (isBlob(r, f)) out.push({ section: 'Context', label: `Save ${f.label} of ${group.label} ${r.recordLabel ?? ''}`.trim(), run: () => saveBlob(this.store, r, group.table, String(r.values?.[child!.primaryKeyField] ?? ''), f) }) + else if (v.file?.route) { + const route = v.file.route + out.push({ section: 'Context', label: `Save ${f.label} of ${group.label} ${r.recordLabel ?? ''}`.trim(), detail: v.file.fileName, run: () => saveFile(this.store, `Save ${v.file!.fileName}`, v.file!.fileName, (p) => this.store.run(this.store.client!.call('downloadRecordFieldFile', { path: route }), (res) => { const b = res.bytes ?? new TextEncoder().encode(String(res.data ?? '')); this.store.message(`Saved ${saveDownload(p, b, { explicit: true })} (${b.length} bytes).`) })) }) + } + } + } out.push({ section: 'Context', label: `New ${group.label} for this record`, key: 'F7', run: () => this.newChild(group) }) out.push({ section: 'Context', label: `Query all ${group.label} of this record`, run: () => import('../nav').then((n) => n.openTable(this.store, group.table, this.childFilterOf(group))) }) } @@ -414,6 +447,7 @@ export class RecordScreen implements Screen { const v = valueText(f as FieldMeta, this.record as never, { tableNames }) if (v.link) out.push({ section: 'Context', label: `Follow ${f.label}`, detail: cell(this.record, f.name), run: () => this.follow([f]) }) if (v.file) out.push({ section: 'Context', label: `Download ${f.label}`, detail: v.file.fileName, run: () => this.download(f) }) + else if (isBlob(this.record, f)) out.push({ section: 'Context', label: `Save ${f.label}`, detail: blobText(this.record, f.name), run: () => saveBlob(this.store, this.record!, this.tableName, String(this.primaryKey), f) }) if (v.masked) out.push({ section: 'Context', label: `Reveal ${f.label}`, run: () => this.follow([f]) }) } for (const p of this.recordProcesses()) out.push({ section: 'Context', label: `Run ${p.label} on this record`, run: () => openProcess(this.store, p.name, { recordIds: [String(this.primaryKey)] }) }) diff --git a/packages/tui/src/screens/table.tsx b/packages/tui/src/screens/table.tsx index dba3b49..a9c9755 100644 --- a/packages/tui/src/screens/table.tsx +++ b/packages/tui/src/screens/table.tsx @@ -64,6 +64,18 @@ export function layoutColumns(fields: FieldMetaData[], rows: QueryRecord[], avai c.width += add spare -= add } + // room for longer values on later pages: text columns grow toward 40 cells from what is left + // (the layout is frozen for the life of the grid, so this is decided once) + const growable = cols.filter((c) => !view.widths?.[c.name] && !c.numeric && ['STRING', 'TEXT', 'HTML'].includes(c.type) && c.width < 40) + while (spare > 0 && view.fit !== 'fill' && growable.some((c) => c.width < 40)) { + for (const c of growable) { + if (spare <= 0) break + if (c.width < 40) { + c.width++ + spare-- + } + } + } if (spare > 0 && view.fit === 'fill' && cols.length) { const each = Math.floor(spare / cols.length) cols.forEach((c, i) => { c.width += each + (i < spare - each * cols.length ? 1 : 0) }) @@ -112,6 +124,8 @@ export class TableScreen implements Screen { if (this.filter.orderBys?.[0]) this.sort = { field: this.filter.orderBys[0].fieldName, asc: this.filter.orderBys[0].isAscending } delete this.filter.orderBys this.view = readViewSettings(`table:${tableName}`) + const savedSort = this.view.sort as { field: string; asc: boolean } | undefined + if (!this.sort && savedSort) this.sort = savedSort store.run(store.client!.tableMeta(tableName), (meta) => { this.meta = meta this.newCursor(options.index ?? 0) @@ -304,6 +318,11 @@ export class TableScreen implements Screen { if (!this.meta) return undefined return new GridViewSettingsOverlay(this.store, this.meta, `table:${this.tableName}`, this.fields().map((f) => f.name), () => { this.view = readViewSettings(`table:${this.tableName}`) + const sort = this.view.sort as { field: string; asc: boolean } | undefined + if (JSON.stringify(sort) !== JSON.stringify(this.sort)) { + this.sort = sort + this.newCursor(0) + } this.store.bump() }) } diff --git a/packages/tui/test/fake-server.ts b/packages/tui/test/fake-server.ts index 7e43fa9..fcef797 100644 --- a/packages/tui/test/fake-server.ts +++ b/packages/tui/test/fake-server.ts @@ -43,6 +43,12 @@ const processMeta = { ], } +const docMeta = { + name: 'doc', label: 'Doc', primaryKeyField: 'id', insertPermission: true, editPermission: true, deletePermission: true, + fields: { id: { name: 'id', label: 'Id', type: 'INTEGER', isEditable: false }, title: { name: 'title', label: 'Title', type: 'STRING' }, content: { name: 'content', label: 'Content', type: 'BLOB' } }, + sections: [{ name: 's', label: 'Doc', tier: 'T1', fieldNames: ['id', 'title', 'content'] }], +} + const wideMeta = { name: 'wide', label: 'Wide', primaryKeyField: 'id', insertPermission: true, editPermission: true, deletePermission: true, fields: { id: { name: 'id', label: 'Id', type: 'INTEGER' }, name: { name: 'name', label: 'Name', type: 'STRING' }, note: { name: 'note', label: 'Note', type: 'STRING' } }, @@ -73,13 +79,29 @@ export function fakeFetch(log: FakeLog = { requests: [] }, bigMeta?: unknown): t return json({ apps: { people: { name: 'people', label: 'People', children: [{ type: 'TABLE', name: 'person', label: 'Person' }, { type: 'TABLE', name: 'address', label: 'Address' }, { type: 'PROCESS', name: 'sleeper', label: 'Sleeper' }] } }, appTree: [{ type: 'APP', name: 'people', label: 'People', children: [{ type: 'TABLE', name: 'person', label: 'Person' }, { type: 'TABLE', name: 'address', label: 'Address' }, { type: 'PROCESS', name: 'sleeper', label: 'Sleeper' }] }], - tables: { person: { name: 'person', label: 'Person', insertPermission: true }, address: { name: 'address', label: 'Address', insertPermission: true }, wide: { name: 'wide', label: 'Wide' } }, + tables: { person: { name: 'person', label: 'Person', insertPermission: true }, address: { name: 'address', label: 'Address', insertPermission: true }, wide: { name: 'wide', label: 'Wide' }, doc: { name: 'doc', label: 'Doc', insertPermission: true } }, processes: { sleeper: { name: 'sleeper', label: 'Sleeper' } }, widgets: {}, branding: { appName: 'Fake' }, }) } if (path === '/metaData/table/person') return json(personMeta) if (path === '/metaData/table/wide') return json(wideMeta) + if (path === '/metaData/table/doc') return json(docMeta) + if (path === '/table/doc' && request.method === 'POST') { + const b = body as Record + const file = b.content as File | undefined + const text = file && typeof file === 'object' ? await file.text() : '' + log.requests.at(-1)!.body = { title: b.title, content: file && typeof file === 'object' ? `${file.name}:${text}` : b.content } + return json({ record: { values: { id: 5, title: b.title, content: Buffer.from(text).toString('base64') }, recordLabel: String(b.title) } }) + } + if (path === '/table/doc/5' && request.method === 'GET') return json({ record: { values: { id: 5, title: 'Report', content: Buffer.from('file body').toString('base64') }, displayValues: {}, recordLabel: 'Report' } }) + if (path === '/table/person/export') { + const b = body as { filter: { criteria?: Array<{ fieldName: string; values: string[] }> }; fieldNames: string[]; format: string } + let rows = [...PEOPLE] + for (const c of b.filter.criteria ?? []) rows = rows.filter((p) => String((p as J)[c.fieldName]) === String(c.values[0])) + const csv = [b.fieldNames.join(','), ...rows.map((r) => b.fieldNames.map((f) => String((r as J)[f] ?? '')).join(','))].join('\n') + return new Response(csv, { status: 200, headers: { 'content-type': 'text/csv' } }) + } if (path === '/table/wide/count') return json({ count: WIDE.length }) if (path === '/table/wide/query') { const f = ((body as J).filter ?? {}) as { skip?: number; limit?: number } diff --git a/packages/tui/test/tui.test.tsx b/packages/tui/test/tui.test.tsx index 608fd10..10f82f3 100644 --- a/packages/tui/test/tui.test.tsx +++ b/packages/tui/test/tui.test.tsx @@ -6,6 +6,10 @@ import { afterEach, beforeAll, expect, test } from 'bun:test' import { KeyCodes } from '@opentui/core/testing' import type { TableScreen } from '../src/screens/table' +import { mkdtempSync, readFileSync, writeFileSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { writeFilePreferences } from '@qrun-io/qqq-core' import { command, isolate, press, startTui, waitFor, type Harness } from './helpers' let h: Harness | undefined @@ -163,3 +167,55 @@ test('Home: panels, Tab between them, Enter on an application card opens its app await press(h, () => h!.setup.mockInput.pressEscape()) expect(h.store.screen?.kind).toBe('home') }) + +test('export from a filtered grid: the filter and the visible columns go to the server; the file is written', async () => { + const dir = mkdtempSync(join(tmpdir(), 'qqq-export-')) + writeFilePreferences({ downloadDir: dir, downloadMode: 'silent' }) + try { + h = await startTui(120, 30) + await command(h, 'table query person -w "lastName = Odd"') + await waitFor(h, (x) => x.includes('15 records')) + await command(h, 'export csv') + await waitFor(h, (x) => x.includes('Wrote')) + const req = h.log.requests.find((r) => r.path === '/table/person/export')! + const b = req.body as { filter: { criteria: unknown[] }; fieldNames: string[]; format: string } + expect(b.format).toBe('csv') + expect(b.filter.criteria).toEqual([{ fieldName: 'lastName', operator: 'EQUALS', values: ['Odd'] }]) + expect(b.fieldNames).toEqual(['id', 'firstName', 'lastName', 'birthDate']) + const text = readFileSync(join(dir, 'person.csv'), 'utf8') + expect(text.split('\n').length).toBe(16) + } finally { + writeFilePreferences({ downloadDir: undefined, downloadMode: undefined }) + } +}) + +test('a file field: chosen in the file dialog, sent as a multipart part; a stored BLOB saves from the record view', async () => { + const dir = mkdtempSync(join(tmpdir(), 'qqq-upload-')) + writeFileSync(join(dir, 'notes.txt'), 'hello upload') + h = await startTui(120, 34) + await command(h, 'table create doc --set title=Report') + await waitFor(h, (x) => x.includes('(choose a file…)')) + await press(h, () => h!.setup.mockInput.pressArrow('down')) + await press(h, () => h!.setup.mockInput.pressEnter()) + await waitFor(h, (x) => x.includes('File for Content')) + await press(h, () => h!.setup.mockInput.pressTab()) + await press(h, () => h!.setup.mockInput.pressKey('u', { ctrl: true })) + await press(h, () => h!.setup.mockInput.typeText(join(dir, 'notes.txt'))) + await press(h, () => h!.setup.mockInput.pressEnter()) + await waitFor(h, (x) => x.includes('notes.txt (12 B)')) + await press(h, () => h!.setup.mockInput.pressKey('s', { ctrl: true })) + await waitFor(h, (x) => x.includes('created Doc 5')) + expect(h.log.requests.find((r) => r.path === '/table/doc' && r.method === 'POST')!.body).toEqual({ title: 'Report', content: 'notes.txt:hello upload' }) + writeFilePreferences({ downloadDir: dir, downloadMode: 'silent' }) + try { + await command(h, 'table get doc 5') + await waitFor(h, (x) => x.includes('file, 9 B')) + await press(h, () => h!.setup.mockInput.pressKey('a')) + await press(h, () => h!.setup.mockInput.typeText('save content')) + await press(h, () => h!.setup.mockInput.pressEnter()) + await waitFor(h, (x) => x.includes('Saved')) + expect(readFileSync(join(dir, 'doc-5-content'), 'utf8')).toBe('file body') + } finally { + writeFilePreferences({ downloadDir: undefined, downloadMode: undefined }) + } +})