Refactored the code, autocomplete function part of the Server struct(??)
This commit is contained in:
parent
d517e9a530
commit
6a53c8f7e2
33
client.go
33
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
|
||||
}
|
||||
}
|
||||
|
|
22
server.go
22
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()
|
||||
|
|
Loading…
Reference in New Issue