-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c580544
commit 09c90ee
Showing
7 changed files
with
1,057 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,19 @@ | ||
# go-bubble-table | ||
TUI table component for Bubbletea. | ||
# Table | ||
|
||
[![Go Reference](https://pkg.go.dev/badge/github.com/calyptia/go-bubble-table.svg)](https://pkg.go.dev/github.com/calyptia/go-bubble-table) | ||
|
||
TUI table component for [Bubble Tea](https://github.com/charmbracelet/bubbletea) applications. | ||
|
||
This component is used in the [Calyptia Cloud CLI](https://github.com/calyptia/cloud-cli) under the `top` command. | ||
|
||
https://user-images.githubusercontent.com/7969166/155205138-e205b38b-3631-43b2-a369-1f57914da838.mp4 | ||
|
||
## Installation | ||
|
||
```bash | ||
go get github.com/calyptia/go-bubble-table | ||
``` | ||
|
||
## Example | ||
|
||
For an example please take a look at the `/example` directory. |
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,81 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/brianvoe/gofakeit/v6" | ||
table "github.com/calyptia/go-bubble-table" | ||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/charmbracelet/lipgloss" | ||
"golang.org/x/term" | ||
) | ||
|
||
func main() { | ||
err := tea.NewProgram(initialModel(), tea.WithAltScreen()).Start() | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
var ( | ||
styleDoc = lipgloss.NewStyle().Padding(1) | ||
) | ||
|
||
func initialModel() model { | ||
w, h, err := term.GetSize(int(os.Stdout.Fd())) | ||
if err != nil { | ||
w = 80 | ||
h = 24 | ||
} | ||
top, right, bottom, left := styleDoc.GetPadding() | ||
w = w - left - right | ||
h = h - top - bottom | ||
tbl := table.New([]string{"ID", "NAME", "AGE", "CITY"}, w, h) | ||
rows := make([]table.Row, 100) | ||
for i := 0; i < 100; i++ { | ||
rows[i] = table.SimpleRow{ | ||
i, | ||
gofakeit.Name(), | ||
gofakeit.Number(0, 122), | ||
gofakeit.City(), | ||
} | ||
} | ||
tbl.SetRows(rows) | ||
return model{table: tbl} | ||
} | ||
|
||
type model struct { | ||
table table.Model | ||
} | ||
|
||
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: | ||
top, right, bottom, left := styleDoc.GetPadding() | ||
m.table.SetSize( | ||
msg.Width-left-right, | ||
msg.Height-top-bottom, | ||
) | ||
case tea.KeyMsg: | ||
switch msg.String() { | ||
case "ctrl+c": | ||
return m, tea.Quit | ||
} | ||
} | ||
|
||
var cmd tea.Cmd | ||
m.table, cmd = m.table.Update(msg) | ||
return m, cmd | ||
} | ||
|
||
func (m model) View() string { | ||
return styleDoc.Render( | ||
m.table.View(), | ||
) | ||
} |
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,25 @@ | ||
module github.com/calyptia/go-bubble-table | ||
|
||
go 1.17 | ||
|
||
require ( | ||
github.com/brianvoe/gofakeit/v6 v6.14.5 | ||
github.com/charmbracelet/bubbles v0.10.3 | ||
github.com/charmbracelet/bubbletea v0.20.0 | ||
github.com/charmbracelet/lipgloss v0.5.0 | ||
github.com/juju/ansiterm v0.0.0-20210929141451-8b71cc96ebdc | ||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 | ||
) | ||
|
||
require ( | ||
github.com/containerd/console v1.0.3 // indirect | ||
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect | ||
github.com/lunixbochs/vtclean v1.0.0 // indirect | ||
github.com/mattn/go-isatty v0.0.14 // indirect | ||
github.com/mattn/go-runewidth v0.0.13 // indirect | ||
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect | ||
github.com/muesli/reflow v0.3.0 // indirect | ||
github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739 // indirect | ||
github.com/rivo/uniseg v0.2.0 // indirect | ||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect | ||
) |
Oops, something went wrong.