From 5763d9754793f15586ec4b14cf01de1f71c5c9ac Mon Sep 17 00:00:00 2001 From: reugn Date: Sun, 18 Feb 2024 17:41:54 +0200 Subject: [PATCH] feat: utilize readline library for input reading --- cli/chat.go | 22 +++++++++++++--------- cmd/gemini/main.go | 5 ++++- go.mod | 1 + go.sum | 7 +++++++ 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/cli/chat.go b/cli/chat.go index 11816ae..15c49aa 100644 --- a/cli/chat.go +++ b/cli/chat.go @@ -1,11 +1,10 @@ package cli import ( - "bufio" "fmt" - "os" "strings" + "github.com/chzyer/readline" "github.com/reugn/gemini-cli/gemini" ) @@ -19,24 +18,29 @@ type ChatOpts struct { type Chat struct { model *gemini.ChatSession prompt *prompt - reader *bufio.Reader + reader *readline.Instance opts *ChatOpts } // NewChat returns a new Chat. -func NewChat(user string, model *gemini.ChatSession, opts *ChatOpts) *Chat { +func NewChat(user string, model *gemini.ChatSession, opts *ChatOpts) (*Chat, error) { + reader, err := readline.NewEx(&readline.Config{}) + if err != nil { + return nil, err + } + prompt := newPrompt(user) + reader.SetPrompt(prompt.user) return &Chat{ model: model, - prompt: newPrompt(user), - reader: bufio.NewReader(os.Stdin), + prompt: prompt, + reader: reader, opts: opts, - } + }, nil } // StartChat starts the chat loop. func (c *Chat) StartChat() { for { - fmt.Print(c.prompt.user) message, ok := c.readLine() if !ok { continue @@ -49,7 +53,7 @@ func (c *Chat) StartChat() { } func (c *Chat) readLine() (string, bool) { - input, err := c.reader.ReadString('\n') + input, err := c.reader.Readline() if err != nil { fmt.Printf("%s%s\n", c.prompt.cli, err) return "", false diff --git a/cmd/gemini/main.go b/cmd/gemini/main.go index 4baed04..86057da 100644 --- a/cmd/gemini/main.go +++ b/cmd/gemini/main.go @@ -34,7 +34,10 @@ func run() int { if err != nil { return err } - chat := cli.NewChat(getCurrentUser(), chatSession, &opts) + chat, err := cli.NewChat(getCurrentUser(), chatSession, &opts) + if err != nil { + return err + } chat.StartChat() chatSession.Close() diff --git a/go.mod b/go.mod index a283905..89524d2 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.21 require ( github.com/charmbracelet/glamour v0.6.0 + github.com/chzyer/readline v1.5.1 github.com/google/generative-ai-go v0.7.0 github.com/spf13/cobra v1.8.0 google.golang.org/api v0.149.0 diff --git a/go.sum b/go.sum index 4a6e51f..bc79a67 100644 --- a/go.sum +++ b/go.sum @@ -17,6 +17,12 @@ github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd3 github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/charmbracelet/glamour v0.6.0 h1:wi8fse3Y7nfcabbbDuwolqTqMQPMnVPeZhDM273bISc= github.com/charmbracelet/glamour v0.6.0/go.mod h1:taqWV4swIMMbWALc0m7AfE9JkPSU8om2538k9ITBxOc= +github.com/chzyer/logex v1.2.1 h1:XHDu3E6q+gdHgsdTPH6ImJMIp436vR6MPtH8gP05QzM= +github.com/chzyer/logex v1.2.1/go.mod h1:JLbx6lG2kDbNRFnfkgvh4eRJRPX1QCoOIWomwysCBrQ= +github.com/chzyer/readline v1.5.1 h1:upd/6fQk4src78LMRzh5vItIt361/o4uq553V8B5sGI= +github.com/chzyer/readline v1.5.1/go.mod h1:Eh+b79XXUwfKfcPLepksvw2tcLE/Ct21YObkaSkeBlk= +github.com/chzyer/test v1.0.0 h1:p3BQDXSxOhOG0P9z6/hGnII4LGiEPOYBhs8asl/fC04= +github.com/chzyer/test v1.0.0/go.mod h1:2JlltgoNkt4TW/z9V/IzDdFaMTM2JPIi26O1pF38GC8= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= @@ -142,6 +148,7 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=