|
| 1 | +package command |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "math" |
| 7 | + "math/rand" |
| 8 | + "os" |
| 9 | + "os/exec" |
| 10 | + "runtime" |
| 11 | + "strings" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/spf13/cobra" |
| 15 | + "golang.org/x/crypto/ssh/terminal" |
| 16 | + |
| 17 | + "github.com/cli/cli/utils" |
| 18 | +) |
| 19 | + |
| 20 | +var thankYou = ` |
| 21 | + _ _ |
| 22 | + | | | | |
| 23 | +_|_ | | __, _ _ | | __ |
| 24 | + | |/ \ / | / |/ | |/_) | | / \_| | |
| 25 | + |_/| |_/\_/|_/ | |_/| \_/ \_/|/\__/ \_/|_/ |
| 26 | + /| |
| 27 | + \| |
| 28 | + _ |
| 29 | + o | | | |
| 30 | + __ __ _ _ _|_ ,_ | | _|_ __ ,_ , | |
| 31 | +/ / \_/ |/ | | / | | |/ \_| | | / \_/ | / \_| |
| 32 | +\___/\__/ | |_/|_/ |_/|_/\_/ \_/|_/|_/\__/ |_/ \/ o |
| 33 | +
|
| 34 | +
|
| 35 | +` |
| 36 | + |
| 37 | +func init() { |
| 38 | + RootCmd.AddCommand(creditsCmd) |
| 39 | + |
| 40 | + creditsCmd.Flags().BoolP("static", "s", false, "Print a static version of the credits") |
| 41 | +} |
| 42 | + |
| 43 | +var creditsCmd = &cobra.Command{ |
| 44 | + Use: "credits [repository]", |
| 45 | + Short: "View project's credits", |
| 46 | + Long: `View animated credits for this or another project. |
| 47 | +
|
| 48 | +Examples: |
| 49 | +
|
| 50 | + gh credits # see a credits animation for this project |
| 51 | + gh credits owner/repo # see a credits animation for owner/repo |
| 52 | + gh credits -s # display a non-animated thank you |
| 53 | + gh credits | cat # just print the contributors, one per line |
| 54 | +`, |
| 55 | + Args: cobra.MaximumNArgs(1), |
| 56 | + RunE: credits, |
| 57 | +} |
| 58 | + |
| 59 | +func credits(cmd *cobra.Command, args []string) error { |
| 60 | + isWindows := runtime.GOOS == "windows" |
| 61 | + ctx := contextForCommand(cmd) |
| 62 | + client, err := apiClientForContext(ctx) |
| 63 | + if err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + |
| 67 | + owner := "cli" |
| 68 | + repo := "cli" |
| 69 | + if len(args) > 0 { |
| 70 | + parts := strings.SplitN(args[0], "/", 2) |
| 71 | + owner = parts[0] |
| 72 | + repo = parts[1] |
| 73 | + } |
| 74 | + |
| 75 | + type Contributor struct { |
| 76 | + Login string |
| 77 | + } |
| 78 | + |
| 79 | + type Result []Contributor |
| 80 | + |
| 81 | + result := Result{} |
| 82 | + body := bytes.NewBufferString("") |
| 83 | + path := fmt.Sprintf("repos/%s/%s/contributors", owner, repo) |
| 84 | + |
| 85 | + err = client.REST("GET", path, body, &result) |
| 86 | + if err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + |
| 90 | + out := cmd.OutOrStdout() |
| 91 | + isTTY := false |
| 92 | + outFile, isFile := out.(*os.File) |
| 93 | + if isFile { |
| 94 | + isTTY = utils.IsTerminal(outFile) |
| 95 | + if isTTY { |
| 96 | + // FIXME: duplicates colorableOut |
| 97 | + out = utils.NewColorable(outFile) |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + static, err := cmd.Flags().GetBool("static") |
| 102 | + if err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + |
| 106 | + static = static || isWindows |
| 107 | + |
| 108 | + if isTTY && static { |
| 109 | + fmt.Fprintln(out, "THANK YOU CONTRIBUTORS!!! <3") |
| 110 | + fmt.Println() |
| 111 | + } |
| 112 | + |
| 113 | + logins := []string{} |
| 114 | + for x, c := range result { |
| 115 | + if isTTY && !static { |
| 116 | + logins = append(logins, getColor(x)(c.Login)) |
| 117 | + } else { |
| 118 | + fmt.Fprintf(out, "%s\n", c.Login) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + if !isTTY || static { |
| 123 | + return nil |
| 124 | + } |
| 125 | + |
| 126 | + rand.Seed(time.Now().UnixNano()) |
| 127 | + |
| 128 | + lines := []string{} |
| 129 | + |
| 130 | + thankLines := strings.Split(thankYou, "\n") |
| 131 | + for x, tl := range thankLines { |
| 132 | + lines = append(lines, getColor(x)(tl)) |
| 133 | + } |
| 134 | + lines = append(lines, "") |
| 135 | + lines = append(lines, logins...) |
| 136 | + lines = append(lines, "( <3 press ctrl-c to quit <3 )") |
| 137 | + |
| 138 | + termWidth, termHeight, err := terminal.GetSize(int(outFile.Fd())) |
| 139 | + if err != nil { |
| 140 | + return err |
| 141 | + } |
| 142 | + |
| 143 | + margin := termWidth / 3 |
| 144 | + |
| 145 | + starLinesLeft := []string{} |
| 146 | + for x := 0; x < len(lines); x++ { |
| 147 | + starLinesLeft = append(starLinesLeft, starLine(margin)) |
| 148 | + } |
| 149 | + |
| 150 | + starLinesRight := []string{} |
| 151 | + for x := 0; x < len(lines); x++ { |
| 152 | + lineWidth := termWidth - (margin + len(lines[x])) |
| 153 | + starLinesRight = append(starLinesRight, starLine(lineWidth)) |
| 154 | + } |
| 155 | + |
| 156 | + loop := true |
| 157 | + startx := termHeight - 1 |
| 158 | + li := 0 |
| 159 | + |
| 160 | + for loop { |
| 161 | + clear() |
| 162 | + for x := 0; x < termHeight; x++ { |
| 163 | + if x == startx || startx < 0 { |
| 164 | + starty := 0 |
| 165 | + if startx < 0 { |
| 166 | + starty = int(math.Abs(float64(startx))) |
| 167 | + } |
| 168 | + for y := starty; y < li+1; y++ { |
| 169 | + if y >= len(lines) { |
| 170 | + continue |
| 171 | + } |
| 172 | + starLineLeft := starLinesLeft[y] |
| 173 | + starLinesLeft[y] = twinkle(starLineLeft) |
| 174 | + starLineRight := starLinesRight[y] |
| 175 | + starLinesRight[y] = twinkle(starLineRight) |
| 176 | + fmt.Fprintf(out, "%s %s %s\n", starLineLeft, lines[y], starLineRight) |
| 177 | + } |
| 178 | + li += 1 |
| 179 | + x += li |
| 180 | + } else { |
| 181 | + fmt.Fprintf(out, "\n") |
| 182 | + } |
| 183 | + } |
| 184 | + if li < len(lines) { |
| 185 | + startx -= 1 |
| 186 | + } |
| 187 | + time.Sleep(300 * time.Millisecond) |
| 188 | + } |
| 189 | + |
| 190 | + return nil |
| 191 | +} |
| 192 | + |
| 193 | +func starLine(width int) string { |
| 194 | + line := "" |
| 195 | + starChance := 0.1 |
| 196 | + for y := 0; y < width; y++ { |
| 197 | + chance := rand.Float64() |
| 198 | + if chance <= starChance { |
| 199 | + charRoll := rand.Float64() |
| 200 | + switch { |
| 201 | + case charRoll < 0.3: |
| 202 | + line += "." |
| 203 | + case charRoll > 0.3 && charRoll < 0.6: |
| 204 | + line += "+" |
| 205 | + default: |
| 206 | + line += "*" |
| 207 | + } |
| 208 | + } else { |
| 209 | + line += " " |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + return line |
| 214 | +} |
| 215 | + |
| 216 | +func twinkle(starLine string) string { |
| 217 | + starLine = strings.ReplaceAll(starLine, ".", "P") |
| 218 | + starLine = strings.ReplaceAll(starLine, "+", "A") |
| 219 | + starLine = strings.ReplaceAll(starLine, "*", ".") |
| 220 | + starLine = strings.ReplaceAll(starLine, "P", "+") |
| 221 | + starLine = strings.ReplaceAll(starLine, "A", "*") |
| 222 | + return starLine |
| 223 | +} |
| 224 | + |
| 225 | +func getColor(x int) func(string) string { |
| 226 | + rainbow := []func(string) string{ |
| 227 | + utils.Magenta, |
| 228 | + utils.Red, |
| 229 | + utils.Yellow, |
| 230 | + utils.Green, |
| 231 | + utils.Cyan, |
| 232 | + utils.Blue, |
| 233 | + } |
| 234 | + |
| 235 | + ix := x % len(rainbow) |
| 236 | + |
| 237 | + return rainbow[ix] |
| 238 | +} |
| 239 | + |
| 240 | +func clear() { |
| 241 | + // on windows we'd do cmd := exec.Command("cmd", "/c", "cls"); unfortunately the draw speed is so |
| 242 | + // slow that the animation is very jerky, flashy, and painful to look at. |
| 243 | + cmd := exec.Command("clear") |
| 244 | + cmd.Stdout = os.Stdout |
| 245 | + _ = cmd.Run() |
| 246 | +} |
0 commit comments