dynamo-browse/internal/slog-view/ui/model.go

84 lines
2.5 KiB
Go
Raw Normal View History

2022-03-30 04:07:49 +00:00
package ui
import (
tea "github.com/charmbracelet/bubbletea"
2022-07-28 11:36:16 +00:00
"github.com/lmika/audax/internal/common/ui/commandctrl"
"github.com/lmika/audax/internal/dynamo-browse/ui/teamodels/layout"
"github.com/lmika/audax/internal/dynamo-browse/ui/teamodels/statusandprompt"
"github.com/lmika/audax/internal/slog-view/controllers"
"github.com/lmika/audax/internal/slog-view/styles"
"github.com/lmika/audax/internal/slog-view/ui/fullviewlinedetails"
"github.com/lmika/audax/internal/slog-view/ui/linedetails"
"github.com/lmika/audax/internal/slog-view/ui/loglines"
2022-03-30 04:07:49 +00:00
)
type Model struct {
2022-06-27 06:05:59 +00:00
controller *controllers.LogFileController
cmdController *commandctrl.CommandController
2022-03-30 04:07:49 +00:00
2022-06-27 06:05:59 +00:00
root tea.Model
logLines *loglines.Model
lineDetails *linedetails.Model
statusAndPrompt *statusandprompt.StatusAndPrompt
2022-03-30 04:07:49 +00:00
fullViewLineDetails *fullviewlinedetails.Model
}
func NewModel(controller *controllers.LogFileController, cmdController *commandctrl.CommandController) Model {
2022-06-27 06:05:59 +00:00
defaultStyles := styles.DefaultStyles
logLines := loglines.New(defaultStyles.Frames)
lineDetails := linedetails.New(defaultStyles.Frames)
2022-03-30 04:07:49 +00:00
box := layout.NewVBox(layout.LastChildFixedAt(17), logLines, lineDetails)
2022-06-27 06:05:59 +00:00
fullViewLineDetails := fullviewlinedetails.NewModel(box, defaultStyles.Frames)
statusAndPrompt := statusandprompt.New(fullViewLineDetails, "", defaultStyles.StatusAndPrompt)
2022-03-30 04:07:49 +00:00
root := layout.FullScreen(statusAndPrompt)
return Model{
2022-06-27 06:05:59 +00:00
controller: controller,
cmdController: cmdController,
root: root,
statusAndPrompt: statusAndPrompt,
logLines: logLines,
lineDetails: lineDetails,
2022-03-30 04:07:49 +00:00
fullViewLineDetails: fullViewLineDetails,
}
}
func (m Model) Init() tea.Cmd {
return m.controller.ReadLogFile()
}
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case controllers.NewLogFile:
m.logLines.SetLogFile(msg)
case controllers.ViewLogLineFullScreen:
m.fullViewLineDetails.ViewItem(msg)
case loglines.NewLogLineSelected:
m.lineDetails.SetSelectedItem(msg)
case tea.KeyMsg:
if !m.statusAndPrompt.InPrompt() {
switch msg.String() {
// TEMP
case ":":
return m, func() tea.Msg { return m.cmdController.Prompt() }
2022-03-30 04:07:49 +00:00
case "w":
return m, m.controller.ViewLogLineFullScreen(m.logLines.SelectedLogLine())
// END TEMP
case "ctrl+c", "q":
return m, tea.Quit
}
}
}
newRoot, cmd := m.root.Update(msg)
m.root = newRoot
return m, cmd
}
func (m Model) View() string {
return m.root.View()
}