| 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 | // |
| 13 | // entry #1 |
| 14 | // entry #2 |
| 15 | // msg: ..., done. |
| 16 | type ListTask struct { |
| 17 | msg string |
| 18 | ch chan *Update |
| 19 | } |
| 20 | |
| 21 | // NewListTask instantiates a new *ListTask instance with the given message. |
| 22 | func NewListTask(msg string) *ListTask { |
| 23 | return &ListTask{ |
| 24 | msg: msg, |
| 25 | ch: make(chan *Update, 1), |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | // Entry logs a line-delimited task entry. |
| 30 | func (l *ListTask) Entry(update string) { |
| 31 | l.ch <- &Update{ |
| 32 | S: fmt.Sprintf("%s\n", update), |
| 33 | At: time.Now(), |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func (l *ListTask) Complete() { |
| 38 | l.ch <- &Update{ |
| 39 | S: fmt.Sprintf("%s: ...", l.msg), |
| 40 | At: time.Now(), |
| 41 | } |
| 42 | close(l.ch) |
| 43 | } |
| 44 | |
| 45 | // Throttled implements the Task.Throttled function and ensures that all log |
| 46 | // updates are printed to the sink. |
| 47 | func (l *ListTask) Throttled() bool { return false } |
| 48 | |
| 49 | // Updates implements the Task.Updates function and returns a channel of updates |
| 50 | // to log to the sink. |
| 51 | func (l *ListTask) Updates() <-chan *Update { return l.ch } |