AutoCompleteFunction is back.

This commit is contained in:
Andrey Petrov 2014-12-25 00:37:50 -08:00
parent d3b0a56d78
commit 3bb4bbf991
2 changed files with 31 additions and 15 deletions

View File

@ -83,3 +83,14 @@ func (ch *Channel) Topic() string {
func (ch *Channel) SetTopic(s string) {
ch.topic = s
}
// NamesPrefix lists all members' names with a given prefix, used to query
// for autocompletion purposes.
func (ch *Channel) NamesPrefix(prefix string) []string {
users := ch.users.ListPrefix(prefix)
names := make([]string, len(users))
for i, u := range users {
names[i] = u.(*User).Name()
}
return names
}

35
host.go
View File

@ -3,6 +3,7 @@ package main
import (
"fmt"
"io"
"strings"
"github.com/shazow/ssh-chat/chat"
"github.com/shazow/ssh-chat/sshd"
@ -29,7 +30,7 @@ func (h *Host) Connect(term *sshd.Terminal) {
defer term.Close()
name := term.Conn.User()
term.SetPrompt(fmt.Sprintf("[%s] ", name))
// TODO: term.AutoCompleteCallback = ...
term.AutoCompleteCallback = h.AutoCompleteFunction
user := chat.NewUserScreen(name, term)
defer user.Close()
@ -70,30 +71,34 @@ func (h *Host) Serve() {
}
}
/* TODO: ...
// AutoCompleteFunction is a callback for terminal autocompletion
func (h *Host) AutoCompleteFunction(line string, pos int, key rune) (newLine string, newPos int, ok bool) {
if key != 9 {
return
}
shortLine := strings.Split(line[:pos], " ")
partialNick := shortLine[len(shortLine)-1]
nicks := h.channel.users.ListPrefix(&partialNick)
if len(nicks) == 0 {
fields := strings.Fields(line[:pos])
partial := fields[len(fields)-1]
names := h.channel.NamesPrefix(partial)
if len(names) == 0 {
// Didn't find anything
return
}
nick := nicks[len(nicks)-1]
posPartialNick := pos - len(partialNick)
if len(shortLine) < 2 {
nick += ": "
name := names[len(names)-1]
posPartial := pos - len(partial)
// Append suffix separator
if len(fields) < 2 {
name += ": "
} else {
nick += " "
name += " "
}
newLine = strings.Replace(line[posPartialNick:], partialNick, nick, 1)
newLine = line[:posPartialNick] + newLine
newPos = pos + (len(nick) - len(partialNick))
// Reposition the cursor
newLine = strings.Replace(line[posPartial:], partial, name, 1)
newLine = line[:posPartial] + newLine
newPos = pos + (len(name) - len(partial))
ok = true
return
}
*/