-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.go
85 lines (75 loc) · 1.49 KB
/
views.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"fmt"
"log"
"os"
"github.com/charmbracelet/bubbles/table"
"github.com/charmbracelet/lipgloss"
"golang.org/x/term"
)
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 * 2
default:
return p
}
}
const (
XS int = 1
SM int = 3
MD int = 5
LG int = 10
)
func setupTable(tasks []task) table.Model {
w, _, err := term.GetSize(int(os.Stdout.Fd()))
if err != nil {
log.Println("unable to calc height and width of term")
}
columns := []table.Column{
{Title: "ID", Width: calculateWidth(XS, w)},
{Title: "Name", Width: calculateWidth(LG, w)},
{Title: "Project", Width: calculateWidth(SM, w)},
{Title: "Status", Width: calculateWidth(SM, w)},
{Title: "Created At", Width: calculateWidth(MD, w)},
}
var rows []table.Row
for _, task := range tasks {
rows = append(rows, table.Row{
fmt.Sprintf("%d", task.ID),
task.Name,
task.Project,
task.Status,
task.Created.Format("2006-01-02"),
})
}
tbl := 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)
tbl.SetStyles(s)
return tbl
}