-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(table): gum table for tabular data
- Loading branch information
1 parent
a82d5af
commit bdd86d5
Showing
13 changed files
with
362 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package pager | ||
|
||
import ( | ||
"github.com/charmbracelet/bubbles/viewport" | ||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/charmbracelet/glamour" | ||
"github.com/charmbracelet/gum/internal/stdin" | ||
) | ||
|
||
// Run provides a shell script interface for the viewport bubble. | ||
// https://github.com/charmbracelet/bubbles/viewport | ||
func (o Options) Run() error { | ||
vp := viewport.New(o.Style.Width, o.Style.Height) | ||
vp.Style = o.Style.ToLipgloss() | ||
var err error | ||
|
||
if o.Content == "" { | ||
o.Content, err = stdin.Read() | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
renderer, err := glamour.NewTermRenderer( | ||
glamour.WithWordWrap(80), | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
md, err := renderer.Render(o.Content) | ||
vp.SetContent(md) | ||
|
||
model := model{ | ||
viewport: vp, | ||
helpStyle: o.HelpStyle.ToLipgloss(), | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return tea.NewProgram(model, tea.WithAltScreen()).Start() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package pager | ||
|
||
import "github.com/charmbracelet/gum/style" | ||
|
||
// Options are the options for the pager. | ||
type Options struct { | ||
Style style.Styles `embed:"" help:"Style the pager" envprefix:"GUM_PAGER_"` | ||
HelpStyle style.Styles `embed:"" prefix:"help." help:"Style the help text" set:"defaultForeground=241" envprefix:"GUM_PAGER_HELP_"` | ||
Content string `arg:"" optional:"" help:"Display content to scroll"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Package pager provides a pager (similar to less) for the terminal. | ||
// | ||
// $ cat file.txt | gum page | ||
package pager | ||
|
||
import ( | ||
"github.com/charmbracelet/bubbles/viewport" | ||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/charmbracelet/lipgloss" | ||
) | ||
|
||
type model struct { | ||
viewport viewport.Model | ||
helpStyle lipgloss.Style | ||
} | ||
|
||
func (m model) Init() tea.Cmd { | ||
return nil | ||
} | ||
|
||
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
switch msg := msg.(type) { | ||
case tea.WindowSizeMsg: | ||
m.viewport.Width = msg.Width | ||
m.viewport.Height = msg.Height - 1 | ||
case tea.KeyMsg: | ||
switch msg.String() { | ||
case "q", "ctrl+c", "esc": | ||
return m, tea.Quit | ||
} | ||
} | ||
var cmd tea.Cmd | ||
m.viewport, cmd = m.viewport.Update(msg) | ||
return m, cmd | ||
} | ||
|
||
func (m model) View() string { | ||
return m.viewport.View() + "\n" + m.helpStyle.Render("↑/↓: Navigate • q: Quit") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Bubble Gum,Price,Ingredients | ||
Strawberry,$0.88,"Water,Sugar" | ||
Guava,$1.00,"Guava Flavoring,Food Coloring,Xanthan Gum" | ||
Orange,$0.99,"Sugar,Dextrose,Glucose" | ||
Cinnamon,$0.50,"Cin""na""mon" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package table | ||
|
||
import ( | ||
"encoding/csv" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/alecthomas/kong" | ||
"github.com/charmbracelet/bubbles/table" | ||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/mattn/go-runewidth" | ||
|
||
"github.com/charmbracelet/gum/internal/stdin" | ||
"github.com/charmbracelet/gum/style" | ||
) | ||
|
||
// Run provides a shell script interface for rendering tabular data (CSV) | ||
func (o Options) Run() error { | ||
var reader *csv.Reader | ||
if o.File != "" { | ||
file, err := os.OpenFile(o.File, os.O_RDONLY, 0600) | ||
if err != nil { | ||
return fmt.Errorf("could not find file at path %s", o.File) | ||
} | ||
reader = csv.NewReader(file) | ||
} else { | ||
if stdin.IsEmpty() { | ||
return fmt.Errorf("no data provided") | ||
} | ||
reader = csv.NewReader(os.Stdin) | ||
} | ||
|
||
separatorRunes := []rune(o.Separator) | ||
if len(separatorRunes) != 1 { | ||
return fmt.Errorf("separator must be single character") | ||
} | ||
reader.Comma = separatorRunes[0] | ||
|
||
var columnNames []string | ||
var err error | ||
// If no columns are provided we'll use the first row of the CSV as the | ||
// column names. | ||
if len(o.Columns) <= 0 { | ||
columnNames, err = reader.Read() | ||
if err != nil { | ||
return fmt.Errorf("unable to parse columns") | ||
} | ||
} else { | ||
columnNames = o.Columns | ||
} | ||
|
||
data, err := reader.ReadAll() | ||
if err != nil { | ||
return fmt.Errorf("invalid data provided") | ||
} | ||
var columns []table.Column | ||
|
||
for i, title := range columnNames { | ||
width := runewidth.StringWidth(title) | ||
if len(o.Widths) > i { | ||
width = o.Widths[i] | ||
} | ||
columns = append(columns, table.Column{ | ||
Title: title, | ||
Width: width, | ||
}) | ||
} | ||
|
||
defaultStyles := table.DefaultStyles() | ||
|
||
styles := table.Styles{ | ||
Cell: defaultStyles.Cell.Inherit(o.CellStyle.ToLipgloss()), | ||
Header: defaultStyles.Header.Inherit(o.HeaderStyle.ToLipgloss()), | ||
Selected: defaultStyles.Selected.Inherit(o.SelectedStyle.ToLipgloss()), | ||
} | ||
|
||
var rows []table.Row | ||
for _, row := range data { | ||
rows = append(rows, table.Row(row)) | ||
} | ||
|
||
table := table.New( | ||
table.WithColumns(columns), | ||
table.WithFocused(true), | ||
table.WithHeight(o.Height), | ||
table.WithRows(rows), | ||
table.WithStyles(styles), | ||
) | ||
|
||
tm, err := tea.NewProgram(model{table: table}, tea.WithOutput(os.Stderr)).StartReturningModel() | ||
|
||
if err != nil { | ||
return fmt.Errorf("failed to start tea program: %w", err) | ||
} | ||
|
||
if tm == nil { | ||
return fmt.Errorf("failed to get selection") | ||
} | ||
|
||
m := tm.(model) | ||
fmt.Println(strings.Join([]string(m.selected), string(o.Separator))) | ||
|
||
return nil | ||
} | ||
|
||
// BeforeReset hook. Used to unclutter style flags. | ||
func (o Options) BeforeReset(ctx *kong.Context) error { | ||
style.HideFlags(ctx) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Bubble Gum Flavor,Price | ||
Strawberry,$0.99 | ||
Cherry,$0.50 | ||
Banana,$0.75 | ||
Orange,$0.25 | ||
Lemon,$0.50 | ||
Lime,$0.50 | ||
Grape,$0.50 | ||
Watermelon,$0.50 | ||
Pineapple,$0.50 | ||
Blueberry,$0.50 | ||
Raspberry,$0.50 | ||
Cranberry,$0.50 | ||
Peach,$0.50 | ||
Apple,$0.50 | ||
Mango,$0.50 | ||
Pomegranate,$0.50 | ||
Coconut,$0.50 | ||
Cinnamon,$0.50 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Bubble Gum Flavor | ||
Strawberry,$0.99 | ||
Cherry,$0.50 | ||
Banana,$0.75 | ||
Orange | ||
Lemon,$0.50 | ||
Lime,$0.50 | ||
Grape,$0.50 | ||
Watermelon,$0.50 | ||
Pineapple,$0.50 | ||
Blueberry,$0.50 | ||
Raspberry,$0.50 | ||
Cranberry,$0.50 | ||
Peach,$0.50 | ||
Apple,$0.50 | ||
Mango,$0.50 | ||
Pomegranate,$0.50 | ||
Coconut,$0.50 | ||
Cinnamon,$0.50 |
Oops, something went wrong.