-
Notifications
You must be signed in to change notification settings - Fork 2
/
ux.go
151 lines (139 loc) · 4.04 KB
/
ux.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
145
146
147
148
149
150
151
package ctoai
import (
"fmt"
"os"
"github.com/cto-ai/sdk-go/v2/internal/daemon"
)
// Ux is the object that contains the UX methods
type Ux struct{}
// NewUx creates a new Ux object and returns it
func NewUx() *Ux {
return &Ux{}
}
// Bold adds formatting for boldface type to the given text
func (*Ux) Bold(text string) string {
if os.Getenv("SDK_INTERFACE_TYPE") == "slack" {
return fmt.Sprintf("*%s*", text)
}
return fmt.Sprintf("\033[1m%s\033[0m", text)
}
// Italic adds formatting for italic type to the given text
func (*Ux) Italic(text string) string {
if os.Getenv("SDK_INTERFACE_TYPE") == "slack" {
return fmt.Sprintf("_%s_", text)
}
return fmt.Sprintf("\033[3m%s\033[23m", text)
}
// Print prints text to the output interface (i.e. terminal/slack).
//
// Example:
//
// u := ctoai.NewUx()
// err := u.Print("testing")
// if err != nil {
// panic(err)
// }
//
// Output:
//
// testing
func (*Ux) Print(text string) error {
return daemon.SimpleRequest("print", daemon.PrintBody{Text: text}, "POST")
}
// SpinnerStart presents a spinner on the output interface
// (i.e. terminal or slack) that to spin until the SpinnerStop method
// is called.
//
// Example:
//
// u := ctoai.NewUx()
// err := u.SpinnerStart("Starting process...")
// if err != nil {
// panic(err)
// }
//
// Output:
// [spinner emoji w/ spinner animation here] Starting process...
func (*Ux) SpinnerStart(text string) error {
return daemon.SimpleRequest("start-spinner", daemon.SpinnerStartBody{Text: text}, "POST")
}
// SpinnerStop stops a spinner that has been previously started on the
// interface (i.e. terminal or slack).
//
// Example:
//
// ... //previous spinner started here
//
// err := u.SpinnerStop("Done!")
// if err != nil {
// panic(err)
// }
//
// Output:
// [spinner completed completed here] Done!
func (*Ux) SpinnerStop(text string) error {
return daemon.SimpleRequest("stop-spinner", daemon.SpinnerStopBody{Text: text}, "POST")
}
// ProgressBarStart presents a progressbar on the output interface
// (i.e. terminal or slack) that will stay present until the
// progressbar stop method is called.
//
// The input length is the total length of the progress bar, e.g.
// if you have 5 steps in your logic, then a unit length of 5 might be
// and appropriate length.
//
// The initial length indicates the unit length (out of total length) that is initially
// filled at the start.
//
// Example:
//
// u := ctoai.NewUx()
// err := u.ProgressBarStart(5, 1, "Downloading...")
// if err != nil {
// panic(err)
// }
//
// Output:
// [progressbar animation with 1/5 of the bar filled here] Downloading...
func (*Ux) ProgressBarStart(length, initial int, message string) error {
return daemon.SimpleRequest("progress-bar/start", daemon.ProgressBarStartBody{Length: length, Initial: initial, Text: message}, "POST")
}
// ProgressBarAdvance adds onto a progressbar that is already present
// on the interface (i.e. terminal or slack).
//
// The increment indicates the additional length (out of total length)
// that will be filled.
//
// Example:
//
// ...
// [progressbar animation with 1/5 of the bar filled here] Downloading...
// err := u.ProgressBarAdvance(1)
// if err != nil {
// panic(err)
// }
//
// Output:
// [progressbar animation with 2/5 of the bar filled here] Downloading...
func (*Ux) ProgressBarAdvance(increment int) error {
return daemon.SimpleRequest("progress-bar/advance", daemon.ProgressBarAdvanceBody{Increment: increment}, "POST")
}
// ProgressBarStop completes a progressbar that is already present on
// the interface (i.e. terminal or slack).
//
// The text will change the initial text (set from the ux.ProgressBarStart method).
//
// Example:
//
// ...
// [progressbar animation with 2/5 of the bar filled here] Downloading...
// err := u.ProgressBarStop("Done!")
// if err != nil {
// panic(err)
// }
//
// Output:
// [progressbar animation with 5/5 of the bar filled here] Done!
func (*Ux) ProgressBarStop(message string) error {
return daemon.SimpleRequest("progress-bar/stop", daemon.ProgressBarStopBody{Text: message}, "POST")
}