Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions cmd/run/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
"syscall"
"time"

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

// Detect terminal theme early, before any subprocesses run.
// This prevents ANSI response sequences from leaking onto stdin.
// Uses stderr for probing since stdout may be piped/redirected.
// When no TTY is available, RUNFILE_THEME stays unset and highlighting is skipped.
if os.Getenv("RUNFILE_THEME") == "" {
if term.IsTerminal(int(os.Stderr.Fd())) {
theme := "dark"
if !termenv.NewOutput(os.Stderr).HasDarkBackground() {
theme = "light"
}
os.Setenv("RUNFILE_THEME", theme)
}
}

cmd := cli.Command{
Name: "run",
Version: Version,
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@ toolchain go1.24.2
require (
github.com/alecthomas/chroma/v2 v2.15.0
github.com/charmbracelet/lipgloss v1.0.0
github.com/creack/pty v1.1.24
github.com/joho/godotenv v1.5.1
github.com/muesli/termenv v0.15.2
github.com/nxtcoder17/fastlog v0.0.0-20251112144402-5324a708e570
github.com/nxtcoder17/fwatcher v1.2.2-0.20250804201159-543ad31be162
github.com/nxtcoder17/go.errors v0.0.0-20251116060059-d31bd582d4c8
github.com/urfave/cli/v3 v3.0.0-beta1
golang.org/x/sync v0.19.0
golang.org/x/term v0.39.0
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/x/ansi v0.4.2 // indirect
github.com/creack/pty v1.1.24 // indirect
github.com/dlclark/regexp2 v1.11.4 // indirect
github.com/fsnotify/fsnotify v1.8.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
Expand All @@ -33,7 +34,6 @@ require (
github.com/samber/lo v1.47.0 // indirect
github.com/samber/slog-common v0.18.1 // indirect
github.com/samber/slog-zerolog/v2 v2.7.3 // indirect
golang.org/x/sync v0.19.0 // indirect
golang.org/x/sys v0.40.0 // indirect
golang.org/x/text v0.16.0 // indirect
)
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,8 @@ golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/sys v0.40.0 h1:DBZZqJ2Rkml6QMQsZywtnjnnGvHza6BTfYFWY9kjEWQ=
golang.org/x/sys v0.40.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
golang.org/x/term v0.32.0 h1:DR4lr0TjUs3epypdhTOkMmuF5CDFJ/8pOnbzMZPQ7bg=
golang.org/x/term v0.32.0/go.mod h1:uZG1FhGx848Sqfsq4/DlJr3xGGsYMu/L5GW4abiaEPQ=
golang.org/x/term v0.39.0 h1:RclSuaJf32jOqZz74CkPA9qFuVTX7vhLlpfj/IGWlqY=
golang.org/x/term v0.39.0/go.mod h1:yxzUCTP/U+FzoxfdKmLaA0RV1WgE0VY7hXBwKtY/4ww=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
Expand Down
33 changes: 11 additions & 22 deletions pkg/runfile/resolver/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

"github.com/alecthomas/chroma/v2/quick"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/termenv"
"github.com/nxtcoder17/fwatcher/pkg/watcher"
"github.com/nxtcoder17/go.errors"
"github.com/nxtcoder17/runfile/pkg/executor"
Expand Down Expand Up @@ -272,21 +271,10 @@ func CreateCommand(ctx context.Context, args CmdArgs) *exec.Cmd {
return c
}

var (
darkThemeOnce sync.Once
darkThemeResult bool
)

func isDarkTheme() bool {
darkThemeOnce.Do(func() {
darkThemeResult = termenv.NewOutput(os.Stdout).HasDarkBackground()
})
return darkThemeResult
}

func printCommand(w *writer.LogWriter, prefix, lang, cmd string) {
borderColor := "#4388cc"
if !isDarkTheme() {
switch os.Getenv("RUNFILE_THEME") {
case "light":
borderColor = "#3d5485"
}

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

hlCode := new(bytes.Buffer)
// choose colorschemes from `https://swapoff.org/chroma/playground/`
colorscheme := "catppuccin-macchiato"
if !isDarkTheme() {
colorscheme = "xcode"
cmdStr := strings.TrimSpace(cmd)

switch os.Getenv("RUNFILE_THEME") {
case "dark":
quick.Highlight(hlCode, cmdStr, lang, "terminal16m", "catppuccin-macchiato")
case "light":
quick.Highlight(hlCode, cmdStr, lang, "terminal16m", "xcode")
default:
hlCode.WriteString(cmdStr)
}

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

cmdStr := strings.TrimSpace(cmd)

quick.Highlight(hlCode, cmdStr, lang, "terminal16m", colorscheme)

if width > 0 && longestLen >= width-2 {
s = s.Width(width - 2)
}
Expand Down