Skip to content

Commit

Permalink
feat: add separate terminal prompt for multiline input
Browse files Browse the repository at this point in the history
  • Loading branch information
reugn committed Dec 6, 2024
1 parent e98b3a4 commit 6a476a1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
1 change: 1 addition & 0 deletions internal/handler/input_mode_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func (h *InputModeCommand) Handle(_ string) (Response, bool) {
}

h.terminal.Config.Multiline = multiline
h.terminal.SetUserPrompt()
if h.terminal.Config.Multiline {
// disable history for multi-line messages since it is
// unusable for future requests
Expand Down
13 changes: 11 additions & 2 deletions internal/terminal/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (io *IO) readLine() string {
}

func (io *IO) readMultiLine() string {
defer io.Reader.SetPrompt(io.Prompt.User)
defer io.SetUserPrompt()
var builder strings.Builder
for {
input, err := io.Reader.Readline()
Expand All @@ -92,7 +92,7 @@ func (io *IO) readMultiLine() string {
}

if builder.Len() == 0 {
io.Reader.SetPrompt(io.Prompt.UserNext)
io.Reader.SetPrompt(io.Prompt.UserMultilineNext)
}

builder.WriteString(input)
Expand All @@ -112,3 +112,12 @@ func (io *IO) handleReadError(err error, inputLen int) string {
}
return ""
}

// SetUserPrompt sets the terminal prompt according to the current input mode.
func (io *IO) SetUserPrompt() {
if io.Config.Multiline {
io.Reader.SetPrompt(io.Prompt.UserMultiline)
} else {
io.Reader.SetPrompt(io.Prompt.User)
}
}
26 changes: 14 additions & 12 deletions internal/terminal/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ const (
)

type Prompt struct {
User string
UserNext string
Gemini string
Cli string
User string
UserMultiline string
UserMultilineNext string
Gemini string
Cli string
}

type promptColor struct {
Expand Down Expand Up @@ -45,16 +46,17 @@ func NewPrompt(currentUser string) *Prompt {
maxLength := maxLength(currentUser, geminiUser, cliUser)
pc := newPromptColor()
return &Prompt{
User: pc.user(buildPrompt(currentUser, maxLength)),
UserNext: pc.user(buildPrompt(strings.Repeat(" ", len(currentUser)), maxLength)),
Gemini: pc.gemini(buildPrompt(geminiUser, maxLength)),
Cli: pc.cli(buildPrompt(cliUser, maxLength)),
User: pc.user(buildPrompt(currentUser, '>', maxLength)),
UserMultiline: pc.user(buildPrompt(currentUser, '#', maxLength)),
UserMultilineNext: pc.user(buildPrompt(strings.Repeat(" ", len(currentUser)), '>', maxLength)),
Gemini: pc.gemini(buildPrompt(geminiUser, '>', maxLength)),
Cli: pc.cli(buildPrompt(cliUser, '>', maxLength)),
}
}

func maxLength(str ...string) int {
func maxLength(strings ...string) int {
var maxLength int
for _, s := range str {
for _, s := range strings {
length := len(s)
if maxLength < length {
maxLength = length
Expand All @@ -63,6 +65,6 @@ func maxLength(str ...string) int {
return maxLength
}

func buildPrompt(user string, length int) string {
return fmt.Sprintf("%s>%s", user, strings.Repeat(" ", length-len(user)+1))
func buildPrompt(user string, p byte, length int) string {
return fmt.Sprintf("%s%c%s", user, p, strings.Repeat(" ", length-len(user)+1))
}

0 comments on commit 6a476a1

Please sign in to comment.