Skip to content

Commit 51e6d2e

Browse files
committed
fix: fixes log writer handling with interactive commands
1 parent d6eb5f0 commit 51e6d2e

File tree

2 files changed

+58
-40
lines changed

2 files changed

+58
-40
lines changed

runner/run-task.go

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package runner
22

33
import (
4-
"bytes"
54
"context"
65
"fmt"
76
"io"
@@ -11,8 +10,8 @@ import (
1110
"strings"
1211
"sync"
1312

14-
"github.com/alecthomas/chroma/v2/quick"
15-
"github.com/charmbracelet/lipgloss"
13+
// "github.com/alecthomas/chroma/v2/quick"
14+
// "github.com/charmbracelet/lipgloss"
1615
"github.com/muesli/termenv"
1716
"github.com/nxtcoder17/fwatcher/pkg/executor"
1817
"github.com/nxtcoder17/fwatcher/pkg/watcher"
@@ -54,35 +53,35 @@ func isTTY() bool {
5453
return ((stdout.Mode() & os.ModeCharDevice) == os.ModeCharDevice) && ((stderr.Mode() & os.ModeCharDevice) == os.ModeCharDevice)
5554
}
5655

57-
func printCommand(writer io.Writer, prefix, lang, cmd string) {
58-
if isTTY() {
59-
borderColor := "#4388cc"
60-
if !isDarkTheme() {
61-
borderColor = "#3d5485"
62-
}
63-
s := lipgloss.NewStyle().BorderForeground(lipgloss.Color(borderColor)).PaddingLeft(1).PaddingRight(1).Border(lipgloss.RoundedBorder(), true, true, true, true)
64-
// labelStyle := lipgloss.NewStyle().Foreground(lipgloss.Color(borderColor)).Blink(true)
65-
66-
hlCode := new(bytes.Buffer)
67-
// choose colorschemes from `https://swapoff.org/chroma/playground/`
68-
colorscheme := "catppuccin-macchiato"
69-
if !isDarkTheme() {
70-
colorscheme = "monokailight"
71-
}
72-
_ = colorscheme
73-
// quick.Highlight(hlCode, strings.TrimSpace(command.Command), "bash", "terminal16m", colorscheme)
74-
75-
cmdStr := strings.TrimSpace(cmd)
76-
77-
quick.Highlight(hlCode, cmdStr, lang, "terminal16m", colorscheme)
78-
// cst := styles.Get("gruvbox")
79-
// fmt.Println("cst: ", cst.Name, styles.Fallback.Name, styles.Names())
80-
81-
// fmt.Printf("%s\n", s.Render(args.taskName+" | "+hlCode.String()))
82-
fmt.Fprintf(writer, "%s\n", s.Render(padString(hlCode.String(), prefix)))
83-
}
56+
func hasANSISupport() bool {
57+
term := os.Getenv("TERM")
58+
return strings.Contains(term, "xterm") || strings.Contains(term, "screen") || strings.Contains(term, "vt100")
8459
}
8560

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+
// }
84+
8685
type CreateCommandGroupArgs struct {
8786
Runfile *types.ParsedRunfile
8887
Task *types.ParsedTask
@@ -141,16 +140,24 @@ func createCommandGroups(ctx Context, args CreateCommandGroupArgs) ([]executor.C
141140
}
142141
}
143142

144-
printCommand(args.Stdout.WithPrefix(""), strings.Join(args.Trail, "/"), "bash", strings.Join(commandsList, "\n"))
145-
146143
return CreateCommand(ctx, CmdArgs{
147144
Shell: args.Task.Shell,
148145
Env: fn.ToEnviron(args.Task.Env),
149146
Cmd: *cmd.Command,
150147
WorkingDir: args.Task.WorkingDir,
151148
interactive: args.Task.Interactive,
152-
Stdout: args.Stdout.WithPrefix(strings.Join(args.Trail, "/")),
153-
Stderr: args.Stderr.WithPrefix(strings.Join(args.Trail, "/")),
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+
}(),
154161
})
155162
})
156163

runner/writer.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (s *LogWriter) WithPrefix(prefix string) io.Writer {
3737
s.wg.Add(1)
3838
go func() {
3939
defer s.wg.Done()
40-
copyStream(prefix, s.w, pr)
40+
copyStreamLineByLine(prefix, s.w, pr)
4141
}()
4242
return &LineWriter{w: pw}
4343
}
@@ -46,22 +46,33 @@ func (s *LogWriter) Wait() {
4646
s.wg.Wait()
4747
}
4848

49-
func copyStream(prefix string, dest io.Writer, src io.Reader) {
49+
const (
50+
Reset = "\033[0m"
51+
Bold = "\033[1m"
52+
Green = "\033[32m"
53+
)
54+
55+
func copyStreamLineByLine(prefix string, dest io.Writer, src io.Reader) {
56+
hasPrefix := prefix != ""
57+
if hasPrefix && hasANSISupport() {
58+
prefix = fmt.Sprintf("%s[%s]%s ", Green, prefix, Reset)
59+
// prefix = fmt.Sprintf("%s%s |%s ", Green, prefix, Reset)
60+
}
5061
r := bufio.NewReader(src)
5162
for {
5263
b, err := r.ReadBytes('\n')
5364
if err != nil {
5465
if errors.Is(err, io.EOF) {
55-
if prefix != "" {
56-
dest.Write([]byte(fmt.Sprintf("[%s] ", prefix)))
66+
if hasPrefix {
67+
dest.Write([]byte(prefix))
5768
}
5869
dest.Write(b)
5970
return
6071
}
6172
}
6273

63-
if prefix != "" {
64-
dest.Write([]byte(fmt.Sprintf("[%s] ", prefix)))
74+
if hasPrefix {
75+
dest.Write([]byte(prefix))
6576
}
6677
dest.Write(b)
6778
}

0 commit comments

Comments
 (0)