Skip to content

Commit

Permalink
feat: synchronize prompt colors with terminal background (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
reugn authored Mar 5, 2024
1 parent b1fe8cf commit 3f9b170
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
24 changes: 12 additions & 12 deletions cli/color/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package color
import "fmt"

var (
reset = "\033[0m"
red = "\033[31m"
green = "\033[32m"
yellow = "\033[33m"
blue = "\033[34m"
purple = "\033[35m"
cyan = "\033[36m"
gray = "\033[37m"
white = "\033[97m"
reset = "\033[0m"
red = "\033[31m"
green = "\033[32m"
yellow = "\033[33m"
blue = "\033[34m"
magenta = "\033[35m"
cyan = "\033[36m"
gray = "\033[37m"
white = "\033[97m"
)

// Red adds red color to str in terminal.
Expand All @@ -34,9 +34,9 @@ func Blue(str string) string {
return fmt.Sprintf("%s%s%s", blue, str, reset)
}

// Purple adds purple color to str in terminal.
func Purple(str string) string {
return fmt.Sprintf("%s%s%s", purple, str, reset)
// Magenta adds magenta color to str in terminal.
func Magenta(str string) string {
return fmt.Sprintf("%s%s%s", magenta, str, reset)
}

// Cyan adds cyan color to str in terminal.
Expand Down
31 changes: 27 additions & 4 deletions cli/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"strings"

"github.com/muesli/termenv"
"github.com/reugn/gemini-cli/cli/color"
)

Expand All @@ -19,13 +20,35 @@ type prompt struct {
cli string
}

type promptColor struct {
user func(string) string
gemini func(string) string
cli func(string) string
}

func newPromptColor() *promptColor {
if termenv.HasDarkBackground() {
return &promptColor{
user: color.Cyan,
gemini: color.Green,
cli: color.Yellow,
}
}
return &promptColor{
user: color.Blue,
gemini: color.Green,
cli: color.Magenta,
}
}

func newPrompt(currentUser string) *prompt {
maxLength := maxLength(currentUser, geminiUser, cliUser)
pc := newPromptColor()
return &prompt{
user: color.Blue(buildPrompt(currentUser, maxLength)),
userNext: color.Blue(buildPrompt(strings.Repeat(" ", len(currentUser)), maxLength)),
gemini: color.Green(buildPrompt(geminiUser, maxLength)),
cli: color.Yellow(buildPrompt(cliUser, maxLength)),
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)),
}
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ 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/muesli/termenv v0.13.0
github.com/spf13/cobra v1.8.0
google.golang.org/api v0.149.0
)
Expand All @@ -31,7 +32,6 @@ require (
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/microcosm-cc/bluemonday v1.0.21 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.13.0 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
Expand Down

0 comments on commit 3f9b170

Please sign in to comment.