Skip to content

Commit 51378c0

Browse files
committed
fix issue #104: add output limiter for asciinema
1 parent 776e1d5 commit 51378c0

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ cmd-test-1:
104104
semaphore add -- curl example.com; \
105105
semaphore add -- curl localhost; \
106106
cat /tmp/semaphore.json && echo ""; \
107-
semaphore wait --notify --timeout=10s; \
107+
semaphore wait --notify --speed=300 --timeout=10s; \
108108
echo $$?'
109109

110110
.PHONY: cmd-test-2
@@ -136,7 +136,7 @@ cmd-test-1-local:
136136
semaphore add -- curl unknown
137137
semaphore add -- curl example.com
138138
semaphore add -- curl localhost
139-
semaphore wait --notify --timeout=10s
139+
semaphore wait --notify --speed=300 --timeout=10s
140140
echo $$?
141141

142142
.PHONY: cmd-test-2-local

cmd/semaphore/cli.go

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,44 @@ type ColoredOutput struct {
214214
}
215215

216216
// Write implements io.Writer interface.
217-
func (c *ColoredOutput) Write(p []byte) (int, error) {
218-
return c.clr.Fprint(c.dst, string(p))
217+
func (o *ColoredOutput) Write(p []byte) (int, error) {
218+
return o.clr.Fprint(o.dst, string(p))
219+
}
220+
221+
// LimitedOutput wraps another output and writes to it with specified velocity.
222+
type LimitedOutput struct {
223+
dst io.Writer
224+
speed int
225+
}
226+
227+
// For sets limiter for passed io.Writer.
228+
func (o *LimitedOutput) For(dst io.Writer) *LimitedOutput {
229+
o.dst = dst
230+
return o
231+
}
232+
233+
// Write implements io.Writer interface.
234+
func (o *LimitedOutput) Write(p []byte) (int, error) {
235+
if o.speed != 0 {
236+
pause := time.Second / time.Duration(o.speed)
237+
var (
238+
total, n int
239+
err error
240+
)
241+
for _, r := range []rune(string(p)) {
242+
if r == 0 {
243+
continue
244+
}
245+
n, err = o.dst.Write([]byte(string(r)))
246+
total += n
247+
if err != nil {
248+
break
249+
}
250+
time.Sleep(pause)
251+
}
252+
return total, err
253+
}
254+
return o.dst.Write(p)
219255
}
220256

221257
// WaitCommand is a command to execute a semaphore task.
@@ -224,6 +260,7 @@ type WaitCommand struct {
224260
CmdName string
225261
Notify bool
226262
Output io.Writer
263+
Speed int
227264
Template *template.Template
228265
Timeout time.Duration
229266
}
@@ -234,6 +271,7 @@ func (c *WaitCommand) FlagSet() *flag.FlagSet {
234271
c.Flags = c.BaseCommand.FlagSet(c.CmdName)
235272
c.Flags.BoolVar(&c.Notify, "notify", false, "show notification at the end (not implemented yet)")
236273
c.Flags.DurationVar(&c.Timeout, "timeout", time.Minute, "timeout for task execution")
274+
c.Flags.IntVar(&c.Speed, "speed", 0, "a velocity of report output (characters per second)")
237275
}
238276
return c.Flags
239277
}
@@ -268,6 +306,7 @@ func (c *WaitCommand) Do() error {
268306
bar = pb.New(len(task.Jobs))
269307
results = &Results{}
270308
red = &ColoredOutput{clr: color.New(color.FgHiRed), dst: c.Output}
309+
limiter = &LimitedOutput{speed: c.Speed}
271310
success, failure = 0, 0
272311
output io.Writer
273312
start, end time.Time
@@ -290,12 +329,13 @@ func (c *WaitCommand) Do() error {
290329
bar.Finish()
291330

292331
for _, result := range results.Sort() {
332+
output = c.Output
293333
if result.Error != nil {
294334
output = red
295335
}
296336
stdout, _ := ioutil.ReadAll(result.Stdout)
297337
stderr, _ := ioutil.ReadAll(result.Stderr)
298-
err = errors.WithMessage(c.Template.Execute(output, struct {
338+
err = errors.WithMessage(c.Template.Execute(limiter.For(output), struct {
299339
Name string
300340
Args []string
301341
Error error

0 commit comments

Comments
 (0)