@@ -2,18 +2,15 @@ package main
22
33import (
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
0 commit comments