Merge pull request #5 from voldyman/master

Autocomplete nick support.
This commit is contained in:
Andrey Petrov 2014-12-13 10:38:50 -08:00
commit ca580264f0
3 changed files with 27 additions and 4 deletions

View File

@ -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

View File

@ -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

View File

@ -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()