Imported Upstream version 2.4.0
[scm/test.git] / tasklog / list_task.go
1 package tasklog
2
3 import (
4         "fmt"
5         "time"
6 )
7
8 // ListTask is a Task implementation that logs all updates in a list where each
9 // entry is line-delimited.
10 //
11 // For example:
12 //   entry #1
13 //   entry #2
14 //   msg: ..., done
15 type ListTask struct {
16         msg string
17         ch  chan *Update
18 }
19
20 // NewListTask instantiates a new *ListTask instance with the given message.
21 func NewListTask(msg string) *ListTask {
22         return &ListTask{
23                 msg: msg,
24                 ch:  make(chan *Update, 1),
25         }
26 }
27
28 // Entry logs a line-delimited task entry.
29 func (l *ListTask) Entry(update string) {
30         l.ch <- &Update{
31                 S:  fmt.Sprintf("%s\n", update),
32                 At: time.Now(),
33         }
34 }
35
36 func (l *ListTask) Complete() {
37         l.ch <- &Update{
38                 S:  fmt.Sprintf("%s: ...", l.msg),
39                 At: time.Now(),
40         }
41         close(l.ch)
42 }
43
44 // Throttled implements the Task.Throttled function and ensures that all log
45 // updates are printed to the sink.
46 func (l *ListTask) Throttled() bool { return false }
47
48 // Updates implements the Task.Updates function and returns a channel of updates
49 // to log to the sink.
50 func (l *ListTask) Updates() <-chan *Update { return l.ch }