-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.go
141 lines (123 loc) · 3.08 KB
/
table.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package tui
import (
"fmt"
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/evertras/bubble-table/table"
)
var (
styleBase = HeaderStyle
)
func NewTable(columns []table.Column, isStatic bool) *TableModel {
var newTable = table.Model{}
if isStatic {
newTable = table.New(columns).WithFooterVisibility(false).
BorderRounded()
} else {
newTable = table.New(columns).Filtered(true).
BorderRounded().Focused(true).WithPageSize(10)
}
keyMap := table.DefaultKeyMap()
keyMap.RowSelectToggle = key.NewBinding(
key.WithKeys("enter"),
key.WithHelp("enter", "select row"),
)
newTable = newTable.WithKeyMap(keyMap)
newTable = newTable.HighlightStyle(SelectStyle)
t := &TableModel{
table: newTable,
Columns: columns,
rowsPerPage: 10,
isStatic: isStatic,
}
return t
}
type TableModel struct {
table table.Model
Columns []table.Column
Rows []table.Row
AllRows []table.Row
currentPage int
totalPages int
rowsPerPage int
isStatic bool
filtered bool
handle func()
Title string
highlightRows []int
searchString string
highlightStyle lipgloss.Style
}
func (t *TableModel) UpdatePagination() {
t.totalPages = (len(t.Rows) + t.rowsPerPage - 1) / t.rowsPerPage
if t.currentPage > t.totalPages {
t.currentPage = t.totalPages
}
if t.currentPage < 1 {
t.currentPage = 1
}
}
func (t *TableModel) Init() tea.Cmd { return nil }
func (t *TableModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.Type {
case tea.KeyCtrlC, tea.KeyEsc, tea.KeyCtrlQ:
if len(t.highlightRows) > 0 {
t.CleanHighlight()
t.SetRows(t.AllRows)
return t, nil
}
return t, tea.Quit
case tea.KeyEnter:
t.handleSelectedRow()
return t, tea.Quit
}
}
t.table, cmd = t.table.Update(msg)
return t, tea.Batch(cmd)
}
func (t *TableModel) View() string {
if t.isStatic {
t.table.WithPageSize(len(t.Rows))
return fmt.Sprintf("%s\n", t.Title) + "\n" + t.table.View() + "\n"
}
return fmt.Sprintf("%s\n", t.Title) + "\n" + t.table.View() + "\n"
}
func (t *TableModel) SetRows(rows []table.Row) {
t.Rows = rows
t.table = t.table.WithRows(rows)
}
func (t *TableModel) handleSelectedRow() {
t.handle()
}
func (t *TableModel) SetHandle(handle func()) {
t.handle = handle
}
func (t *TableModel) CleanHighlight() {
t.highlightRows = []int{}
t.searchString = ""
}
func (t *TableModel) SetMultiline() {
t.table = t.table.WithMultiline(true)
}
func (t *TableModel) GetSelectedRow() []table.Row {
selectedRow := t.table.SelectedRows()
return selectedRow
}
func (t *TableModel) GetHighlightedRow() table.Row {
selectedRow := t.table.HighlightedRow()
return selectedRow
}
func (t *TableModel) pageView(s string) string {
style := lipgloss.NewStyle().Inline(true).Align(lipgloss.Left)
return style.Render(s)
}
func (t *TableModel) SetAscSort(s string) {
t.table = t.table.SortByAsc(s)
}
func (t *TableModel) SetDescSort(s string) {
t.table = t.table.SortByDesc(s)
}