mirror of
https://gitea.com/gitea/tea.git
synced 2026-10-26 11:01:25 +01:00
feat: fit list tables to the terminal and write TSV when piped
Lists had no header, wrapped every row once a title was longer than the terminal, and were space-padded when piped, so cut -f and awk -F'\t' could not split them and empty cells collapsed. terminal.Table now takes column specs and a Display. On a terminal it prints an uppercase header, aligns columns two spaces apart and shrinks flexible columns, widest first, to Streams.Width (the terminal, then $COLUMNS, then 80), cutting by display width with "…". States use the terminal's ANSI 16 colors, hex colors fall back to the 256-color palette unless COLORTERM advertises 24-bit, and OSC 8 links appear only on the first column, independently of color (TEA_HYPERLINKS=auto|always|never). Piped tables are tab-separated without header, color or links, and times are RFC3339 UTC instead of "about 2 hours ago". terminal.View lays out single resources (title, badges, metadata, fields, body, footer URL) and renders markdown bodies with glamour on a terminal only (TEA_MARKDOWN=raw keeps them raw). Shared formatters cover relative times, timestamps, sizes, durations and status icons. In the app, writeTable and writeView are the new list and view writers: a "Showing N of M …" context line, an empty-state message on stderr and the next-page hint. writeList, writeItem and batch keep working and now render through the new table: tab-separated when piped, truncated at the widest column on a terminal, with headers for issue list and pr list. Signed-off-by: bircni <bircni@icloud.com>
This commit is contained in:
17 files changed
+1253
-103
No files matched your search
@@ -6,10 +6,12 @@ package app
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"gitea.dev/tea/internal/settings"
|
||||
"gitea.dev/tea/internal/terminal"
|
||||
"github.com/mattn/go-runewidth"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@@ -28,6 +30,22 @@ func (a *application) colorEnabled() bool {
|
||||
return terminal.ColorEnabled(preferences.Color, a.deps.Streams.OutTTY, a.deps.Getenv)
|
||||
}
|
||||
|
||||
// display describes stdout for tables and views. The hyperlinks (auto, always, never) and markdown
|
||||
// (auto, raw) preferences are read from TEA_HYPERLINKS and TEA_MARKDOWN until config.yml stores them.
|
||||
func (a *application) display() terminal.Display {
|
||||
preferences, _ := a.preferences()
|
||||
streams := a.deps.Streams
|
||||
return terminal.Display{
|
||||
TTY: streams.OutTTY,
|
||||
Color: terminal.ColorEnabled(preferences.Color, streams.OutTTY, a.deps.Getenv),
|
||||
TrueColor: terminal.TrueColor(a.deps.Getenv),
|
||||
Links: terminal.HyperlinksEnabled(a.deps.Getenv("TEA_HYPERLINKS"), streams.OutTTY, a.deps.Getenv),
|
||||
Markdown: terminal.MarkdownEnabled(a.deps.Getenv("TEA_MARKDOWN"), streams.OutTTY),
|
||||
Width: streams.Width,
|
||||
Now: a.deps.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
// writeHuman pages multi-line human list/view text. JSON callers must not use it.
|
||||
// --no-input does not disable paging.
|
||||
func (a *application) writeHuman(cmd *cobra.Command, text string) error {
|
||||
@@ -40,6 +58,81 @@ func (a *application) writeJSON(cmd *cobra.Command, v any) error {
|
||||
return terminal.WriteJSON(cmd.OutOrStdout(), v, a.options.json, a.options.jq)
|
||||
}
|
||||
|
||||
// listSpec describes how a list command shows its items to people.
|
||||
type listSpec[T any] struct {
|
||||
Columns []terminal.Column
|
||||
Row func(T) []terminal.Cell
|
||||
// Empty, such as "no open issues in alice/x", goes to stderr on a TTY when nothing is listed.
|
||||
Empty string
|
||||
// Context names the listed items, such as "open issues in alice/x", for the line that heads the
|
||||
// table on a TTY: "Showing 30 of 112 open issues in alice/x".
|
||||
Context string
|
||||
}
|
||||
|
||||
// listPage places a page of items in the whole listing.
|
||||
type listPage struct {
|
||||
// Number is the 1-based --page value.
|
||||
Number int
|
||||
// More reports later pages. Total is the size of the whole listing when Counted.
|
||||
More bool
|
||||
Total int
|
||||
Counted bool
|
||||
}
|
||||
|
||||
// writeTable writes items as a JSON array, or as a table through the pager. On a TTY the table has
|
||||
// a header row and, with a spec Context, a context line that also points at the next page; an empty
|
||||
// list prints the spec's Empty message to stderr instead. Piped tables are tab-separated, and the
|
||||
// next-page hint goes to stderr.
|
||||
func writeTable[T any](a *application, cmd *cobra.Command, items []T, spec listSpec[T], page listPage) error {
|
||||
if a.options.json != "" {
|
||||
return a.writeJSON(cmd, items)
|
||||
}
|
||||
d := a.display()
|
||||
if len(items) == 0 {
|
||||
if !d.TTY || spec.Empty == "" {
|
||||
return nil
|
||||
}
|
||||
_, err := fmt.Fprintln(cmd.ErrOrStderr(), spec.Empty)
|
||||
return err
|
||||
}
|
||||
rows := make([][]terminal.Cell, len(items))
|
||||
for i, item := range items {
|
||||
rows[i] = spec.Row(item)
|
||||
}
|
||||
text := terminal.Table(spec.Columns, rows, d)
|
||||
if d.TTY && spec.Context != "" {
|
||||
return a.writeHuman(cmd, showing(len(items), page, spec.Context)+"\n\n"+text)
|
||||
}
|
||||
if err := a.writeHuman(cmd, text); err != nil {
|
||||
return err
|
||||
}
|
||||
return a.morePagesHint(cmd, page.More, page.Number)
|
||||
}
|
||||
|
||||
// showing is the context line above a table: "Showing 30 of 112 open issues in alice/x · next: --page 2".
|
||||
func showing(shown int, page listPage, what string) string {
|
||||
line := fmt.Sprintf("Showing %d", shown)
|
||||
switch {
|
||||
case page.Counted:
|
||||
line += fmt.Sprintf(" of %d", page.Total)
|
||||
case !page.More && page.Number <= 1:
|
||||
line += fmt.Sprintf(" of %d", shown)
|
||||
}
|
||||
line += " " + what
|
||||
if page.More {
|
||||
line += fmt.Sprintf(" · next: --page %d", page.Number+1)
|
||||
}
|
||||
return line
|
||||
}
|
||||
|
||||
// writeView writes item as a JSON object, or as a view through the pager.
|
||||
func writeView[T any](a *application, cmd *cobra.Command, item T, view func(T) terminal.View) error {
|
||||
if a.options.json != "" {
|
||||
return a.writeJSON(cmd, item)
|
||||
}
|
||||
return a.writeHuman(cmd, view(item).Render(a.display()))
|
||||
}
|
||||
|
||||
// abortBatch stops a batch instead of recording an item failure.
|
||||
type abortBatch struct{ error }
|
||||
|
||||
@@ -70,7 +163,7 @@ func batch[K, T any](a *application, cmd *cobra.Command, keys []K, label func(K)
|
||||
var err error
|
||||
switch {
|
||||
case a.options.json == "":
|
||||
err = a.writeHuman(cmd, table(results, row, a.colorEnabled()))
|
||||
err = a.writeHuman(cmd, legacyTable(a, cmd, results, row))
|
||||
case len(keys) != 1:
|
||||
err = a.writeJSON(cmd, results)
|
||||
case len(results) == 1:
|
||||
@@ -79,20 +172,64 @@ func batch[K, T any](a *application, cmd *cobra.Command, keys []K, label func(K)
|
||||
return errors.Join(append(failures, err)...)
|
||||
}
|
||||
|
||||
// writeList writes items as a JSON array, or as one aligned table through the pager.
|
||||
// writeList writes items as a JSON array, or as one table through the pager. List commands move
|
||||
// to writeTable.
|
||||
func writeList[T any](a *application, cmd *cobra.Command, items []T, row func(T) []terminal.Cell) error {
|
||||
if a.options.json != "" {
|
||||
return a.writeJSON(cmd, items)
|
||||
}
|
||||
return a.writeHuman(cmd, table(items, row, a.colorEnabled()))
|
||||
return a.writeHuman(cmd, legacyTable(a, cmd, items, row))
|
||||
}
|
||||
|
||||
// writeItem writes one item as a JSON object, or as its list row.
|
||||
// writeItem writes one item as a JSON object, or as its list row. Views move to writeView.
|
||||
func writeItem[T any](a *application, cmd *cobra.Command, item T, row func(T) []terminal.Cell) error {
|
||||
if a.options.json != "" {
|
||||
return a.writeJSON(cmd, item)
|
||||
}
|
||||
return a.writeHuman(cmd, table([]T{item}, row, a.colorEnabled()))
|
||||
return a.writeHuman(cmd, legacyTable(a, cmd, []T{item}, row))
|
||||
}
|
||||
|
||||
// legacyColumns gives lists that still render through writeList a header row and a flexible
|
||||
// column, keyed by command path without the root. Their owners replace an entry with a listSpec.
|
||||
var legacyColumns = map[string][]terminal.Column{
|
||||
"issue list": numberedColumns,
|
||||
"pr list": numberedColumns,
|
||||
}
|
||||
|
||||
// numberedColumns head the rows of issueRow and pullRow.
|
||||
var numberedColumns = []terminal.Column{{Header: "ID", Prefix: "#"}, {Header: "Title", Flex: true}, {Header: "State"}, {Header: "Author"}}
|
||||
|
||||
// legacyTable renders rows of commands that predate listSpec: tab-separated when piped, and on a
|
||||
// TTY fit to the terminal width by truncating their widest column, unless legacyColumns names
|
||||
// their columns.
|
||||
func legacyTable[T any](a *application, cmd *cobra.Command, items []T, row func(T) []terminal.Cell) string {
|
||||
rows := make([][]terminal.Cell, len(items))
|
||||
for i, item := range items {
|
||||
rows[i] = row(item)
|
||||
}
|
||||
columns, ok := legacyColumns[strings.TrimPrefix(cmd.CommandPath(), cmd.Root().Name()+" ")]
|
||||
if !ok {
|
||||
columns = widestFlex(rows)
|
||||
}
|
||||
return terminal.Table(columns, rows, a.display())
|
||||
}
|
||||
|
||||
// widestFlex makes the column with the widest cell flexible.
|
||||
func widestFlex(rows [][]terminal.Cell) []terminal.Column {
|
||||
var widths []int
|
||||
for _, row := range rows {
|
||||
for i, cell := range row {
|
||||
if i == len(widths) {
|
||||
widths = append(widths, 0)
|
||||
}
|
||||
widths[i] = max(widths[i], runewidth.StringWidth(terminal.SafeLine(cell.Text)))
|
||||
}
|
||||
}
|
||||
columns := make([]terminal.Column, len(widths))
|
||||
if len(widths) > 0 {
|
||||
columns[slices.Index(widths, slices.Max(widths))].Flex = true
|
||||
}
|
||||
return columns
|
||||
}
|
||||
|
||||
// writeRecord writes one item as a JSON object, or as a human-readable record.
|
||||
@@ -121,15 +258,17 @@ func numberedRecord(number int64, title, url string) terminal.Record {
|
||||
return terminal.Record{Heading: []terminal.Cell{{Text: fmt.Sprintf("#%d", number), Bold: true}, {Text: title}}, Lines: []string{url}}
|
||||
}
|
||||
|
||||
// stateColors colors issue, pull request and milestone states on color terminals.
|
||||
var stateColors = map[string]string{"open": "2da44e", "closed": "cf222e", "merged": "8250df"}
|
||||
// stateColors colors issue, pull request and milestone states with the terminal's own palette.
|
||||
var stateColors = map[string]string{"open": "green", "closed": "red", "merged": "magenta", "draft": "muted"}
|
||||
|
||||
// table renders rows aligned like a TTY table without header or truncation, for commands that
|
||||
// predate listSpec and compose their own output.
|
||||
func table[T any](items []T, row func(T) []terminal.Cell, color bool) string {
|
||||
rows := make([][]terminal.Cell, len(items))
|
||||
for i, item := range items {
|
||||
rows[i] = row(item)
|
||||
}
|
||||
return terminal.Table(rows, color)
|
||||
return terminal.Table(nil, rows, terminal.Display{TTY: true, Color: color, TrueColor: true, Links: color})
|
||||
}
|
||||
|
||||
// morePagesHint tells human readers on stderr that the listing continues past this page.
|
||||
|
||||
Reference in new issue
Block a user