Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add separate terminal prompt for multiline input #32

Merged
merged 1 commit into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
}
Loading