Skip to content

Commit 2a7eea9

Browse files
authored
refactor: use lipgloss table (#6)
1 parent 5afdcb9 commit 2a7eea9

File tree

3 files changed

+36
-82
lines changed

3 files changed

+36
-82
lines changed

cmds.go

Lines changed: 26 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@ package main
22

33
import (
44
"fmt"
5-
"log"
6-
"os"
75
"strconv"
86
"time"
97

108
tea "github.com/charmbracelet/bubbletea"
119
"github.com/charmbracelet/kancli"
12-
"golang.org/x/term"
1310

1411
"github.com/charmbracelet/bubbles/list"
15-
"github.com/charmbracelet/bubbles/table"
1612
"github.com/charmbracelet/lipgloss"
13+
"github.com/charmbracelet/lipgloss/table"
1714
"github.com/spf13/cobra"
1815
)
1916

@@ -119,86 +116,43 @@ var listCmd = &cobra.Command{
119116
if err != nil {
120117
return err
121118
}
122-
table := setupTable(tasks)
123-
fmt.Print(table.View())
119+
fmt.Print(setupTable(tasks))
124120
return nil
125121
},
126122
}
127123

128-
func calculateWidth(min, width int) int {
129-
p := width / 10
130-
switch min {
131-
case XS:
132-
if p < XS {
133-
return XS
134-
}
135-
return p / 2
136-
137-
case SM:
138-
if p < SM {
139-
return SM
140-
}
141-
return p / 2
142-
case MD:
143-
if p < MD {
144-
return MD
145-
}
146-
return p * 2
147-
case LG:
148-
if p < LG {
149-
return LG
150-
}
151-
return p * 3
152-
default:
153-
return p
154-
}
155-
}
156-
157-
const (
158-
XS int = 1
159-
SM int = 3
160-
MD int = 5
161-
LG int = 10
162-
)
163-
164-
func setupTable(tasks []task) table.Model {
165-
// get term size
166-
w, _, err := term.GetSize(int(os.Stdout.Fd()))
167-
if err != nil {
168-
// we don't really want to fail it...
169-
log.Println("unable to calculate height and width of terminal")
170-
}
171-
172-
columns := []table.Column{
173-
{Title: "ID", Width: calculateWidth(XS, w)},
174-
{Title: "Name", Width: calculateWidth(LG, w)},
175-
{Title: "Project", Width: calculateWidth(MD, w)},
176-
{Title: "Status", Width: calculateWidth(SM, w)},
177-
{Title: "Created At", Width: calculateWidth(MD, w)},
178-
}
179-
var rows []table.Row
124+
func setupTable(tasks []task) *table.Table {
125+
columns := []string{"ID", "Name", "Project", "Status", "Created At"}
126+
var rows [][]string
180127
for _, task := range tasks {
181-
rows = append(rows, table.Row{
128+
rows = append(rows, []string{
182129
fmt.Sprintf("%d", task.ID),
183130
task.Name,
184131
task.Project,
185132
task.Status,
186133
task.Created.Format("2006-01-02"),
187134
})
188135
}
189-
t := table.New(
190-
table.WithColumns(columns),
191-
table.WithRows(rows),
192-
table.WithFocused(false),
193-
table.WithHeight(len(tasks)),
194-
)
195-
s := table.DefaultStyles()
196-
s.Header = s.Header.
197-
BorderStyle(lipgloss.NormalBorder()).
198-
BorderForeground(lipgloss.Color("240")).
199-
BorderBottom(true).
200-
Bold(false)
201-
t.SetStyles(s)
136+
t := table.New().
137+
Border(lipgloss.HiddenBorder()).
138+
Headers(columns...).
139+
Rows(rows...).
140+
StyleFunc(func(row, col int) lipgloss.Style {
141+
if row == 0 {
142+
return lipgloss.NewStyle().
143+
Foreground(lipgloss.Color("212")).
144+
Border(lipgloss.NormalBorder()).
145+
BorderTop(false).
146+
BorderLeft(false).
147+
BorderRight(false).
148+
BorderBottom(true).
149+
Bold(true)
150+
}
151+
if row%2 == 0 {
152+
return lipgloss.NewStyle().Foreground(lipgloss.Color("246"))
153+
}
154+
return lipgloss.NewStyle()
155+
})
202156
return t
203157
}
204158

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ require (
66
github.com/charmbracelet/bubbles v0.16.1
77
github.com/charmbracelet/bubbletea v0.24.2
88
github.com/charmbracelet/kancli v0.0.0-20230629174247-b2093471047b
9-
github.com/charmbracelet/lipgloss v0.7.1
9+
github.com/charmbracelet/lipgloss v0.9.1
1010
github.com/mattn/go-sqlite3 v1.14.17
1111
github.com/muesli/go-app-paths v0.2.2
1212
github.com/spf13/cobra v1.7.0
13-
golang.org/x/term v0.9.0
1413
)
1514

1615
require (
@@ -19,9 +18,9 @@ require (
1918
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
2019
github.com/inconshreveable/mousetrap v1.1.0 // indirect
2120
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
22-
github.com/mattn/go-isatty v0.0.19 // indirect
21+
github.com/mattn/go-isatty v0.0.20 // indirect
2322
github.com/mattn/go-localereader v0.0.1 // indirect
24-
github.com/mattn/go-runewidth v0.0.14 // indirect
23+
github.com/mattn/go-runewidth v0.0.15 // indirect
2524
github.com/mitchellh/go-homedir v1.1.0 // indirect
2625
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
2726
github.com/muesli/cancelreader v0.2.2 // indirect
@@ -32,5 +31,6 @@ require (
3231
github.com/spf13/pflag v1.0.5 // indirect
3332
golang.org/x/sync v0.3.0 // indirect
3433
golang.org/x/sys v0.16.0 // indirect
34+
golang.org/x/term v0.9.0 // indirect
3535
golang.org/x/text v0.10.0 // indirect
3636
)

go.sum

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ github.com/charmbracelet/bubbletea v0.24.2 h1:uaQIKx9Ai6Gdh5zpTbGiWpytMU+CfsPp06
88
github.com/charmbracelet/bubbletea v0.24.2/go.mod h1:XdrNrV4J8GiyshTtx3DNuYkR1FDaJmO3l2nejekbsgg=
99
github.com/charmbracelet/kancli v0.0.0-20230629174247-b2093471047b h1:ElLjniv+gMcqCK+RyqDnquzp+CP9uslGjfyJKABSLfU=
1010
github.com/charmbracelet/kancli v0.0.0-20230629174247-b2093471047b/go.mod h1:hq6p8QuwQr/Fsj1nZou17b0taqWI09Nz8GvzVBNaW4s=
11-
github.com/charmbracelet/lipgloss v0.7.1 h1:17WMwi7N1b1rVWOjMT+rCh7sQkvDU75B2hbZpc5Kc1E=
12-
github.com/charmbracelet/lipgloss v0.7.1/go.mod h1:yG0k3giv8Qj8edTCbbg6AlQ5e8KNWpFujkNawKNhE2c=
11+
github.com/charmbracelet/lipgloss v0.9.1 h1:PNyd3jvaJbg4jRHKWXnCj1akQm4rh8dbEzN1p/u1KWg=
12+
github.com/charmbracelet/lipgloss v0.9.1/go.mod h1:1mPmG4cxScwUQALAAnacHaigiiHB9Pmr+v1VEawJl6I=
1313
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 h1:q2hJAaP1k2wIvVRd/hEHD7lacgqrCPS+k8g1MndzfWY=
1414
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=
1515
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
@@ -18,13 +18,13 @@ github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLf
1818
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
1919
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
2020
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
21-
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
22-
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
21+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
22+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
2323
github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=
2424
github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=
2525
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
26-
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
27-
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
26+
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
27+
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
2828
github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM=
2929
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
3030
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=

0 commit comments

Comments
 (0)