Skip to content

Commit 39e40c1

Browse files
authored
Merge pull request #41 from dispatchrun/color-fix
Color fixes
2 parents 12c3eeb + aae8a4b commit 39e40c1

File tree

5 files changed

+25
-37
lines changed

5 files changed

+25
-37
lines changed

cli/color.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ package cli
33
import "github.com/charmbracelet/lipgloss"
44

55
var (
6-
grayColor = lipgloss.Color("#7D7D7D")
7-
whiteColor = lipgloss.Color("#FFFFFF")
8-
redColor = lipgloss.Color("#FF0000")
9-
greenColor = lipgloss.Color("#00FF00")
10-
yellowColor = lipgloss.Color("#FFAA00")
6+
defaultColor = lipgloss.NoColor{}
7+
8+
// See https://www.hackitu.de/termcolor256/
9+
grayColor = lipgloss.ANSIColor(102)
10+
redColor = lipgloss.ANSIColor(124)
11+
greenColor = lipgloss.ANSIColor(34)
12+
yellowColor = lipgloss.ANSIColor(142)
13+
magentaColor = lipgloss.ANSIColor(127)
1114
)

cli/config.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ var (
2323
DispatchConsoleUrl string
2424

2525
DispatchConfigPath string
26-
27-
Color bool
2826
)
2927

3028
func init() {
@@ -53,18 +51,6 @@ func init() {
5351
}
5452
DispatchConfigPath = filepath.Join(os.ExpandEnv(configHome), "dispatch/config.toml")
5553
}
56-
57-
// Enable color when connected to a terminal, unless the NO_COLOR
58-
// environment variable is set (to any value). If stdout or stderr are
59-
// redirected, colors are disabled. If the FORCE_COLOR environment
60-
// variable is set (to any value), color is unconditionally enabled.
61-
Color = isTerminal(os.Stdout) && isTerminal(os.Stderr)
62-
if os.Getenv("NO_COLOR") != "" {
63-
Color = false
64-
}
65-
if os.Getenv("FORCE_COLOR") != "" {
66-
Color = true
67-
}
6854
}
6955

7056
func isTerminal(f *os.File) bool {

cli/log.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ var (
1515
logTimeStyle = lipgloss.NewStyle().Foreground(grayColor)
1616
logAttrStyle = lipgloss.NewStyle().Foreground(grayColor)
1717

18-
logDebugStyle = lipgloss.NewStyle().Foreground(whiteColor)
19-
logInfoStyle = lipgloss.NewStyle().Foreground(whiteColor)
18+
logDebugStyle = lipgloss.NewStyle().Foreground(defaultColor)
19+
logInfoStyle = lipgloss.NewStyle().Foreground(defaultColor)
2020
logWarnStyle = lipgloss.NewStyle().Foreground(yellowColor)
2121
logErrorStyle = lipgloss.NewStyle().Foreground(redColor)
2222
)

cli/run.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
sdkv1 "buf.build/gen/go/stealthrocket/dispatch-proto/protocolbuffers/go/dispatch/sdk/v1"
2626
tea "github.com/charmbracelet/bubbletea"
27+
"github.com/charmbracelet/lipgloss"
2728
"github.com/google/uuid"
2829
"github.com/spf13/cobra"
2930
"google.golang.org/protobuf/proto"
@@ -47,6 +48,12 @@ var httpClient = &http.Client{
4748
Timeout: pollTimeout,
4849
}
4950

51+
var (
52+
dispatchLogPrefixStyle = lipgloss.NewStyle().Foreground(greenColor)
53+
appLogPrefixStyle = lipgloss.NewStyle().Foreground(magentaColor)
54+
logPrefixSeparatorStyle = lipgloss.NewStyle().Foreground(grayColor)
55+
)
56+
5057
func runCommand() *cobra.Command {
5158
cmd := &cobra.Command{
5259
Use: "run",
@@ -103,14 +110,10 @@ previous run.`, defaultEndpoint),
103110
if Verbose {
104111
level = slog.LevelDebug
105112
}
106-
dispatchPrefix := []byte(pad("dispatch", prefixWidth) + " | ")
107-
if Color {
108-
dispatchPrefix = []byte("\033[32m" + pad("dispatch", prefixWidth) + " \033[90m|\033[0m ")
109-
}
110113
slog.SetDefault(slog.New(&slogHandler{
111114
stream: &prefixLogWriter{
112115
stream: logWriter,
113-
prefix: dispatchPrefix,
116+
prefix: []byte(dispatchLogPrefixStyle.Render(pad("dispatch", prefixWidth)) + logPrefixSeparatorStyle.Render(" | ")),
114117
},
115118
level: level,
116119
}))
@@ -275,13 +278,9 @@ Run 'dispatch help run' to learn about Dispatch sessions.`, BridgeSession)
275278
}
276279

277280
// Add a prefix to the local application's logs.
278-
appPrefix := []byte(pad(arg0, prefixWidth) + " | ")
279-
appSuffix := []byte("\n")
280-
if Color {
281-
appPrefix = []byte("\033[35m" + pad(arg0, prefixWidth) + " \033[90m|\033[0m ")
282-
}
283-
go printPrefixedLines(logWriter, stdout, appPrefix, appSuffix)
284-
go printPrefixedLines(logWriter, stderr, appPrefix, appSuffix)
281+
appLogPrefix := []byte(appLogPrefixStyle.Render(pad(arg0, prefixWidth)) + logPrefixSeparatorStyle.Render(" | "))
282+
go printPrefixedLines(logWriter, stdout, appLogPrefix)
283+
go printPrefixedLines(logWriter, stderr, appLogPrefix)
285284

286285
err = cmd.Wait()
287286

@@ -591,15 +590,15 @@ func withoutEnv(env []string, prefixes ...string) []string {
591590
})
592591
}
593592

594-
func printPrefixedLines(w io.Writer, r io.Reader, prefix, suffix []byte) {
593+
func printPrefixedLines(w io.Writer, r io.Reader, prefix []byte) {
595594
scanner := bufio.NewScanner(r)
596595
buffer := bytes.NewBuffer(nil)
597596
buffer.Write(prefix)
598597

599598
for scanner.Scan() {
600599
buffer.Truncate(len(prefix))
601600
buffer.Write(scanner.Bytes())
602-
buffer.Write(suffix)
601+
buffer.WriteByte('\n')
603602
_, _ = w.Write(buffer.Bytes())
604603
}
605604
}

cli/tui.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ var (
2929
viewportStyle = lipgloss.NewStyle().Margin(1, 2)
3030

3131
// Styles for the dispatch_ ASCII logo.
32-
logoStyle = lipgloss.NewStyle().Foreground(whiteColor)
32+
logoStyle = lipgloss.NewStyle().Foreground(defaultColor)
3333
logoUnderscoreStyle = lipgloss.NewStyle().Foreground(greenColor)
3434

3535
// Style for the table of function calls.
36-
tableHeaderStyle = lipgloss.NewStyle().Foreground(whiteColor).Bold(true)
36+
tableHeaderStyle = lipgloss.NewStyle().Foreground(defaultColor).Bold(true)
3737

3838
// Styles for function names and statuses in the table.
3939
pendingStyle = lipgloss.NewStyle().Foreground(grayColor)

0 commit comments

Comments
 (0)