Skip to content

Commit

Permalink
exit, quit, or bye exit.
Browse files Browse the repository at this point in the history
Also, slightly changed the organization of the code.
  • Loading branch information
Ric Szopa authored and Ric Szopa committed Jun 6, 2023
1 parent 3e21a0e commit 6a1c162
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
5 changes: 4 additions & 1 deletion cmd/think/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
25 changes: 17 additions & 8 deletions cmd/think/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"github.com/sirupsen/logrus"
)

const outputSent = 1000

type REPL struct {
readline *readline.Instance
inCode bool
Expand All @@ -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{
Expand All @@ -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")
Expand All @@ -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() {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6a1c162

Please sign in to comment.