|
1 | 1 | package app |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "github.com/charmbracelet/bubbles/table" |
5 | | - "github.com/charmbracelet/bubbles/textinput" |
6 | 4 | tea "github.com/charmbracelet/bubbletea" |
7 | 5 | "github.com/charmbracelet/lipgloss" |
8 | 6 |
|
9 | 7 | "github.com/hedhyw/json-log-viewer/internal/pkg/config" |
10 | | - "github.com/hedhyw/json-log-viewer/internal/pkg/source" |
11 | 8 | ) |
12 | 9 |
|
13 | | -// Model of the application. |
14 | | -type Model struct { |
15 | | - config *config.Config |
| 10 | +// Application global state. |
| 11 | +type Application struct { |
| 12 | + Path string |
| 13 | + Config *config.Config |
16 | 14 |
|
17 | | - baseStyle lipgloss.Style |
18 | | - footerStyle lipgloss.Style |
| 15 | + BaseStyle lipgloss.Style |
| 16 | + FooterStyle lipgloss.Style |
19 | 17 |
|
20 | | - fileLogPath string |
21 | | - |
22 | | - table table.Model |
23 | | - allLogEntries source.LogEntries |
24 | | - |
25 | | - filteredLogEntries source.LogEntries |
26 | | - |
27 | | - lastWindowSize tea.WindowSizeMsg |
28 | | - jsonView tea.Model |
29 | | - |
30 | | - textInputShown bool |
31 | | - textInput textinput.Model |
32 | | - |
33 | | - err error |
| 18 | + LastWindowSize tea.WindowSizeMsg |
34 | 19 | } |
35 | 20 |
|
36 | | -// NewModel initializes a new application model. It accept the path |
37 | | -// to the file with logs. |
38 | | -func NewModel(path string, cfg *config.Config) Model { |
39 | | - tableLogs := table.New( |
40 | | - table.WithColumns(getColumns(100, cfg)), |
41 | | - table.WithFocused(true), |
42 | | - table.WithHeight(7), |
| 21 | +func newApplication(path string, config *config.Config) Application { |
| 22 | + const ( |
| 23 | + initialWidth = 70 |
| 24 | + initialHeight = 20 |
43 | 25 | ) |
44 | 26 |
|
45 | | - tableLogs.SetStyles(getTableStyles()) |
46 | | - |
47 | | - return Model{ |
48 | | - config: cfg, |
49 | | - |
50 | | - baseStyle: getBaseStyle(), |
51 | | - footerStyle: getFooterStyle(), |
52 | | - |
53 | | - fileLogPath: path, |
54 | | - table: tableLogs, |
| 27 | + return Application{ |
| 28 | + Path: path, |
| 29 | + Config: config, |
55 | 30 |
|
56 | | - err: nil, |
57 | | - allLogEntries: nil, |
58 | | - filteredLogEntries: nil, |
| 31 | + BaseStyle: getBaseStyle(), |
| 32 | + FooterStyle: getFooterStyle(), |
59 | 33 |
|
60 | | - textInputShown: false, |
61 | | - textInput: textinput.Model{}, |
62 | | - |
63 | | - lastWindowSize: tea.WindowSizeMsg{}, |
64 | | - jsonView: nil, |
65 | | - } |
66 | | -} |
67 | | - |
68 | | -// Init implements team.Model interface. |
69 | | -func (m Model) Init() tea.Cmd { |
70 | | - return source.LoadLogsFromFile(m.fileLogPath, m.config) |
71 | | -} |
72 | | - |
73 | | -// Update implements team.Model interface. |
74 | | -func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { |
75 | | - switch msg := msg.(type) { |
76 | | - case tea.WindowSizeMsg: |
77 | | - m = m.handleWindowSizeMsg(msg) |
78 | | - case source.LogEntries: |
79 | | - m = m.handleLogEntriesMsg(msg) |
80 | | - case error: |
81 | | - m = m.handleErrorMsg(msg) |
82 | | - |
83 | | - return m, nil |
84 | | - case tea.KeyMsg: |
85 | | - newModel, cmd := m.handleKeyMsg(msg) |
86 | | - if newModel != nil || cmd != nil { |
87 | | - return newModel, cmd |
88 | | - } |
| 34 | + LastWindowSize: tea.WindowSizeMsg{ |
| 35 | + Width: initialWidth, |
| 36 | + Height: initialHeight, |
| 37 | + }, |
89 | 38 | } |
90 | | - |
91 | | - return m.handleUpdateInViews(msg) |
92 | 39 | } |
93 | 40 |
|
94 | | -// View implements team.Model interface. |
95 | | -func (m Model) View() string { |
96 | | - return m.renderViews() |
| 41 | +// NewModel initializes a new application model. It accept the path |
| 42 | +// to the file with logs. |
| 43 | +func NewModel(path string, config *config.Config) tea.Model { |
| 44 | + return newStateInitial(newApplication(path, config)) |
97 | 45 | } |
0 commit comments