prompt now shows at the right time for clients

master
Jordan Orelli 12 years ago
parent 0101163559
commit 512549a07c

@ -43,10 +43,15 @@ func die(message string) {
} }
func tcpInterpreter(conn net.Conn, out chan interface{}, errors chan error) { func tcpInterpreter(conn net.Conn, out chan interface{}, errors chan error) {
prompt := func() {
io.WriteString(conn, "> ")
}
prompt()
for { for {
select { select {
case v := <-out: case v := <-out:
fmt.Fprintln(conn, v) fmt.Fprintln(conn, v)
prompt()
case err := <-errors: case err := <-errors:
fmt.Fprintf(conn, "error: %v", err) fmt.Fprintf(conn, "error: %v", err)
} }
@ -55,20 +60,16 @@ func tcpInterpreter(conn net.Conn, out chan interface{}, errors chan error) {
func startConnection(conn net.Conn, c, d chan net.Conn) { func startConnection(conn net.Conn, c, d chan net.Conn) {
c <- conn c <- conn
defer func() { d <- conn }() defer func() {
disconnect := func() { d <- conn
fmt.Println("disconnected") fmt.Println("disconnected")
} }()
out, errors := make(chan interface{}), make(chan error) out, errors := make(chan interface{}), make(chan error)
go tcpInterpreter(conn, out, errors) go tcpInterpreter(conn, out, errors)
r := bufio.NewReader(conn) r := bufio.NewReader(conn)
for { for {
if _, err := io.WriteString(conn, "> "); err != nil {
disconnect()
return
}
line, prefix, err := r.ReadLine() line, prefix, err := r.ReadLine()
if prefix { if prefix {
fmt.Println("(prefix)") fmt.Println("(prefix)")
@ -77,11 +78,9 @@ func startConnection(conn net.Conn, c, d chan net.Conn) {
case nil: case nil:
break break
case io.EOF: case io.EOF:
disconnect()
return return
default: default:
printErrorMsg(err.Error()) printErrorMsg(err.Error())
disconnect()
return return
} }

Loading…
Cancel
Save