forked from chainreactors/tui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable.go
More file actions
162 lines (143 loc) · 3.48 KB
/
Copy pathtable.go
File metadata and controls
162 lines (143 loc) · 3.48 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package tui
import (
"bytes"
"fmt"
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/evertras/bubble-table/table"
"os"
)
var (
styleBase = lipgloss.NewStyle().
Align(lipgloss.Left)
)
func NewTable(columns []table.Column, isStatic bool) *TableModel {
var newTable = table.Model{}
if isStatic {
newTable = table.New(columns).WithFooterVisibility(false).
BorderRounded().WithBaseStyle(styleBase)
} else {
newTable = table.New(columns).Filtered(true).
BorderRounded().Focused(true).WithPageSize(10).WithBaseStyle(styleBase)
}
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
*bytes.Buffer
currentPage int
totalPages int
rowsPerPage int
isStatic bool
filtered bool
handle func()
Title string
selected table.Row
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.selected = t.GetHighlightedRow()
if t.handle == nil {
return t, tea.Quit
}
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.selected
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)
}
func (t *TableModel) Run() error {
p := tea.NewProgram(t)
_, err := p.Run()
if err != nil {
return err
}
fmt.Printf(HelpStyle("<Press enter to exit>\n"))
os.Stdin.Write([]byte("\n"))
ClearLines(1)
return nil
}