From a80afe3c24dfc8ec0b1f2b85208412743a96e19a Mon Sep 17 00:00:00 2001
From: reugn
Date: Tue, 5 Mar 2024 09:47:06 +0200
Subject: [PATCH] feat: synchronize prompt colors with terminal background
---
cli/color/color.go | 24 ++++++++++++------------
cli/prompt.go | 31 +++++++++++++++++++++++++++----
go.mod | 2 +-
3 files changed, 40 insertions(+), 17 deletions(-)
diff --git a/cli/color/color.go b/cli/color/color.go
index 1702e0a..9f324fb 100644
--- a/cli/color/color.go
+++ b/cli/color/color.go
@@ -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.
@@ -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.
diff --git a/cli/prompt.go b/cli/prompt.go
index 4cbfd13..6b3cb90 100644
--- a/cli/prompt.go
+++ b/cli/prompt.go
@@ -4,6 +4,7 @@ import (
"fmt"
"strings"
+ "github.com/muesli/termenv"
"github.com/reugn/gemini-cli/cli/color"
)
@@ -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)),
}
}
diff --git a/go.mod b/go.mod
index 89524d2..a0804df 100644
--- a/go.mod
+++ b/go.mod
@@ -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
)
@@ -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