Skip to content

Commit

Permalink
feat: utilize readline library for input reading
Browse files Browse the repository at this point in the history
  • Loading branch information
reugn committed Feb 18, 2024
1 parent f6de19f commit 5763d97
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
22 changes: 13 additions & 9 deletions cli/chat.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package cli

import (
"bufio"
"fmt"
"os"
"strings"

"github.com/chzyer/readline"
"github.com/reugn/gemini-cli/gemini"
)

Expand All @@ -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
Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion cmd/gemini/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down

0 comments on commit 5763d97

Please sign in to comment.