Skip to content

Commit

Permalink
feat: command syntax highlighting based on terminal theme
Browse files Browse the repository at this point in the history
  • Loading branch information
nxtcoder17 committed Dec 9, 2024
1 parent 0b68343 commit 917c460
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions pkg/runfile/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/alecthomas/chroma/v2/quick"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/termenv"
fn "github.com/nxtcoder17/runfile/pkg/functions"
"golang.org/x/sync/errgroup"
)
Expand Down Expand Up @@ -119,6 +120,10 @@ func processOutputLineByLine(writer io.Writer, reader io.Reader, prefix *string)
}
}

func isDarkTheme() bool {
return termenv.NewOutput(os.Stdout).HasDarkBackground()
}

func runTask(ctx Context, rf *Runfile, args runTaskArgs) *Error {
runfilePath := fn.Must(filepath.Rel(rf.attrs.RootRunfilePath, rf.attrs.RunfilePath))

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

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

Expand Down Expand Up @@ -216,13 +220,20 @@ func runTask(ctx Context, rf *Runfile, args runTaskArgs) *Error {
// // }
// // processOutputLineByLine(os.Stderr, stderrR, &logPrefix)
// }()
//
// }

s := lipgloss.NewStyle().BorderForeground(lipgloss.Color("#4388cc")).PaddingLeft(1).PaddingRight(1).Border(lipgloss.RoundedBorder(), true, true, true, true)
borderColor := "#4388cc"
if !isDarkTheme() {
borderColor = "#3d5485"
}
s := lipgloss.NewStyle().BorderForeground(lipgloss.Color(borderColor)).PaddingLeft(1).PaddingRight(1).Border(lipgloss.RoundedBorder(), true, true, true, true)

hlCode := new(bytes.Buffer)
quick.Highlight(hlCode, strings.TrimSpace(command.Command), "bash", "terminal16m", "onedark")
colorscheme := "onedark"
if !isDarkTheme() {
colorscheme = "monokailight"
}
quick.Highlight(hlCode, strings.TrimSpace(command.Command), "bash", "terminal16m", colorscheme)

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

Expand Down

0 comments on commit 917c460

Please sign in to comment.