diff --git a/README.md b/README.md index 838485f..42d25c2 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ things up with `make run`. * [x] set term width properly * [x] client map rather than list * [x] backfill chat history -* [ ] tab completion +* [x] tab completion * [x] /ban * [ ] /ban by ip * [x] /help @@ -88,7 +88,7 @@ things up with `make run`. * [ ] More tests. * [ ] Even more tests. * [ ] Lots of refactoring - * [ ] Pull out the chat-related stuff into isolation from the ssh serving +* [ ] Pull out the chat-related stuff into isolation from the ssh serving stuff diff --git a/client.go b/client.go index 33bd4f7..1de35b1 100644 --- a/client.go +++ b/client.go @@ -29,7 +29,6 @@ const ABOUT_TEXT string = `-> ssh-chat is made by @shazow. For more, visit shazow.net or follow at twitter.com/shazow ` - type Client struct { Server *Server Conn *ssh.ServerConn @@ -56,7 +55,7 @@ func NewClient(server *Server, conn *ssh.ServerConn) *Client { } func (c *Client) ColoredName() string { - return ColorString(c.Color, c.Name) + return ColorString(c.Color, c.Name) } func (c *Client) Write(msg string) { @@ -257,6 +256,8 @@ func (c *Client) handleChannels(channels <-chan ssh.NewChannel) { defer channel.Close() c.term = terminal.NewTerminal(channel, prompt) + c.term.AutoCompleteCallback = c.Server.AutoCompleteFunction + for req := range requests { var width, height int var ok bool diff --git a/server.go b/server.go index abdd61e..71b221a 100644 --- a/server.go +++ b/server.go @@ -273,6 +273,28 @@ func (s *Server) Start(laddr string) error { return nil } +func (s *Server) AutoCompleteFunction(line string, pos int, key rune) (newLine string, newPos int, ok bool) { + if key == 9 { + shortLine := strings.Split(line[:pos], " ") + partialNick := shortLine[len(shortLine)-1] + + nicks := s.List(&partialNick) + if len(nicks) > 0 { + nick := nicks[len(nicks)-1] + posPartialNick := pos - len(partialNick) + + newLine = strings.Replace(line[posPartialNick:], + partialNick, nick, 1) + newLine = line[:posPartialNick] + newLine + newPos = pos + (len(nick) - len(partialNick)) + ok = true + } + } else { + ok = false + } + return +} + func (s *Server) Stop() { for _, client := range s.clients { client.Conn.Close()