Skip to content

Commit 917c460

Browse files
committed
feat: command syntax highlighting based on terminal theme
1 parent 0b68343 commit 917c460

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

pkg/runfile/run.go

+15-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"github.com/alecthomas/chroma/v2/quick"
1616
"github.com/charmbracelet/lipgloss"
17+
"github.com/muesli/termenv"
1718
fn "github.com/nxtcoder17/runfile/pkg/functions"
1819
"golang.org/x/sync/errgroup"
1920
)
@@ -119,6 +120,10 @@ func processOutputLineByLine(writer io.Writer, reader io.Reader, prefix *string)
119120
}
120121
}
121122

123+
func isDarkTheme() bool {
124+
return termenv.NewOutput(os.Stdout).HasDarkBackground()
125+
}
126+
122127
func runTask(ctx Context, rf *Runfile, args runTaskArgs) *Error {
123128
runfilePath := fn.Must(filepath.Rel(rf.attrs.RootRunfilePath, rf.attrs.RunfilePath))
124129

@@ -153,7 +158,6 @@ func runTask(ctx Context, rf *Runfile, args runTaskArgs) *Error {
153158
return formatErr(err)
154159
}
155160

156-
logger.Debug("debugging env", "pt.environ", pt.Env, "overrides", args.envOverrides)
157161
for _, command := range pt.Commands {
158162
logger.Debug("running command task", "command.run", command.Run, "parent.task", args.taskName)
159163

@@ -216,13 +220,20 @@ func runTask(ctx Context, rf *Runfile, args runTaskArgs) *Error {
216220
// // }
217221
// // processOutputLineByLine(os.Stderr, stderrR, &logPrefix)
218222
// }()
219-
//
220223
// }
221224

222-
s := lipgloss.NewStyle().BorderForeground(lipgloss.Color("#4388cc")).PaddingLeft(1).PaddingRight(1).Border(lipgloss.RoundedBorder(), true, true, true, true)
225+
borderColor := "#4388cc"
226+
if !isDarkTheme() {
227+
borderColor = "#3d5485"
228+
}
229+
s := lipgloss.NewStyle().BorderForeground(lipgloss.Color(borderColor)).PaddingLeft(1).PaddingRight(1).Border(lipgloss.RoundedBorder(), true, true, true, true)
223230

224231
hlCode := new(bytes.Buffer)
225-
quick.Highlight(hlCode, strings.TrimSpace(command.Command), "bash", "terminal16m", "onedark")
232+
colorscheme := "onedark"
233+
if !isDarkTheme() {
234+
colorscheme = "monokailight"
235+
}
236+
quick.Highlight(hlCode, strings.TrimSpace(command.Command), "bash", "terminal16m", colorscheme)
226237

227238
fmt.Printf("%s\n", s.Render(hlCode.String()))
228239

0 commit comments

Comments
 (0)