Skip to content

Commit d671e55

Browse files
authored
refactor: organize behavioral states (#39)
1 parent e3aacf1 commit d671e55

30 files changed

+1668
-701
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ lint: bin/golangci-lint
2929
test:
3030
go test \
3131
-coverpkg=${COVER_PACKAGES} \
32-
-covermode=count \
32+
-covermode=atomic \
33+
-race \
3334
-coverprofile=coverage.out \
3435
./...
3536
go tool cover -func=coverage.out

assets/example.log

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@
4545
{"time":"1970-01-01T00:00:00.00","level":"DEBUG","message": "A house divided against itself cannot stand.","author": "Abraham Lincoln"}
4646
{"time":"1970-01-01T00:00:00.00","level":"TRACE","message": "Difficulties increase the nearer we get to the goal.","author": "Johann Wolfgang von Goethe"}
4747
plain text log
48-
{"time":"12444334.222","level":"TRACE","message": "Wealth consists not in having great possessions, but in having few wants.","author": "Epictetus"}
49-
{"time":12444334.222,"level":"INFO","message": "Laugh at the world's foolishness or weep over it, you will regret both","author": "Kierkegaard"}
50-
{"time":12444334,"level":"WARN","message": "Wealth consists not in having great possessions, but in having few wants.","author": "Epictetus"}
51-
{"time":"12444334","level":"DEBUG","message": "Wealth consists not in having great possessions, but in having few wants.","author": "Epictetus"}
52-
{"time":124999444443,"level":"TRACE","message": "Money doesn't talk, it swears","author": "Bob Dylan"}
53-
{"time":124999444443.45,"level":"WARN","message": "If a man knows not to which port he sails, no wind is favorable","author": "Seneca"}
54-
{"time":"124999444443.45","level":"VERBOSE","message": "Begin at once to live, and count each separate day as a separate life","author": "Seneca"}
55-
{"time":12499944444398.45,"level":"VERBOSE","message": "Begin at once to live, and count each separate day as a separate life","author": "Seneca"}
48+
{"time":"725850000.222","level":"TRACE","message": "Wealth consists not in having great possessions, but in having few wants.","author": "Epictetus"}
49+
{"time":757386000.222,"level":"INFO","message": "Laugh at the world's foolishness or weep over it, you will regret both","author": "Kierkegaard"}
50+
{"time":788922000,"level":"WARN","message": "Wealth consists not in having great possessions, but in having few wants.","author": "Epictetus"}
51+
{"time":"820454400","level":"DEBUG","message": "Wealth consists not in having great possessions, but in having few wants.","author": "Epictetus"}
52+
{"time":865900800,"level":"TRACE","message": "Money doesn't talk, it swears","author": "Bob Dylan"}
53+
{"time":883612800.45,"level":"WARN","message": "If a man knows not to which port he sails, no wind is favorable","author": "Seneca"}
54+
{"time":"915148800000.45","level":"VERBOSE","message": "Begin at once to live, and count each separate day as a separate life","author": "Seneca"}
55+
{"time":946684800.45,"level":"VERBOSE","message": "Begin at once to live, and count each separate day as a separate life","author": "Seneca"}

internal/app/action.go

Lines changed: 0 additions & 107 deletions
This file was deleted.

internal/app/app.go

Lines changed: 24 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,45 @@
11
package app
22

33
import (
4-
"github.com/charmbracelet/bubbles/table"
5-
"github.com/charmbracelet/bubbles/textinput"
64
tea "github.com/charmbracelet/bubbletea"
75
"github.com/charmbracelet/lipgloss"
86

97
"github.com/hedhyw/json-log-viewer/internal/pkg/config"
10-
"github.com/hedhyw/json-log-viewer/internal/pkg/source"
118
)
129

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
1614

17-
baseStyle lipgloss.Style
18-
footerStyle lipgloss.Style
15+
BaseStyle lipgloss.Style
16+
FooterStyle lipgloss.Style
1917

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
3419
}
3520

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
4325
)
4426

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,
5530

56-
err: nil,
57-
allLogEntries: nil,
58-
filteredLogEntries: nil,
31+
BaseStyle: getBaseStyle(),
32+
FooterStyle: getFooterStyle(),
5933

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+
},
8938
}
90-
91-
return m.handleUpdateInViews(msg)
9239
}
9340

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))
9745
}

0 commit comments

Comments
 (0)