forked from mirror/readline
support dial to remote readline
This commit is contained in:
parent
cbab1f1566
commit
8853e17195
|
@ -0,0 +1,49 @@
|
|||
package readline
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Conn struct {
|
||||
Conn net.Conn
|
||||
Terminal *Terminal
|
||||
runChan chan error
|
||||
}
|
||||
|
||||
func NewConn(conn net.Conn, t *Terminal) (*Conn, error) {
|
||||
return &Conn{
|
||||
Conn: conn,
|
||||
Terminal: t,
|
||||
runChan: make(chan error),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *Conn) Run() error {
|
||||
c.Terminal.EnterRawMode()
|
||||
go func() {
|
||||
_, err := io.Copy(c.Conn, os.Stdin)
|
||||
c.runChan <- err
|
||||
}()
|
||||
go func() {
|
||||
_, err := io.Copy(os.Stdout, c.Conn)
|
||||
c.runChan <- err
|
||||
}()
|
||||
err := <-c.runChan
|
||||
c.Terminal.ExitRawMode()
|
||||
return err
|
||||
}
|
||||
|
||||
func Dial(network string, address string) (*Remote, error) {
|
||||
conn, err := net.Dial(network, address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var cfg Config
|
||||
t, err := NewTerminal(&cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return NewRemote(conn, t)
|
||||
}
|
Loading…
Reference in New Issue