From 6a1c162807fb4b747735ed3fec48f742af47c7cb Mon Sep 17 00:00:00 2001 From: Ric Szopa Date: Tue, 6 Jun 2023 17:19:28 +0200 Subject: [PATCH] exit, quit, or bye exit. Also, slightly changed the organization of the code. --- cmd/think/main.go | 5 ++++- cmd/think/repl.go | 25 +++++++++++++++++-------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/cmd/think/main.go b/cmd/think/main.go index 93d3566..9fd5fb2 100644 --- a/cmd/think/main.go +++ b/cmd/think/main.go @@ -152,7 +152,10 @@ func main() { log.Fatal(err) } - repl := NewREPL(ag, shellPath, strings.Join(flag.Args(), " "), *sendOutput) + repl, err := NewREPL(ag, shellPath, strings.Join(flag.Args(), " "), *sendOutput) + if err != nil { + log.Fatal(err) + } defer repl.Close() repl.Run() diff --git a/cmd/think/repl.go b/cmd/think/repl.go index f1ad64d..e061062 100644 --- a/cmd/think/repl.go +++ b/cmd/think/repl.go @@ -17,6 +17,8 @@ import ( "github.com/sirupsen/logrus" ) +const outputSent = 1000 + type REPL struct { readline *readline.Instance inCode bool @@ -27,7 +29,7 @@ type REPL struct { sendOutput bool } -func NewREPL(agent agent.Agent, shellPath, initialInput string, sendOutput bool) *REPL { +func NewREPL(agent agent.Agent, shellPath, initialInput string, sendOutput bool) (*REPL, error) { red := color.New(color.FgRed).SprintFunc() blue := color.New(color.FgCyan).SprintFunc() repl := &REPL{ @@ -40,7 +42,7 @@ func NewREPL(agent agent.Agent, shellPath, initialInput string, sendOutput bool) homeDir, err := os.UserHomeDir() if err != nil { - log.Fatal(err) + return nil, err } historyFile := filepath.Join(homeDir, ".think_history") @@ -62,7 +64,7 @@ func NewREPL(agent agent.Agent, shellPath, initialInput string, sendOutput bool) rl.WriteStdin([]byte(initialInput)) repl.readline = rl - return repl + return repl, nil } func (repl *REPL) Close() { @@ -121,18 +123,25 @@ func (repl *REPL) Run() { cmd.Stderr = errMulti cmd.Run() lastOut, lastErr = stdoutBuf.String(), stderrBuf.String() - // truncate lastOut and lastErr to 1000 characters - if len(lastOut) > 1000 { - lastOut = lastOut[len(lastOut)-1000:] + // truncate lastOut and lastErr to outputSent characters + if len(lastOut) > outputSent { + lastOut = lastOut[len(lastOut)-outputSent:] } - if len(lastErr) > 1000 { - lastErr = lastErr[len(lastErr)-1000:] + if len(lastErr) > outputSent { + lastErr = lastErr[len(lastErr)-outputSent:] } commandWasRun = true exitCode = cmd.ProcessState.ExitCode() repl.outOfCodeLoop() } else { + // if line is one of exit, quit, bye, or ^D, exit + if line == "exit" || line == "quit" || line == "bye" { + return + } + if line == "" { + continue + } feedback, err := repl.agent.Listen("user", struct { Message string CommandWasRun bool