Compare commits

..

1 Commits

Author SHA1 Message Date
94c2008d9e
WIP: buffers and command history [ci skip]
Some checks failed
continuous-integration/drone/pr Build is failing
2024-02-01 18:28:56 +01:00
2 changed files with 34 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package ui
import (
"bufio"
"fmt"
"os"
"path"
@ -19,6 +20,7 @@ const (
// BufferHandler is the concrete functionality of a Buffer.
type BufferHandler interface {
open(m *model) error // gracefully open a new buffer
validate(m model, input string) error // validate incoming input
handle(input, hiddenInput string) tea.Msg // handle and dispatch messages from input
persist(m *model, input string) // store buffer input for persistence
@ -43,6 +45,31 @@ type Buffer struct {
// the status of ACN connectivity.
type StatusBufferHandler Buffer
func (s StatusBufferHandler) open(m *model) error {
buffer := m.buffers[m.menuState]
cairdeLogsDir := path.Join(m.userDir, "logs")
bufferLogsPath := path.Join(cairdeLogsDir, buffer.name)
f, err := os.OpenFile(bufferLogsPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0664)
if err != nil {
return fmt.Errorf("unable to create/open file %s: %s", bufferLogsPath, err)
}
scanner := bufio.NewScanner(f)
for scanner.Scan() {
buffer.inputHistory = append(buffer.inputHistory, scanner.Text())
}
if err := scanner.Err(); err != nil {
return fmt.Errorf("unable to read lines from %s : %s", bufferLogsPath, err)
}
if err := f.Close(); err != nil {
return fmt.Errorf("unable to close %s file handler: %s", bufferLogsPath, err)
}
return nil
}
func (s StatusBufferHandler) validate(m model, input string) error {
if string(input[0]) != "/" {
return fmt.Errorf("Woops, this is not a chat buffer. Only commands are allowed")

View File

@ -171,6 +171,7 @@ func (m model) Init() tea.Cmd {
return tea.Batch(
textinput.Blink,
m.receiveStatusCmd,
// TODO: open the buffers and return an error if that fails!
)
}
@ -225,6 +226,12 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.KeyMsg:
switch msg.String() {
case "up":
m.input.Reset()
lastCmd := m.statusBuffer.inputHistory[len(m.statusBuffer.inputHistory)-1]
m.statusBuffer.viewport.SetContent(lastCmd)
case "ctrl+c":
cmds = append(cmds, func() tea.Msg {
return turnAcnOff(&m, true)