add tab completion for slash commands
This commit is contained in:
33
ui.go
33
ui.go
@@ -125,8 +125,14 @@ func (b *Barnard) OnScrollOutputBottom(ui *uiterm.Ui, key uiterm.Key) {
|
|||||||
b.UiOutput.ScrollBottom()
|
b.UiOutput.ScrollBottom()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var commands = []string{"/clear", "/deafen", "/exit", "/help", "/quit"}
|
||||||
|
|
||||||
func (b *Barnard) OnFocusPress(ui *uiterm.Ui, key uiterm.Key) {
|
func (b *Barnard) OnFocusPress(ui *uiterm.Ui, key uiterm.Key) {
|
||||||
active := b.Ui.Active()
|
active := b.Ui.Active()
|
||||||
|
if active == uiViewInput && strings.HasPrefix(b.UiInput.Text, "/") {
|
||||||
|
b.completeCommand()
|
||||||
|
return
|
||||||
|
}
|
||||||
if active == uiViewInput {
|
if active == uiViewInput {
|
||||||
b.Ui.SetActive(uiViewTree)
|
b.Ui.SetActive(uiViewTree)
|
||||||
} else if active == 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) {
|
func (b *Barnard) OnTextInput(ui *uiterm.Ui, textbox *uiterm.Textbox, text string) {
|
||||||
if text == "" {
|
if text == "" {
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user