Documentation
¶
Index ¶
- Constants
- func ClipContent(r *model.Result, f Format) (clip.Content, error)
- func HTMLFragment(r *model.Result) string
- func Names() []string
- func NumericColumns(r *model.Result) []bool
- func PlainCells(r *model.Result) string
- func PlainCellsTransposed(r *model.Result) string
- func Pos(i, n int) string
- func Render(r *model.Result, f Format) (string, error)
- func RenderAll(rs []*model.Result, f Format) (string, error)
- func RenderBlock(r *model.Result, f Format, i, n int) (string, error)
- func RenderOpen(rs []*model.Result, f Format) (string, error)
- func RenderRun(rs []*model.Result, at []int, total int, f Format) (string, error)
- func Seq(n int) []int
- func Streamable(f Format) bool
- func TextTable(r *model.Result) string
- func ToClipboard(r *model.Result, f Format) error
- func ToFile(r *model.Result, f Format, path string) error
- func Transpose(r *model.Result, first int) *model.Result
- type Format
Constants ¶
const BlockSep = "\n"
BlockSep goes between two blocks of a multi-result document in a block format. Each block already ends with a newline, so one more makes the blank line that keeps CSV blocks apart and gives the banners air.
const TransposeNameCol = "column"
TransposeNameCol heads the column of names in a transposed result.
Variables ¶
This section is empty.
Functions ¶
func ClipContent ¶
ClipContent is what a copy of r in format f should put on the clipboard.
Every format but HTML is its rendered text and nothing else. HTML is the one that means something different on a clipboard than in a file: whoever copies "as HTML" is about to paste into Teams or a document and wants a TABLE, so the HTML flavor is the inline-styled fragment (HTMLFragment), not the full-page export with its <style> block that paste targets strip.
The plain-text flavor of an HTML copy is the aligned text table (TextTable), NOT the fragment's source. The plain flavor is what the user sees whenever the rich one does not make it — a paste target that takes text, the OSC 52 fallback over SSH, a system with no rich writer, dbc web on a plain-http page — and in every one of those the user asked for a TABLE: a screenful of <td style="…"> tags is the one outcome that is never useful. An aligned table still reads as a table in a monospace target and stays legible in a proportional one. (The fragment used to be the plain flavor too, "for editors"; that turned every lost rich flavor into a wall of markup. The HTML source is still one step away: Export → HTML page.)
clipboard entry for one "Table for Teams" copy ┌────────────────────────┬──────────────────────────────────┐ │ text/html (rich) │ HTMLFragment: inline-styled table │ ← Teams, Outlook, Docs │ text/plain (fallback) │ TextTable: aligned columns │ ← terminals, editors, OSC 52 └────────────────────────┴──────────────────────────────────┘
func HTMLFragment ¶
HTMLFragment renders a result as one self-styled <table>, for the HTML flavor of a clipboard copy. It is not a full document; see the note above for why that is the point.
A leading <meta charset> is included even though a fragment has no <head>: macOS hands public.html to the pasting app as bytes with no encoding attached, and some readers fall back to Latin-1 without it, which turns "café" into "café". The browsers and chat apps that honor it inside a fragment far outnumber any that choke on it.
func NumericColumns ¶
NumericColumns reports, per column, whether every non-NULL value in it is a Go number. The typed Raw values decide rather than the display strings, because "007" in a varchar column looks numeric and is not; a column of nothing but NULLs is not numeric, since there is no evidence either way.
MySQL hands every value back as bytes, which rawVal turns into strings, so MySQL results stay left-aligned. That is the honest answer given what the driver reports, and it costs only alignment.
func PlainCells ¶
PlainCells is the plain, keyboard-shortcut copy of r: one value alone, as it is, or rows of tab-separated values without a header — what pastes cleanly into a spreadsheet or back into a query. The TUI's y / Y and dbc web's copy both use it.
func PlainCellsTransposed ¶ added in v0.7.0
PlainCellsTransposed is PlainCells for a grid on its side: one line per column of r, its values tab-separated across r's rows, and no names — the plain copy carries values only, in the orientation the grid shows them. One value alone is copied as it is, as PlainCells does.
func Pos ¶
Pos formats block i's place for a banner or a note: "i/n" against a known total, "#i" when the total is open-ended (n <= 0).
func RenderAll ¶
RenderAll produces one document holding the results of a multi-statement run, in the requested format. Each result carries a banner naming its position, connection, and row count, so the blocks stay tellable apart — except in CSV and TSV, where a banner would not be data; there the blocks are separated by a blank line and each keeps its own header row. A single result renders exactly as Render does.
func RenderBlock ¶
RenderBlock renders r as block i (1-based) of n in a multi-result document in a Streamable format: its banner, where the format has one, then the result itself. CSV and TSV carry no banner — it would not be data. n is what the banner counts against; a streaming caller passes the number of statements it means to run, since it cannot know how many will succeed.
n <= 0 means the total is open-ended — a script's results, which arrive until the script returns — and the banner reads "#i" instead of "i/n".
func RenderOpen ¶
RenderOpen produces the document a stream of rs writes when the total was open-ended while it ran — a script's s.Show results, whose count is known only once the script returns. Every block carries an "#i" banner, a lone one included: the stream had to write the first block before it could know whether a second would follow, and rendering it bare only when collected would make `> file` and `-o file` differ. Only Streamable formats have such a document; HTML and JSON are collected whole, so RenderAll serves them.
func RenderRun ¶
RenderRun is RenderAll for a run in which not every statement produced a result — one that stopped at a failure, or went on past some. at[i] is the 1-based position of rs[i] among the run's total statements, and the banners (and HTML headings) count against total, so result 4 of a run of 5 in which statement 2 failed still reads "4/5". The shape follows total, not len(rs): a multi-statement run is always a multi-result document, even when only one of its statements made it — a JSON consumer gets the array of envelopes it asked for, not a bare result array. A run of one statement renders exactly as Render does.
func Streamable ¶
Streamable reports whether a multi-result document in f is nothing but its blocks joined by BlockSep. Such a document can be written a block at a time, as each result arrives, and still come out exactly as RenderAll would make it. HTML and JSON wrap the blocks in one document (a <html> shell, a JSON array), so they have to wait for the last result.
func ToClipboard ¶
ToClipboard renders the result and places it on the system clipboard. The HTML format goes on as a real table (see ClipContent), so a script's s.Export(r, "html", "") pastes into a chat app the way the TUI's copy does.
func Transpose ¶ added in v0.7.0
Transpose returns r with rows and columns swapped. Row i of the result is r's column i: its name, then that column's value in each of r's rows. Those rows become columns headed "row N", N counting from first — the display row the first of them was, so a transposed range copied from the middle of a grid keeps the numbers the grid showed. A single row is headed "value" instead: the common case is one record, and "column | value" reads better pasted into a message than "column | row 17".
Raw is transposed with Rows, so a real NULL stays NULL (drawn as such by the HTML renderings) and JSON keeps each value's type.
Types ¶
type Format ¶
type Format string
Format is an output rendering for a query result.
func ParseFormat ¶
ParseFormat resolves a format name, accepting common aliases.