1717package cli
1818
1919import (
20- "bufio "
20+ "errors "
2121 "fmt"
2222 "github.com/james4k/rcon"
23+ "github.com/peterh/liner"
2324 "io"
2425 "log"
2526 "os"
@@ -55,17 +56,35 @@ var colors = map[string]string{
5556 "r" : Reset , // reset
5657}
5758
58- func Start (hostPort string , password string , in io. Reader , out io.Writer ) {
59+ func Start (hostPort string , password string , out io.Writer ) {
5960 remoteConsole , err := rcon .Dial (hostPort , password )
6061 if err != nil {
6162 log .Fatal ("Failed to connect to RCON server" , err )
6263 }
6364 defer remoteConsole .Close ()
6465
65- scanner := bufio .NewScanner (in )
66- _ , _ = out .Write ([]byte ("> " ))
67- for scanner .Scan () {
68- cmd := scanner .Text ()
66+ lineEditor := liner .NewLiner ()
67+ defer lineEditor .Close ()
68+
69+ for {
70+ cmd , err := lineEditor .Prompt ("> " )
71+
72+ if err != nil {
73+ if errors .Is (err , liner .ErrPromptAborted ) {
74+ return
75+ }
76+
77+ if errors .Is (err , io .EOF ) {
78+ return
79+ }
80+
81+ _ , _ = fmt .Fprintln (os .Stderr , "Error reading input:" , err )
82+ }
83+
84+ if cmd == "exit" {
85+ return
86+ }
87+
6988 reqId , err := remoteConsole .Write (cmd )
7089 if err != nil {
7190 _ , _ = fmt .Fprintln (os .Stderr , "Failed to send command:" , err .Error ())
@@ -87,11 +106,8 @@ func Start(hostPort string, password string, in io.Reader, out io.Writer) {
87106
88107 resp = colorize (resp )
89108 _ , _ = fmt .Fprintln (out , resp )
90- _ , _ = out .Write ([]byte ("> " ))
91- }
92109
93- if err := scanner .Err (); err != nil {
94- _ , _ = fmt .Fprintln (os .Stderr , "reading standard input:" , err )
110+ lineEditor .AppendHistory (cmd )
95111 }
96112}
97113
0 commit comments