Skip to content

Commit d93accb

Browse files
authored
add --color override (always/never/auto) (#38)
- auto: current behavior (don't show color of TERM=dumb or not a tty) - always: always output colors - never: never outputs colors
1 parent c250003 commit d93accb

4 files changed

Lines changed: 28 additions & 5 deletions

File tree

config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ type Config struct {
6262
Format string
6363
FileOutput string
6464
Verbose bool
65-
NoSyntax bool // disable syntax highlighting
66-
Dedup bool // collapse byte-identical matches
65+
NoSyntax bool // disable syntax highlighting
66+
Dedup bool // collapse byte-identical matches
67+
Color string // color mode: auto, always, never
6768

6869
// MCP
6970
MCPServer bool

console.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,23 @@ func ConsoleSearch(cfg *Config) {
5858
}
5959

6060
func formatDefault(cfg *Config, results []*common.FileJob) {
61-
noColor := os.Getenv("TERM") == "dumb" ||
62-
(!isatty.IsTerminal(os.Stdout.Fd()) && !isatty.IsCygwinTerminal(os.Stdout.Fd()))
61+
var noColor bool
62+
switch cfg.Color {
63+
case "always":
64+
noColor = false
65+
case "never":
66+
noColor = true
67+
default: // "auto"
68+
noColor = os.Getenv("TERM") == "dumb" ||
69+
(!isatty.IsTerminal(os.Stdout.Fd()) && !isatty.IsCygwinTerminal(os.Stdout.Fd()))
70+
}
71+
color.NoColor = noColor
6372

6473
fmtBegin := "\033[1;31m"
6574
fmtEnd := "\033[0m"
6675
if noColor {
6776
fmtBegin = ""
6877
fmtEnd = ""
69-
color.NoColor = true
7078
}
7179

7280
documentFrequency := ranker.CalculateDocumentTermFrequency(results)

main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,12 @@ func main() {
313313
false,
314314
"disable syntax highlighting in output",
315315
)
316+
flags.StringVar(
317+
&cfg.Color,
318+
"color",
319+
"auto",
320+
"color output mode [auto, always, never]",
321+
)
316322
flags.Float64Var(
317323
&cfg.WeightCode,
318324
"weight-code",

tui.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/charmbracelet/bubbles/textinput"
1616
tea "github.com/charmbracelet/bubbletea"
1717
"github.com/charmbracelet/lipgloss"
18+
"github.com/muesli/termenv"
1819
)
1920

2021
// searchResult represents a single search result
@@ -127,6 +128,13 @@ type model struct {
127128
}
128129

129130
func initialModel(cfg *Config) model {
131+
switch cfg.Color {
132+
case "always":
133+
lipgloss.SetColorProfile(termenv.ANSI256)
134+
case "never":
135+
lipgloss.SetColorProfile(termenv.Ascii)
136+
}
137+
130138
si := textinput.New()
131139
si.Placeholder = "search query..."
132140
si.Prompt = "> "

0 commit comments

Comments
 (0)