forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcredits.go
More file actions
246 lines (206 loc) · 5.17 KB
/
credits.go
File metadata and controls
246 lines (206 loc) · 5.17 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package command
import (
"bytes"
"fmt"
"math"
"math/rand"
"os"
"os/exec"
"runtime"
"strings"
"time"
"github.com/spf13/cobra"
"golang.org/x/crypto/ssh/terminal"
"github.com/cli/cli/utils"
)
var thankYou = `
_ _
| | | |
_|_ | | __, _ _ | | __
| |/ \ / | / |/ | |/_) | | / \_| |
|_/| |_/\_/|_/ | |_/| \_/ \_/|/\__/ \_/|_/
/|
\|
_
o | | |
__ __ _ _ _|_ ,_ | | _|_ __ ,_ , |
/ / \_/ |/ | | / | | |/ \_| | | / \_/ | / \_|
\___/\__/ | |_/|_/ |_/|_/\_/ \_/|_/|_/\__/ |_/ \/ o
`
func init() {
RootCmd.AddCommand(creditsCmd)
creditsCmd.Flags().BoolP("static", "s", false, "Print a static version of the credits")
}
var creditsCmd = &cobra.Command{
Use: "credits [repository]",
Short: "View project's credits",
Long: `View animated credits for this or another project.
Examples:
gh credits # see a credits animation for this project
gh credits owner/repo # see a credits animation for owner/repo
gh credits -s # display a non-animated thank you
gh credits | cat # just print the contributors, one per line
`,
Args: cobra.MaximumNArgs(1),
RunE: credits,
}
func credits(cmd *cobra.Command, args []string) error {
isWindows := runtime.GOOS == "windows"
ctx := contextForCommand(cmd)
client, err := apiClientForContext(ctx)
if err != nil {
return err
}
owner := "cli"
repo := "cli"
if len(args) > 0 {
parts := strings.SplitN(args[0], "/", 2)
owner = parts[0]
repo = parts[1]
}
type Contributor struct {
Login string
}
type Result []Contributor
result := Result{}
body := bytes.NewBufferString("")
path := fmt.Sprintf("repos/%s/%s/contributors", owner, repo)
err = client.REST("GET", path, body, &result)
if err != nil {
return err
}
out := cmd.OutOrStdout()
isTTY := false
outFile, isFile := out.(*os.File)
if isFile {
isTTY = utils.IsTerminal(outFile)
if isTTY {
// FIXME: duplicates colorableOut
out = utils.NewColorable(outFile)
}
}
static, err := cmd.Flags().GetBool("static")
if err != nil {
return err
}
static = static || isWindows
if isTTY && static {
fmt.Fprintln(out, "THANK YOU CONTRIBUTORS!!! <3")
fmt.Println()
}
logins := []string{}
for x, c := range result {
if isTTY && !static {
logins = append(logins, getColor(x)(c.Login))
} else {
fmt.Fprintf(out, "%s\n", c.Login)
}
}
if !isTTY || static {
return nil
}
rand.Seed(time.Now().UnixNano())
lines := []string{}
thankLines := strings.Split(thankYou, "\n")
for x, tl := range thankLines {
lines = append(lines, getColor(x)(tl))
}
lines = append(lines, "")
lines = append(lines, logins...)
lines = append(lines, "( <3 press ctrl-c to quit <3 )")
termWidth, termHeight, err := terminal.GetSize(int(outFile.Fd()))
if err != nil {
return err
}
margin := termWidth / 3
starLinesLeft := []string{}
for x := 0; x < len(lines); x++ {
starLinesLeft = append(starLinesLeft, starLine(margin))
}
starLinesRight := []string{}
for x := 0; x < len(lines); x++ {
lineWidth := termWidth - (margin + len(lines[x]))
starLinesRight = append(starLinesRight, starLine(lineWidth))
}
loop := true
startx := termHeight - 1
li := 0
for loop {
clear()
for x := 0; x < termHeight; x++ {
if x == startx || startx < 0 {
starty := 0
if startx < 0 {
starty = int(math.Abs(float64(startx)))
}
for y := starty; y < li+1; y++ {
if y >= len(lines) {
continue
}
starLineLeft := starLinesLeft[y]
starLinesLeft[y] = twinkle(starLineLeft)
starLineRight := starLinesRight[y]
starLinesRight[y] = twinkle(starLineRight)
fmt.Fprintf(out, "%s %s %s\n", starLineLeft, lines[y], starLineRight)
}
li += 1
x += li
} else {
fmt.Fprintf(out, "\n")
}
}
if li < len(lines) {
startx -= 1
}
time.Sleep(300 * time.Millisecond)
}
return nil
}
func starLine(width int) string {
line := ""
starChance := 0.1
for y := 0; y < width; y++ {
chance := rand.Float64()
if chance <= starChance {
charRoll := rand.Float64()
switch {
case charRoll < 0.3:
line += "."
case charRoll > 0.3 && charRoll < 0.6:
line += "+"
default:
line += "*"
}
} else {
line += " "
}
}
return line
}
func twinkle(starLine string) string {
starLine = strings.ReplaceAll(starLine, ".", "P")
starLine = strings.ReplaceAll(starLine, "+", "A")
starLine = strings.ReplaceAll(starLine, "*", ".")
starLine = strings.ReplaceAll(starLine, "P", "+")
starLine = strings.ReplaceAll(starLine, "A", "*")
return starLine
}
func getColor(x int) func(string) string {
rainbow := []func(string) string{
utils.Magenta,
utils.Red,
utils.Yellow,
utils.Green,
utils.Cyan,
utils.Blue,
}
ix := x % len(rainbow)
return rainbow[ix]
}
func clear() {
// on windows we'd do cmd := exec.Command("cmd", "/c", "cls"); unfortunately the draw speed is so
// slow that the animation is very jerky, flashy, and painful to look at.
cmd := exec.Command("clear")
cmd.Stdout = os.Stdout
_ = cmd.Run()
}