mirror of
https://github.com/shazow/ssh-chat.git
synced 2025-04-18 17:57:41 +03:00
Autocomplete commands.
This commit is contained in:
parent
391a66a876
commit
12402c2338
61
host.go
61
host.go
@ -24,7 +24,7 @@ func GetPrompt(user *chat.User) string {
|
|||||||
type Host struct {
|
type Host struct {
|
||||||
*chat.Room
|
*chat.Room
|
||||||
listener *sshd.SSHListener
|
listener *sshd.SSHListener
|
||||||
commands *chat.Commands
|
commands chat.Commands
|
||||||
|
|
||||||
motd string
|
motd string
|
||||||
auth *Auth
|
auth *Auth
|
||||||
@ -40,13 +40,13 @@ func NewHost(listener *sshd.SSHListener) *Host {
|
|||||||
h := Host{
|
h := Host{
|
||||||
Room: room,
|
Room: room,
|
||||||
listener: listener,
|
listener: listener,
|
||||||
|
commands: chat.Commands{},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make our own commands registry instance.
|
// Make our own commands registry instance.
|
||||||
commands := chat.Commands{}
|
chat.InitCommands(&h.commands)
|
||||||
chat.InitCommands(&commands)
|
h.InitCommands(&h.commands)
|
||||||
h.InitCommands(&commands)
|
room.SetCommands(h.commands)
|
||||||
room.SetCommands(commands)
|
|
||||||
|
|
||||||
go room.Serve()
|
go room.Serve()
|
||||||
return &h
|
return &h
|
||||||
@ -140,34 +140,61 @@ func (h *Host) Serve() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h Host) completeName(partial string) string {
|
||||||
|
names := h.NamesPrefix(partial)
|
||||||
|
if len(names) == 0 {
|
||||||
|
// Didn't find anything
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
return names[len(names)-1]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h Host) completeCommand(partial string) string {
|
||||||
|
for cmd, _ := range h.commands {
|
||||||
|
if strings.HasPrefix(cmd, partial) {
|
||||||
|
return cmd
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
// AutoCompleteFunction is a callback for terminal autocompletion
|
// AutoCompleteFunction is a callback for terminal autocompletion
|
||||||
func (h *Host) AutoCompleteFunction(line string, pos int, key rune) (newLine string, newPos int, ok bool) {
|
func (h *Host) AutoCompleteFunction(line string, pos int, key rune) (newLine string, newPos int, ok bool) {
|
||||||
if key != 9 {
|
if key != 9 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fields := strings.Fields(line[:pos])
|
if strings.HasSuffix(line[:pos], " ") {
|
||||||
partial := fields[len(fields)-1]
|
// Don't autocomplete spaces.
|
||||||
names := h.NamesPrefix(partial)
|
|
||||||
if len(names) == 0 {
|
|
||||||
// Didn't find anything
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
name := names[len(names)-1]
|
fields := strings.Fields(line[:pos])
|
||||||
|
isFirst := len(fields) < 2
|
||||||
|
partial := fields[len(fields)-1]
|
||||||
posPartial := pos - len(partial)
|
posPartial := pos - len(partial)
|
||||||
|
|
||||||
// Append suffix separator
|
var completed string
|
||||||
if len(fields) < 2 {
|
if isFirst && strings.HasPrefix(partial, "/") {
|
||||||
name += ": "
|
// Command
|
||||||
|
completed = h.completeCommand(partial)
|
||||||
} else {
|
} else {
|
||||||
name += " "
|
// Name
|
||||||
|
completed = h.completeName(partial)
|
||||||
|
if completed == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if isFirst {
|
||||||
|
completed += ":"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
completed += " "
|
||||||
|
|
||||||
// Reposition the cursor
|
// Reposition the cursor
|
||||||
newLine = strings.Replace(line[posPartial:], partial, name, 1)
|
newLine = strings.Replace(line[posPartial:], partial, completed, 1)
|
||||||
newLine = line[:posPartial] + newLine
|
newLine = line[:posPartial] + newLine
|
||||||
newPos = pos + (len(name) - len(partial))
|
newPos = pos + (len(completed) - len(partial))
|
||||||
ok = true
|
ok = true
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user