summaryrefslogtreecommitdiff
path: root/internal/askcli/command_watch.go
blob: 8d8e35fd365eef02b051b3d38cf42e8c91dfac75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package askcli

import (
	"bytes"
	"context"
	"fmt"
	"io"
	"time"
)

const (
	watchInterval   = 2 * time.Second
	ansiClearScreen = "\033[2J\033[H"
)

type watchTicker interface {
	C() <-chan time.Time
	Stop()
}

type realWatchTicker struct {
	ticker *time.Ticker
}

func (t realWatchTicker) C() <-chan time.Time {
	return t.ticker.C
}

func (t realWatchTicker) Stop() {
	t.ticker.Stop()
}

var newWatchTicker = func(interval time.Duration) watchTicker {
	return realWatchTicker{ticker: time.NewTicker(interval)}
}

// deferredWriter buffers writes for capture while preserving terminal
// width detection by delegating Fd to the underlying writer.
type deferredWriter struct {
	w   io.Writer
	buf bytes.Buffer
}

func (dw *deferredWriter) Write(p []byte) (int, error) {
	return dw.buf.Write(p)
}

func (dw *deferredWriter) Fd() uintptr {
	type fder interface{ Fd() uintptr }
	if f, ok := dw.w.(fder); ok {
		return f.Fd()
	}
	return 0
}

func (d *Dispatcher) handleWatch(ctx context.Context, args []string, stdout, stderr io.Writer) (int, error) {
	watchArgs := args[1:]
	if len(watchArgs) == 0 {
		watchArgs = []string{"list"}
	}
	if !watchCommandAllowed(watchArgs) {
		_, _ = io.WriteString(stderr, "error: ask watch supports read-only subcommands: list, all, ready, completed, info, dep list, urgency, help\n")
		return 1, nil
	}

	ticker := newWatchTicker(watchInterval)
	defer ticker.Stop()

	var lastOutput []byte
	for {
		output, code, err := d.watchOutput(ctx, watchArgs, stdout, stderr)
		if err != nil {
			return code, err
		}
		if !bytes.Equal(output, lastOutput) {
			_, _ = io.WriteString(stdout, ansiClearScreen)
			_, _ = stdout.Write(output)
			lastOutput = bytes.Clone(output)
		}
		if code != 0 {
			return code, nil
		}

		select {
		case <-ctx.Done():
			return 0, nil
		case <-ticker.C():
		}
	}
}

func watchCommandAllowed(args []string) bool {
	entry, ok := commandRegistry.get(args[0])
	if !ok {
		return false
	}
	if entry.readOnly {
		return true
	}
	// dep is not read-only as a whole, but dep list is.
	if args[0] == "dep" {
		return len(args) >= 2 && args[1] == "list"
	}
	return false
}

func (d *Dispatcher) watchOutput(ctx context.Context, args []string, realStdout, realStderr io.Writer) ([]byte, int, error) {
	outW := &deferredWriter{w: realStdout}
	errW := &deferredWriter{w: realStderr}
	code, err := d.dispatchCommand(ctx, append([]string(nil), args...), nil, outW, errW)
	if err != nil {
		return nil, code, fmt.Errorf("watch %s: %w", args[0], err)
	}
	// Combine stdout and stderr so warnings or errors emitted by the
	// watched subcommand are visible in the watched display.
	out := make([]byte, 0, outW.buf.Len()+errW.buf.Len())
	out = append(out, outW.buf.Bytes()...)
	out = append(out, errW.buf.Bytes()...)
	return out, code, nil
}