Compare commits

..

1 Commits

Author SHA1 Message Date
60d3c0bf97
WIP: buffers and command history [ci skip]
Some checks failed
continuous-integration/drone/pr Build is failing
continuous-integration/drone/push Build is failing
2024-02-04 11:38:11 +01:00
2 changed files with 56 additions and 12 deletions

View File

@ -29,13 +29,14 @@ type BufferHandler interface {
// Buffer is a user-facing interactive buffer in the TUI.
type Buffer struct {
name string // user-friendly buffer name for the menu
inputChannel chan string // channel for passing input to handlers
viewport viewport.Model // viewport for rendering lines
viewportLines []string // lines for viewport renderer
viewportIsReady bool // whether or not the viewport is ready to render
inputHistory []string // buffer input history for optional persistence
persistence bool // whether or not to persist buffer input to file
name string // user-friendly buffer name for the menu
inputChannel chan string // channel for passing input to handlers
viewport viewport.Model // viewport for rendering lines
viewportLines []string // lines for viewport renderer
viewportIsReady bool // whether or not the viewport is ready to render
inputHistory []string // buffer input history for optional persistence
inputHistoryCursor int // where we are in the input history
persistence bool // whether or not to persist buffer input to file
BufferHandler
}
@ -67,6 +68,14 @@ func (s StatusBufferHandler) open(m *model) error {
return fmt.Errorf("unable to close %s file handler: %s", bufferLogsPath, err)
}
buffer.inputHistoryCursor = 0
if len(buffer.inputHistory) > 0 {
buffer.inputHistoryCursor = len(buffer.inputHistory) - 1
}
m.buffers[m.menuState] = buffer
m.statusBuffer = buffer
return nil
}
@ -158,6 +167,7 @@ func (s StatusBufferHandler) persist(m *model, input string) {
buffer := m.buffers[m.menuState]
if buffer.persistence {
buffer.inputHistory = append(buffer.inputHistory, input)
buffer.inputHistoryCursor++
m.buffers[m.menuState] = buffer
}
}
@ -171,7 +181,7 @@ func (s StatusBufferHandler) close(m model) error {
return nil
}
f, err := os.OpenFile(bufferLogsPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0664)
f, err := os.OpenFile(bufferLogsPath, os.O_RDWR|os.O_CREATE, 0664)
if err != nil {
return fmt.Errorf("unable to create/open file %s: %s", bufferLogsPath, err)
}

View File

@ -171,7 +171,12 @@ func (m model) Init() tea.Cmd {
return tea.Batch(
textinput.Blink,
m.receiveStatusCmd,
// TODO: open the buffers and return an error if that fails!
func() tea.Msg {
if err := m.statusBuffer.open(&m); err != nil {
return cmdMsg{output: []string{err.Error()}}
}
return nil
},
)
}
@ -226,11 +231,40 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.KeyMsg:
switch msg.String() {
case "up":
case "down":
buffer := m.buffers[m.menuState]
if len(buffer.inputHistory) == 0 {
break
}
m.input.Reset()
lastCmd := m.statusBuffer.inputHistory[len(m.statusBuffer.inputHistory)-1]
m.statusBuffer.viewport.SetContent(lastCmd)
if buffer.inputHistoryCursor < len(buffer.inputHistory)-1 {
buffer.inputHistoryCursor++
}
m.buffers[m.menuState] = buffer
lastCmd := buffer.inputHistory[buffer.inputHistoryCursor]
m.input.SetValue(lastCmd)
case "up":
buffer := m.buffers[m.menuState]
if len(buffer.inputHistory) == 0 {
break
}
m.input.Reset()
if buffer.inputHistoryCursor > 0 {
buffer.inputHistoryCursor--
}
m.buffers[m.menuState] = buffer
lastCmd := buffer.inputHistory[buffer.inputHistoryCursor]
m.input.SetValue(lastCmd)
case "ctrl+c":
cmds = append(cmds, func() tea.Msg {