Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ gum write
gum write --width 40 --height 6 --placeholder "Type whatever you want" --prompt "| " --show-cursor-line --show-line-numbers --value "Something..." --base.padding 1 --cursor.foreground 99 --prompt.foreground 99

# Table
gum table < table/example.csv
gum table -i 1 -q Peach < table/example.csv

# Pager
gum pager < README.md
Expand Down
14 changes: 14 additions & 0 deletions table/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ func (o Options) Run() error {
columnNames = o.Columns
}

if (o.DefaultColumn == 0) != (o.DefaultSelect == "") {
return fmt.Errorf("--default-column and --default-select must both be set")
}

data, err := reader.ReadAll()
if err != nil {
return fmt.Errorf("invalid data provided")
Expand All @@ -87,6 +91,7 @@ func (o Options) Run() error {
}

rows := make([]table.Row, 0, len(data))
cursor := 0
for row := range data {
if len(data[row]) > len(columns) {
return fmt.Errorf("invalid number of columns")
Expand All @@ -97,6 +102,14 @@ func (o Options) Run() error {
data[row] = append(data[row], "")
}

if o.DefaultColumn > len(columns) {
return fmt.Errorf("--default-column out of range")
}
if o.DefaultColumn > 0 {
if data[row][o.DefaultColumn-1] == o.DefaultSelect {
cursor = row
}
}
for i, col := range data[row] {
if len(o.Widths) == 0 {
width := lipgloss.Width(col)
Expand Down Expand Up @@ -137,6 +150,7 @@ func (o Options) Run() error {
}

table := table.New(opts...)
table.SetCursor(cursor)

ctx, cancel := timeout.Context(o.Timeout)
defer cancel()
Expand Down
2 changes: 2 additions & 0 deletions table/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ type Options struct {
HeaderStyle style.Styles `embed:"" prefix:"header." envprefix:"GUM_TABLE_HEADER_"`
SelectedStyle style.Styles `embed:"" prefix:"selected." set:"defaultForeground=212" envprefix:"GUM_TABLE_SELECTED_"`
ReturnColumn int `short:"r" help:"Which column number should be returned instead of whole row as string. Default=0 returns whole Row" default:"0"`
DefaultSelect string `short:"q" help:"What string to match as the selected row" default:""`
DefaultColumn int `short:"i" help:"Which column to match as the selected row. Default=0 matches against the whole Row" default:"0"`
Timeout time.Duration `help:"Timeout until choose returns selected element" default:"0s" env:"GUM_TABLE_TIMEOUT"`
}