Skip to content

Commit 58e2f5d

Browse files
committed
fix: attempts to fix ANSI sequences leaking because of termenv's background color check
1 parent 65f8ccf commit 58e2f5d

File tree

4 files changed

+29
-28
lines changed

4 files changed

+29
-28
lines changed

cmd/run/main.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import (
1212
"syscall"
1313
"time"
1414

15+
"github.com/muesli/termenv"
1516
"github.com/nxtcoder17/fastlog"
17+
term "golang.org/x/term"
1618
"github.com/nxtcoder17/go.errors"
1719
"github.com/nxtcoder17/runfile/pkg/runfile"
1820
"github.com/urfave/cli/v3"
@@ -37,6 +39,20 @@ func main() {
3739
Version = fmt.Sprintf("nightly | %s", time.Now().Format(time.RFC3339))
3840
}
3941

42+
// Detect terminal theme early, before any subprocesses run.
43+
// This prevents ANSI response sequences from leaking onto stdin.
44+
// Uses stderr for probing since stdout may be piped/redirected.
45+
// When no TTY is available, RUNFILE_THEME stays unset and highlighting is skipped.
46+
if os.Getenv("RUNFILE_THEME") == "" {
47+
if term.IsTerminal(int(os.Stderr.Fd())) {
48+
theme := "dark"
49+
if !termenv.NewOutput(os.Stderr).HasDarkBackground() {
50+
theme = "light"
51+
}
52+
os.Setenv("RUNFILE_THEME", theme)
53+
}
54+
}
55+
4056
cmd := cli.Command{
4157
Name: "run",
4258
Version: Version,

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,21 @@ toolchain go1.24.2
77
require (
88
github.com/alecthomas/chroma/v2 v2.15.0
99
github.com/charmbracelet/lipgloss v1.0.0
10+
github.com/creack/pty v1.1.24
1011
github.com/joho/godotenv v1.5.1
1112
github.com/muesli/termenv v0.15.2
1213
github.com/nxtcoder17/fastlog v0.0.0-20251112144402-5324a708e570
1314
github.com/nxtcoder17/fwatcher v1.2.2-0.20250804201159-543ad31be162
1415
github.com/nxtcoder17/go.errors v0.0.0-20251116060059-d31bd582d4c8
1516
github.com/urfave/cli/v3 v3.0.0-beta1
17+
golang.org/x/sync v0.19.0
1618
golang.org/x/term v0.39.0
1719
gopkg.in/yaml.v3 v3.0.1
1820
)
1921

2022
require (
2123
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
2224
github.com/charmbracelet/x/ansi v0.4.2 // indirect
23-
github.com/creack/pty v1.1.24 // indirect
2425
github.com/dlclark/regexp2 v1.11.4 // indirect
2526
github.com/fsnotify/fsnotify v1.8.0 // indirect
2627
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
@@ -33,7 +34,6 @@ require (
3334
github.com/samber/lo v1.47.0 // indirect
3435
github.com/samber/slog-common v0.18.1 // indirect
3536
github.com/samber/slog-zerolog/v2 v2.7.3 // indirect
36-
golang.org/x/sync v0.19.0 // indirect
3737
golang.org/x/sys v0.40.0 // indirect
3838
golang.org/x/text v0.16.0 // indirect
3939
)

go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,8 @@ golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
6868
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
6969
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
7070
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
71-
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
72-
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
7371
golang.org/x/sys v0.40.0 h1:DBZZqJ2Rkml6QMQsZywtnjnnGvHza6BTfYFWY9kjEWQ=
7472
golang.org/x/sys v0.40.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
75-
golang.org/x/term v0.32.0 h1:DR4lr0TjUs3epypdhTOkMmuF5CDFJ/8pOnbzMZPQ7bg=
76-
golang.org/x/term v0.32.0/go.mod h1:uZG1FhGx848Sqfsq4/DlJr3xGGsYMu/L5GW4abiaEPQ=
7773
golang.org/x/term v0.39.0 h1:RclSuaJf32jOqZz74CkPA9qFuVTX7vhLlpfj/IGWlqY=
7874
golang.org/x/term v0.39.0/go.mod h1:yxzUCTP/U+FzoxfdKmLaA0RV1WgE0VY7hXBwKtY/4ww=
7975
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=

pkg/runfile/resolver/task.go

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414

1515
"github.com/alecthomas/chroma/v2/quick"
1616
"github.com/charmbracelet/lipgloss"
17-
"github.com/muesli/termenv"
1817
"github.com/nxtcoder17/fwatcher/pkg/watcher"
1918
"github.com/nxtcoder17/go.errors"
2019
"github.com/nxtcoder17/runfile/pkg/executor"
@@ -272,21 +271,10 @@ func CreateCommand(ctx context.Context, args CmdArgs) *exec.Cmd {
272271
return c
273272
}
274273

275-
var (
276-
darkThemeOnce sync.Once
277-
darkThemeResult bool
278-
)
279-
280-
func isDarkTheme() bool {
281-
darkThemeOnce.Do(func() {
282-
darkThemeResult = termenv.NewOutput(os.Stdout).HasDarkBackground()
283-
})
284-
return darkThemeResult
285-
}
286-
287274
func printCommand(w *writer.LogWriter, prefix, lang, cmd string) {
288275
borderColor := "#4388cc"
289-
if !isDarkTheme() {
276+
switch os.Getenv("RUNFILE_THEME") {
277+
case "light":
290278
borderColor = "#3d5485"
291279
}
292280

@@ -312,19 +300,20 @@ func printCommand(w *writer.LogWriter, prefix, lang, cmd string) {
312300
}
313301

314302
hlCode := new(bytes.Buffer)
315-
// choose colorschemes from `https://swapoff.org/chroma/playground/`
316-
colorscheme := "catppuccin-macchiato"
317-
if !isDarkTheme() {
318-
colorscheme = "xcode"
303+
cmdStr := strings.TrimSpace(cmd)
304+
305+
switch os.Getenv("RUNFILE_THEME") {
306+
case "dark":
307+
quick.Highlight(hlCode, cmdStr, lang, "terminal16m", "catppuccin-macchiato")
308+
case "light":
309+
quick.Highlight(hlCode, cmdStr, lang, "terminal16m", "xcode")
310+
default:
311+
hlCode.WriteString(cmdStr)
319312
}
320313

321314
// INFO: 2 for spaces around prefix
322315
longestLen := longestLineLen(cmd) + len(prefix) + 2
323316

324-
cmdStr := strings.TrimSpace(cmd)
325-
326-
quick.Highlight(hlCode, cmdStr, lang, "terminal16m", colorscheme)
327-
328317
if width > 0 && longestLen >= width-2 {
329318
s = s.Width(width - 2)
330319
}

0 commit comments

Comments
 (0)