diff --git a/ui.go b/ui.go index a9513eb..716e548 100644 --- a/ui.go +++ b/ui.go @@ -125,8 +125,14 @@ func (b *Barnard) OnScrollOutputBottom(ui *uiterm.Ui, key uiterm.Key) { b.UiOutput.ScrollBottom() } +var commands = []string{"/clear", "/deafen", "/exit", "/help", "/quit"} + func (b *Barnard) OnFocusPress(ui *uiterm.Ui, key uiterm.Key) { active := b.Ui.Active() + if active == uiViewInput && strings.HasPrefix(b.UiInput.Text, "/") { + b.completeCommand() + return + } if active == uiViewInput { b.Ui.SetActive(uiViewTree) } else if active == uiViewTree { @@ -134,6 +140,33 @@ func (b *Barnard) OnFocusPress(ui *uiterm.Ui, key uiterm.Key) { } } +func (b *Barnard) completeCommand() { + text := b.UiInput.Text + var matches []string + for _, cmd := range commands { + if strings.HasPrefix(cmd, text) { + matches = append(matches, cmd) + } + } + if len(matches) == 0 { + return + } + if len(matches) == 1 { + b.UiInput.Text = matches[0] + } else { + prefix := matches[0] + for _, m := range matches[1:] { + for !strings.HasPrefix(m, prefix) { + prefix = prefix[:len(prefix)-1] + } + } + if len(prefix) > len(text) { + b.UiInput.Text = prefix + } + } + b.Ui.Refresh() +} + func (b *Barnard) OnTextInput(ui *uiterm.Ui, textbox *uiterm.Textbox, text string) { if text == "" { return