add tab completion for slash commands

This commit is contained in:
Username
2026-02-24 09:54:07 +01:00
parent 72d549eade
commit f190e073ba

33
ui.go
View File

@@ -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