From d517e9a53086394b5f08f74f4552cd6c2891f2b5 Mon Sep 17 00:00:00 2001 From: Akshay Shekher Date: Sat, 13 Dec 2014 15:30:39 +0530 Subject: [PATCH 1/4] Autocomplete nick support --- client.go | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/client.go b/client.go index 33bd4f7..705aae3 100644 --- a/client.go +++ b/client.go @@ -30,6 +30,8 @@ const ABOUT_TEXT string = `-> ssh-chat is made by @shazow. For more, visit shazow.net or follow at twitter.com/shazow ` +var autoCompleteFunc func(line string, pos int, key rune) (newLine string, newPos int, ok bool) = nil + type Client struct { Server *Server Conn *ssh.ServerConn @@ -45,6 +47,9 @@ type Client struct { } func NewClient(server *Server, conn *ssh.ServerConn) *Client { + if autoCompleteFunc == nil { + autoCompleteFunc = createAutoCompleteFunc(server) + } return &Client{ Server: server, Conn: conn, @@ -56,7 +61,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 +262,8 @@ func (c *Client) handleChannels(channels <-chan ssh.NewChannel) { defer channel.Close() c.term = terminal.NewTerminal(channel, prompt) + c.term.AutoCompleteCallback = autoCompleteFunc + for req := range requests { var width, height int var ok bool @@ -288,3 +295,28 @@ func (c *Client) handleChannels(channels <-chan ssh.NewChannel) { } } } + +func createAutoCompleteFunc(server *Server) func(string, int, rune) (string, int, bool) { + return func(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 := server.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 + fmt.Println(newLine) + } + } else { + ok = false + } + return + } +} From 6a53c8f7e231d2adac89c4cfe24e35ddf1760026 Mon Sep 17 00:00:00 2001 From: Akshay Shekher Date: Sat, 13 Dec 2014 16:03:06 +0530 Subject: [PATCH 2/4] Refactored the code, autocomplete function part of the Server struct(??) --- client.go | 33 +-------------------------------- server.go | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 32 deletions(-) diff --git a/client.go b/client.go index 705aae3..1de35b1 100644 --- a/client.go +++ b/client.go @@ -29,9 +29,6 @@ const ABOUT_TEXT string = `-> ssh-chat is made by @shazow. For more, visit shazow.net or follow at twitter.com/shazow ` - -var autoCompleteFunc func(line string, pos int, key rune) (newLine string, newPos int, ok bool) = nil - type Client struct { Server *Server Conn *ssh.ServerConn @@ -47,9 +44,6 @@ type Client struct { } func NewClient(server *Server, conn *ssh.ServerConn) *Client { - if autoCompleteFunc == nil { - autoCompleteFunc = createAutoCompleteFunc(server) - } return &Client{ Server: server, Conn: conn, @@ -262,7 +256,7 @@ func (c *Client) handleChannels(channels <-chan ssh.NewChannel) { defer channel.Close() c.term = terminal.NewTerminal(channel, prompt) - c.term.AutoCompleteCallback = autoCompleteFunc + c.term.AutoCompleteCallback = c.Server.AutoCompleteFunction for req := range requests { var width, height int @@ -295,28 +289,3 @@ func (c *Client) handleChannels(channels <-chan ssh.NewChannel) { } } } - -func createAutoCompleteFunc(server *Server) func(string, int, rune) (string, int, bool) { - return func(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 := server.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 - fmt.Println(newLine) - } - } else { - ok = false - } - return - } -} diff --git a/server.go b/server.go index abdd61e..e7fa4bc 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() From b346ccdeb7ae1b340dba62687e3b0c90161e7933 Mon Sep 17 00:00:00 2001 From: Akshay Shekher Date: Sat, 13 Dec 2014 16:17:03 +0530 Subject: [PATCH 3/4] Updated the readme to reflect tab completion --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 From f07f7e78ab31b60108bd416cc99ca12457f247be Mon Sep 17 00:00:00 2001 From: Akshay Shekher Date: Sat, 13 Dec 2014 16:18:55 +0530 Subject: [PATCH 4/4] formatting fixes --- server.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server.go b/server.go index e7fa4bc..71b221a 100644 --- a/server.go +++ b/server.go @@ -276,11 +276,11 @@ func (s *Server) Start(laddr string) error { 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] + partialNick := shortLine[len(shortLine)-1] nicks := s.List(&partialNick) if len(nicks) > 0 { - nick := nicks[len(nicks) - 1] + nick := nicks[len(nicks)-1] posPartialNick := pos - len(partialNick) newLine = strings.Replace(line[posPartialNick:],