11package runner
22
33import (
4+ "bytes"
45 "context"
56 "fmt"
67 "io"
@@ -10,8 +11,8 @@ import (
1011 "strings"
1112 "sync"
1213
13- // "github.com/alecthomas/chroma/v2/quick"
14- // "github.com/charmbracelet/lipgloss"
14+ "github.com/alecthomas/chroma/v2/quick"
15+ "github.com/charmbracelet/lipgloss"
1516 "github.com/muesli/termenv"
1617 "github.com/nxtcoder17/fwatcher/pkg/executor"
1718 "github.com/nxtcoder17/fwatcher/pkg/watcher"
@@ -33,8 +34,8 @@ func isDarkTheme() bool {
3334 return termenv .NewOutput (os .Stdout ).HasDarkBackground ()
3435}
3536
36- func padString (v string , padWith string ) string {
37- sp := strings .Split (v , "\n " )
37+ func padString (str string , padWith string ) string {
38+ sp := strings .Split (str , "\n " )
3839 for i := range sp {
3940 if i == 0 {
4041 sp [i ] = fmt .Sprintf ("%s | %s" , padWith , sp [i ])
@@ -58,29 +59,29 @@ func hasANSISupport() bool {
5859 return strings .Contains (term , "xterm" ) || strings .Contains (term , "screen" ) || strings .Contains (term , "vt100" )
5960}
6061
61- // func printCommand(writer io.Writer, prefix, lang, cmd string) {
62- // if isTTY() {
63- // borderColor := "#4388cc"
64- // if !isDarkTheme() {
65- // borderColor = "#3d5485"
66- // }
67- // s := lipgloss.NewStyle().BorderForeground(lipgloss.Color(borderColor)).PaddingLeft(1).PaddingRight(1).Border(lipgloss.RoundedBorder(), true, true, true, true)
68- //
69- // hlCode := new(bytes.Buffer)
70- // // choose colorschemes from `https://swapoff.org/chroma/playground/`
71- // colorscheme := "catppuccin-macchiato"
72- // if !isDarkTheme() {
73- // colorscheme = "monokailight"
74- // }
75- // _ = colorscheme
76- //
77- // cmdStr := strings.TrimSpace(cmd)
78- //
79- // quick.Highlight(hlCode, cmdStr, lang, "terminal16m", colorscheme)
80- //
81- // fmt.Fprintf(writer, "\r%s%s\n", s.Render(padString(hlCode.String(), prefix)), s.UnsetBorderStyle())
82- // }
83- // }
62+ func printCommand (writer io.Writer , prefix , lang , cmd string ) {
63+ if isTTY () {
64+ borderColor := "#4388cc"
65+ if ! isDarkTheme () {
66+ borderColor = "#3d5485"
67+ }
68+ s := lipgloss .NewStyle ().BorderForeground (lipgloss .Color (borderColor )).PaddingLeft (1 ).PaddingRight (1 ).Border (lipgloss .RoundedBorder (), true , true , true , true )
69+
70+ hlCode := new (bytes.Buffer )
71+ // choose colorschemes from `https://swapoff.org/chroma/playground/`
72+ colorscheme := "catppuccin-macchiato"
73+ if ! isDarkTheme () {
74+ colorscheme = "monokailight"
75+ }
76+ _ = colorscheme
77+
78+ cmdStr := strings .TrimSpace (cmd )
79+
80+ quick .Highlight (hlCode , cmdStr , lang , "terminal16m" , colorscheme )
81+
82+ fmt .Fprintf (writer , "\r %s%s\n " , s .Render (padString (hlCode .String (), prefix )), s .UnsetBorderStyle ())
83+ }
84+ }
8485
8586type CreateCommandGroupArgs struct {
8687 Runfile * types.ParsedRunfile
@@ -90,10 +91,12 @@ type CreateCommandGroupArgs struct {
9091
9192 Stdout * LogWriter
9293 Stderr * LogWriter
94+
95+ EnvOverrides map [string ]string
9396}
9497
9598func createCommandGroups (ctx types.Context , args CreateCommandGroupArgs ) ([]executor.CommandGroup , error ) {
96- var cmds []executor.CommandGroup
99+ var groups []executor.CommandGroup
97100
98101 for _ , cmd := range args .Task .Commands {
99102 switch {
@@ -110,11 +113,12 @@ func createCommandGroups(ctx types.Context, args CreateCommandGroupArgs) ([]exec
110113 }
111114
112115 rtCommands , err := createCommandGroups (ctx , CreateCommandGroupArgs {
113- Runfile : args .Runfile ,
114- Task : rtp ,
115- Trail : append (append ([]string {}, args .Trail ... ), rtp .Name ),
116- Stdout : args .Stdout ,
117- Stderr : args .Stderr ,
116+ Runfile : args .Runfile ,
117+ Task : rtp ,
118+ Trail : append (append ([]string {}, args .Trail ... ), rtp .Name ),
119+ Stdout : args .Stdout ,
120+ Stderr : args .Stderr ,
121+ EnvOverrides : cmd .Env ,
118122 })
119123 if err != nil {
120124 return nil , errors .WithErr (err ).KV ("env-vars" , args .Runfile .Env )
@@ -123,52 +127,58 @@ func createCommandGroups(ctx types.Context, args CreateCommandGroupArgs) ([]exec
123127 cg := executor.CommandGroup {
124128 Groups : rtCommands ,
125129 Parallel : rtp .Parallel ,
130+ PreExecCommand : func (c * exec.Cmd ) {
131+ str := c .String ()
132+ sp := strings .SplitN (str , " " , 3 )
133+ args .Stderr .WithDimmedPrefix (* cmd .Run ).Write ([]byte (sp [2 ]))
134+ },
126135 }
127136
128- cmds = append (cmds , cg )
137+ groups = append (groups , cg )
129138 }
130139
131140 case cmd .Command != nil :
132141 {
133142 cg := executor.CommandGroup {Parallel : args .Task .Parallel }
134143
135- cg .Commands = append (cg .Commands , func (c context.Context ) * exec.Cmd {
136- commandsList := make ([]string , 0 , len (args .Task .Commands ))
137- for _ , c := range args .Task .Commands {
138- if c .Command != nil {
139- commandsList = append (commandsList , * c .Command )
140- }
141- }
142-
143- return CreateCommand (ctx , CmdArgs {
144- Shell : args .Task .Shell ,
145- Env : fn .ToEnviron (args .Task .Env ),
146- Cmd : * cmd .Command ,
147- WorkingDir : args .Task .WorkingDir ,
148- interactive : args .Task .Interactive ,
149- Stdout : func () io.Writer {
150- if args .Task .Interactive {
151- return os .Stdout
152- }
153- return args .Stdout .WithPrefix (args .Task .Name )
154- }(),
155- Stderr : func () io.Writer {
156- if args .Task .Interactive {
157- return os .Stderr
158- }
159- return args .Stderr .WithPrefix (args .Task .Name )
160- }(),
144+ cg .PreExecCommand = func (cmd * exec.Cmd ) {
145+ str := strings .TrimSpace (cmd .String ())
146+ sp := strings .SplitN (str , " " , len (args .Task .Shell )+ 1 )
147+ printCommand (args .Stderr , args .Task .Name , "bash" , sp [2 ])
148+ }
149+
150+ cg .Commands = append (
151+ cg .Commands ,
152+ func (c context.Context ) * exec.Cmd {
153+ return CreateCommand (ctx , CmdArgs {
154+ Shell : args .Task .Shell ,
155+ Env : fn .ToEnviron (fn .MapMerge (args .Task .Env , args .EnvOverrides )),
156+ Cmd : * cmd .Command ,
157+ WorkingDir : args .Task .WorkingDir ,
158+ interactive : args .Task .Interactive ,
159+ Stdout : func () io.Writer {
160+ if args .Task .Interactive {
161+ return os .Stdout
162+ }
163+ return args .Stdout .WithPrefix (args .Task .Name )
164+ }(),
165+ Stderr : func () io.Writer {
166+ if args .Task .Interactive {
167+ return os .Stderr
168+ }
169+ return args .Stderr .WithPrefix (args .Task .Name )
170+ }(),
171+ })
161172 })
162- })
163173
164174 ctx .Debug ("HERE" , "cmd" , * cmd .Command , "parallel" , args .Task .Parallel )
165175
166- cmds = append (cmds , cg )
176+ groups = append (groups , cg )
167177 }
168178 }
169179 }
170180
171- return cmds , nil
181+ return groups , nil
172182}
173183
174184func runTask (ctx types.Context , prf * types.ParsedRunfile , args runTaskArgs ) error {
0 commit comments