Skip to content

Commit

Permalink
Added command history
Browse files Browse the repository at this point in the history
  • Loading branch information
bahner committed Feb 22, 2024
1 parent 05bbcf8 commit 47c3d83
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 26 deletions.
70 changes: 69 additions & 1 deletion ui/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"time"

"github.com/bahner/go-ma"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)

// // displaySelfMessage writes a message from ourself to the message window,
Expand All @@ -25,9 +27,76 @@ func (ui *ChatUI) displaySystemMessage(msg string) {
fmt.Fprintf(ui.msgW, "%s %s\n", prompt, msg)
}

func (ui *ChatUI) setupInputField(inputField *tview.InputField, app *tview.Application) {
inputField.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyUp:
if ui.currentHistoryIndex < len(ui.inputHistory)-1 {
ui.currentHistoryIndex++
input := ui.inputHistory[len(ui.inputHistory)-1-ui.currentHistoryIndex]
inputField.SetText(input)
return nil // event handled
}
case tcell.KeyDown:
if ui.currentHistoryIndex > 0 {
ui.currentHistoryIndex--
input := ui.inputHistory[len(ui.inputHistory)-1-ui.currentHistoryIndex]
inputField.SetText(input)
return nil // event handled
} else if ui.currentHistoryIndex == 0 {
ui.currentHistoryIndex = -1
inputField.SetText("") // Clear the input field
return nil // event handled
}
}
return event // let other keys pass through
})

inputField.SetDoneFunc(func(key tcell.Key) {
if key == tcell.KeyEnter {
input := inputField.GetText()
if input != "" {
ui.inputHistory = append(ui.inputHistory, input) // Add to history
ui.currentHistoryIndex = -1 // Reset index
ui.chInput <- input // Send input to be handled
inputField.SetText("") // Clear the input field
}
}
})

// the done func is called when the user hits enter, or tabs out of the field
inputField.SetDoneFunc(func(key tcell.Key) {
if key != tcell.KeyEnter {
// we don't want to do anything if they just tabbed away
return
}

line := inputField.GetText()
if len(line) == 0 {
// ignore blank lines
return
}

// bail if requested
if line == "/quit" {
app.Stop()
return
}

ui.inputHistory = append(ui.inputHistory, line)
ui.currentHistoryIndex = -1

// send the line onto the input chan and reset the field text
ui.chInput <- line
inputField.SetText("")
})

}

// handleEvents runs an event loop that sends user input to the chat room
// and displays messages received from the chat room. It also periodically
// refreshes the list of peers in the UI.

func (ui *ChatUI) handleEvents() {
peerRefreshTicker := time.NewTicker(time.Second)
defer peerRefreshTicker.Stop()
Expand All @@ -46,7 +115,6 @@ func (ui *ChatUI) handleEvents() {
ui.displayBroadcastMessage(m)
continue
}

ui.displayChatMessage(m)

case <-peerRefreshTicker.C:
Expand Down
36 changes: 11 additions & 25 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ type ChatUI struct {
broadcastCtx context.Context
broadcastCancel context.CancelFunc

// History of entries
inputHistory []string
currentHistoryIndex int

// The Topic is used for publication of messages after encryption and signing.
// The names are obviously, from the corresponding DIDDocument.

Expand Down Expand Up @@ -85,29 +89,6 @@ func NewChatUI(p *p2p.P2P, a *actor.Actor) (*ChatUI, error) {
SetFieldWidth(0).
SetFieldBackgroundColor(tcell.ColorBlack)

// the done func is called when the user hits enter, or tabs out of the field
input.SetDoneFunc(func(key tcell.Key) {
if key != tcell.KeyEnter {
// we don't want to do anything if they just tabbed away
return
}
line := input.GetText()
if len(line) == 0 {
// ignore blank lines
return
}

// bail if requested
if line == "/quit" {
app.Stop()
return
}

// send the line onto the input chan and reset the field text
chInput <- line
input.SetText("")
})

// make a text view to hold the list of peers in the room, updated by ui.refreshPeers()
peersList := tview.NewTextView()
peersList.SetBorder(true)
Expand All @@ -129,7 +110,7 @@ func NewChatUI(p *p2p.P2P, a *actor.Actor) (*ChatUI, error) {

app.SetRoot(flex, true)

return &ChatUI{
ui := &ChatUI{
a: a,
p: p,
app: app,
Expand All @@ -139,7 +120,12 @@ func NewChatUI(p *p2p.P2P, a *actor.Actor) (*ChatUI, error) {
chInput: chInput,
chMessage: make(chan *msg.Message, UI_MESSAGES_CHANNEL_BUFFERSIZE),
chDone: make(chan struct{}, 1),
}, nil
}

// A little kludgy, but acceptable for now.
ui.setupInputField(input, app)

return ui, nil
}

// Run starts the chat event loop in the background, then starts
Expand Down

0 comments on commit 47c3d83

Please sign in to comment.