Skip to content

Commit

Permalink
refactor: use lipgloss table (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
bashbunni authored Jan 5, 2024
1 parent 5afdcb9 commit 2a7eea9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 82 deletions.
98 changes: 26 additions & 72 deletions cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@ package main

import (
"fmt"
"log"
"os"
"strconv"
"time"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/kancli"
"golang.org/x/term"

"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/table"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/table"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -119,86 +116,43 @@ var listCmd = &cobra.Command{
if err != nil {
return err
}
table := setupTable(tasks)
fmt.Print(table.View())
fmt.Print(setupTable(tasks))
return nil
},
}

func calculateWidth(min, width int) int {
p := width / 10
switch min {
case XS:
if p < XS {
return XS
}
return p / 2

case SM:
if p < SM {
return SM
}
return p / 2
case MD:
if p < MD {
return MD
}
return p * 2
case LG:
if p < LG {
return LG
}
return p * 3
default:
return p
}
}

const (
XS int = 1
SM int = 3
MD int = 5
LG int = 10
)

func setupTable(tasks []task) table.Model {
// get term size
w, _, err := term.GetSize(int(os.Stdout.Fd()))
if err != nil {
// we don't really want to fail it...
log.Println("unable to calculate height and width of terminal")
}

columns := []table.Column{
{Title: "ID", Width: calculateWidth(XS, w)},
{Title: "Name", Width: calculateWidth(LG, w)},
{Title: "Project", Width: calculateWidth(MD, w)},
{Title: "Status", Width: calculateWidth(SM, w)},
{Title: "Created At", Width: calculateWidth(MD, w)},
}
var rows []table.Row
func setupTable(tasks []task) *table.Table {
columns := []string{"ID", "Name", "Project", "Status", "Created At"}
var rows [][]string
for _, task := range tasks {
rows = append(rows, table.Row{
rows = append(rows, []string{
fmt.Sprintf("%d", task.ID),
task.Name,
task.Project,
task.Status,
task.Created.Format("2006-01-02"),
})
}
t := table.New(
table.WithColumns(columns),
table.WithRows(rows),
table.WithFocused(false),
table.WithHeight(len(tasks)),
)
s := table.DefaultStyles()
s.Header = s.Header.
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("240")).
BorderBottom(true).
Bold(false)
t.SetStyles(s)
t := table.New().
Border(lipgloss.HiddenBorder()).
Headers(columns...).
Rows(rows...).
StyleFunc(func(row, col int) lipgloss.Style {
if row == 0 {
return lipgloss.NewStyle().
Foreground(lipgloss.Color("212")).
Border(lipgloss.NormalBorder()).
BorderTop(false).
BorderLeft(false).
BorderRight(false).
BorderBottom(true).
Bold(true)
}
if row%2 == 0 {
return lipgloss.NewStyle().Foreground(lipgloss.Color("246"))
}
return lipgloss.NewStyle()
})
return t
}

Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ require (
github.com/charmbracelet/bubbles v0.16.1
github.com/charmbracelet/bubbletea v0.24.2
github.com/charmbracelet/kancli v0.0.0-20230629174247-b2093471047b
github.com/charmbracelet/lipgloss v0.7.1
github.com/charmbracelet/lipgloss v0.9.1
github.com/mattn/go-sqlite3 v1.14.17
github.com/muesli/go-app-paths v0.2.2
github.com/spf13/cobra v1.7.0
golang.org/x/term v0.9.0
)

require (
Expand All @@ -19,9 +18,9 @@ require (
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
Expand All @@ -32,5 +31,6 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/term v0.9.0 // indirect
golang.org/x/text v0.10.0 // indirect
)
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ github.com/charmbracelet/bubbletea v0.24.2 h1:uaQIKx9Ai6Gdh5zpTbGiWpytMU+CfsPp06
github.com/charmbracelet/bubbletea v0.24.2/go.mod h1:XdrNrV4J8GiyshTtx3DNuYkR1FDaJmO3l2nejekbsgg=
github.com/charmbracelet/kancli v0.0.0-20230629174247-b2093471047b h1:ElLjniv+gMcqCK+RyqDnquzp+CP9uslGjfyJKABSLfU=
github.com/charmbracelet/kancli v0.0.0-20230629174247-b2093471047b/go.mod h1:hq6p8QuwQr/Fsj1nZou17b0taqWI09Nz8GvzVBNaW4s=
github.com/charmbracelet/lipgloss v0.7.1 h1:17WMwi7N1b1rVWOjMT+rCh7sQkvDU75B2hbZpc5Kc1E=
github.com/charmbracelet/lipgloss v0.7.1/go.mod h1:yG0k3giv8Qj8edTCbbg6AlQ5e8KNWpFujkNawKNhE2c=
github.com/charmbracelet/lipgloss v0.9.1 h1:PNyd3jvaJbg4jRHKWXnCj1akQm4rh8dbEzN1p/u1KWg=
github.com/charmbracelet/lipgloss v0.9.1/go.mod h1:1mPmG4cxScwUQALAAnacHaigiiHB9Pmr+v1VEawJl6I=
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 h1:q2hJAaP1k2wIvVRd/hEHD7lacgqrCPS+k8g1MndzfWY=
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
Expand All @@ -18,13 +18,13 @@ github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLf
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=
github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM=
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
Expand Down

0 comments on commit 2a7eea9

Please sign in to comment.