Skip to content

Commit

Permalink
Implement border customization
Browse files Browse the repository at this point in the history
Also refactors border code significantly for sanity and dealing with various edge cases
  • Loading branch information
Evertras authored Feb 13, 2022
1 parent 51fd171 commit 61f4393
Show file tree
Hide file tree
Showing 9 changed files with 549 additions and 125 deletions.
12 changes: 8 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
.PHONY: default
default:
go run examples/features/main.go
.PHONY: features
features:
@go run ./examples/features/main.go

.PHONY: dimensions
dimensions:
@go run ./examples/dimensions/main.go

.PHONY: test
test:
go test ./table
@go test ./table

.PHONY: fmt
fmt:
Expand Down
33 changes: 28 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,46 @@

A table component for the [Bubble Tea framework](https://github.com/charmbracelet/bubbletea).

This is currently messy as it's in early development and came out of a different
project, and should be cleaned up over time.

![Table](sample.png)

[View sample source code](./examples/features/main.go)

## Features

Displays a table with a header and borders.
For a code reference, please see the [full feature example](./examples/features/main.go).

Displays a table with a header, rows, and borders.

Border shape is customizable with a basic thick square default.

Rows can be individually styled.

Can be focused to highlight a row and navigate with up/down (and j/k).

Can make rows selectable, and fetch the current selections.

## Demos

Code examples are located in [the examples directory](./examples). Run commands
are added to the [Makefile](Makefile) for convenience but they should be as
simple as `go run ./examples/features/main.go`, etc.

To run the examples, clone this repo and run:

```bash
# Run the full feature demo
make

# Run the example
make dimensions

# Or run any of them directly
go run ./examples/features/main.go
```

## Contributing

Contributions welcome, but since this is being actively developed for use in
[Khan](https://github.com/evertras/khan) please check first by opening an issue!
[Khan](https://github.com/evertras/khan) please check first by opening an issue
or commenting on an existing one!

119 changes: 119 additions & 0 deletions examples/dimensions/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package main

import (
"fmt"
"log"
"strings"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/evertras/bubble-table/table"
)

type Model struct {
table3x3 table.Model
table1x3 table.Model
table3x1 table.Model
table1x1 table.Model
table5x5 table.Model
}

func genTable(columnCount int, rowCount int) table.Model {
headers := []table.Header{}

for column := 0; column < columnCount; column++ {
columnStr := fmt.Sprintf("%d", column+1)
headers = append(headers, table.NewHeader(columnStr, columnStr, 4))
}

rows := []table.Row{}

for row := 1; row < rowCount; row++ {
rowData := table.RowData{}

for column := 0; column < columnCount; column++ {
columnStr := fmt.Sprintf("%d", column+1)
rowData[columnStr] = fmt.Sprintf("%d,%d", column+1, row+1)
}

rows = append(rows, table.NewRow(rowData))
}

return table.New(headers).WithRows(rows).HeaderStyle(lipgloss.NewStyle().Bold(true))
}

func NewModel() Model {
return Model{
table1x1: genTable(1, 1),
table3x1: genTable(3, 1),
table1x3: genTable(1, 3),
table3x3: genTable(3, 3),
table5x5: genTable(5, 5),
}
}

func (m Model) Init() tea.Cmd {
return nil
}

func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var (
cmd tea.Cmd
cmds []tea.Cmd
)

m.table1x1, cmd = m.table1x1.Update(msg)
cmds = append(cmds, cmd)

m.table3x1, cmd = m.table3x1.Update(msg)
cmds = append(cmds, cmd)

m.table1x3, cmd = m.table1x3.Update(msg)
cmds = append(cmds, cmd)

m.table3x3, cmd = m.table3x3.Update(msg)
cmds = append(cmds, cmd)

m.table5x5, cmd = m.table5x5.Update(msg)
cmds = append(cmds, cmd)

switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "esc", "q":
cmds = append(cmds, tea.Quit)
}
}

return m, tea.Batch(cmds...)
}

func (m Model) View() string {
body := strings.Builder{}

body.WriteString("Table demo with various sized tables!\nPress space/enter to select a row, q or ctrl+c to quit\n")

pad := lipgloss.NewStyle().Padding(1)

tablesSmall := lipgloss.JoinHorizontal(
lipgloss.Top,
pad.Render(m.table1x1.View()),
pad.Render(m.table1x3.View()),
pad.Render(m.table3x1.View()),
pad.Render(m.table3x3.View()),
)

tableBig := pad.Render(m.table5x5.View())

body.WriteString(lipgloss.JoinVertical(lipgloss.Center, tablesSmall, tableBig))

return body.String()
}

func main() {
p := tea.NewProgram(NewModel())

if err := p.Start(); err != nil {
log.Fatal(err)
}
}
35 changes: 31 additions & 4 deletions examples/features/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// This file contains a full demo of most available features, for both testing
// and for reference
package main

import (
Expand All @@ -17,13 +19,35 @@ const (
columnKeyCount = "count"
)

var (
customBorder = table.Border{
Top: "─",
Left: "│",
Right: "│",
Bottom: "─",

TopRight: "╮",
TopLeft: "╭",
BottomRight: "╯",
BottomLeft: "╰",

TopJunction: "╥",
LeftJunction: "├",
RightJunction: "┤",
BottomJunction: "╨",
InnerJunction: "╫",

InnerDivider: "║",
}
)

type Model struct {
tableModel table.Model
}

func NewModel() Model {
headers := []table.Header{
table.NewHeader(columnKeyID, "ID", 5).WithStyle(lipgloss.NewStyle().Bold(true)),
table.NewHeader(columnKeyID, "ID", 5),
table.NewHeader(columnKeyName, "Name", 10),
table.NewHeader(columnKeyDescription, "Description", 30),
table.NewHeader(columnKeyCount, "#", 5),
Expand Down Expand Up @@ -53,9 +77,10 @@ func NewModel() Model {
return Model{
tableModel: table.New(headers).
WithRows(rows).
HeaderStyle(lipgloss.NewStyle().Bold(true)).
HeaderStyle(lipgloss.NewStyle().Foreground(lipgloss.Color("10")).Bold(true)).
SelectableRows(true).
Focused(true),
Focused(true).
Border(customBorder),
}
}

Expand Down Expand Up @@ -87,7 +112,7 @@ func (m Model) View() string {
body := strings.Builder{}

highlightedRow := m.tableModel.HighlightedRow()
body.WriteString("Table demo with selectable rows!\nPress space/enter to select a row, q or ctrl+c to quit\n")
body.WriteString("Table demo with all features enabled!\nPress space/enter to select a row, q or ctrl+c to quit\n")

body.WriteString(fmt.Sprintf("Currently looking at ID: %s\n", highlightedRow.Data[columnKeyID]))

Expand All @@ -102,6 +127,8 @@ func (m Model) View() string {

body.WriteString(m.tableModel.View())

body.WriteString("\n")

return body.String()
}

Expand Down
Loading

0 comments on commit 61f4393

Please sign in to comment.