-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathhandler.go
102 lines (78 loc) · 1.85 KB
/
handler.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
package app
import (
tea "github.com/charmbracelet/bubbletea"
"github.com/hedhyw/json-log-viewer/internal/pkg/source"
)
const cellIDLogLevel = 1
func (m Model) handleKeyMsg(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
for _, key := range m.table.KeyMap.LineUp.Keys() {
if msg.String() == key {
return m.handleUp()
}
}
switch msg.String() {
case "esc":
return m.back()
case "ctrl+c":
return m.quit()
case "q":
return m.quitByRune()
case "enter":
return m.handleEnter()
case "f":
return m.showFilter()
default:
return nil, nil
}
}
func (m Model) handleEnter() (tea.Model, tea.Cmd) {
if m.IsErrorShown() {
return m.quit()
}
if m.IsFilterShown() {
return m.applyFilter()
}
if m.IsTableShown() || m.IsJSONShown() {
return m.toggleLogEntity()
}
return nil, nil
}
func (m Model) handleUp() (tea.Model, tea.Cmd) {
if m.table.Cursor() != 0 || !m.IsTableShown() || m.IsFiltered() {
return nil, nil
}
m.allLogEntries = nil
return m, m.Init()
}
func (m Model) handleWindowSizeMsg(msg tea.WindowSizeMsg) Model {
x, y := m.baseStyle.GetFrameSize()
m.table.SetWidth(msg.Width - x*2)
m.table.SetHeight(msg.Height - y*2 - footerSize)
m.table.SetColumns(getColumns(m.table.Width() - 10))
m.lastWindowSize = msg
return m
}
func (m Model) handleLogEntriesMsg(msg source.LogEntries) Model {
if len(m.allLogEntries) == 0 {
m.allLogEntries = msg
}
m.table.SetRows(msg.Rows())
m.filteredLogEntries = msg
tableStyles := getTableStyles()
tableStyles.RenderCell = func(value string, rowID, columnID int) string {
style := tableStyles.Cell
if columnID == cellIDLogLevel {
return removeClearSequence(
m.getLogLevelStyle(style, rowID).Render(value),
)
}
return style.Render(value)
}
m.table.SetStyles(tableStyles)
m.table.UpdateViewport()
return m
}
func (m Model) handleErrorMsg(err error) Model {
m.err = err
return m
}