-
Notifications
You must be signed in to change notification settings - Fork 112
/
task.go
144 lines (128 loc) · 2.65 KB
/
task.go
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"fmt"
"github.com/chelnak/ysmrr"
"os"
"strings"
)
type TaskManager struct {
sm ysmrr.SpinnerManager
isOut bool
noProgress bool
}
type Task struct {
spinner *ysmrr.Spinner
manager *TaskManager
title string
}
func InitTaskManager(jsonOutput, unixOutput bool) *TaskManager {
isOut := !jsonOutput || unixOutput
tm := &TaskManager{sm: ysmrr.NewSpinnerManager(), isOut: isOut, noProgress: unixOutput}
if isOut && !unixOutput {
tm.sm.Start()
}
return tm
}
func (tm *TaskManager) Reset() {
if tm.isOut && !tm.noProgress {
tm.sm.Stop()
tm.sm = ysmrr.NewSpinnerManager()
tm.sm.Start()
}
}
func (tm *TaskManager) Stop() {
if tm.isOut && !tm.noProgress {
tm.sm.Stop()
}
}
func (tm *TaskManager) Println(message string) {
if tm.noProgress {
fmt.Println(message)
return
}
if tm.isOut {
context := &Task{manager: tm}
context.spinner = tm.sm.AddSpinner(message)
context.Complete()
}
}
func (tm *TaskManager) RunWithTrigger(enable bool, title string, callback func(task *Task)) {
if enable {
tm.Run(title, callback)
}
}
func (tm *TaskManager) Run(title string, callback func(task *Task)) {
context := &Task{manager: tm, title: title}
if tm.isOut {
if tm.noProgress {
//fmt.Println(title)
} else {
context.spinner = tm.sm.AddSpinner(title)
}
}
callback(context)
}
func (tm *TaskManager) AsyncRun(title string, callback func(task *Task)) {
context := &Task{manager: tm, title: title}
if tm.isOut {
if tm.noProgress {
//fmt.Println(title)
} else {
context.spinner = tm.sm.AddSpinner(title)
}
}
go callback(context)
}
func (t *Task) Complete() {
if t.manager.noProgress {
return
}
if t.spinner == nil {
return
}
t.spinner.Complete()
}
func (t *Task) Updatef(format string, a ...interface{}) {
if t.spinner == nil || t.manager.noProgress {
return
}
t.spinner.UpdateMessagef(format, a...)
}
func (t *Task) Update(format string) {
if t.spinner == nil || t.manager.noProgress {
return
}
t.spinner.UpdateMessage(format)
}
func (t *Task) Println(message string) {
if t.manager.noProgress {
fmt.Println(message)
return
}
if t.spinner == nil {
return
}
t.spinner.UpdateMessage(message)
}
func (t *Task) Printf(format string, a ...interface{}) {
if t.manager.noProgress {
fmt.Printf(format+"\n", a...)
return
}
if t.spinner == nil {
return
}
t.spinner.UpdateMessagef(format, a...)
}
func (t *Task) CheckError(err error) {
if err != nil {
if t.spinner != nil {
t.Printf("Fatal: %s, err: %v", strings.ToLower(t.title), err)
t.spinner.Error()
t.manager.Stop()
} else {
fmt.Printf("Fatal: %s, err: %v", strings.ToLower(t.title), err)
}
os.Exit(0)
}
}