X Tutup
Skip to content

hackday: gh credits#840

Merged
vilmibm merged 3 commits intomasterfrom
credits
May 8, 2020
Merged

hackday: gh credits#840
vilmibm merged 3 commits intomasterfrom
credits

Conversation

@vilmibm
Copy link
Copy Markdown
Contributor

@vilmibm vilmibm commented Apr 27, 2020

We decided to merge this into the product and it's now ready for review.

The code is Not Good but it's self-contained and introduces no new third party dependencies. There are no tests for hopefully obvious reasons.


this PR adds a new top level command, gh credits:

credits3

considerations:

  • it defaults to showing contributors for cli/cli but you can specify any repo as a string (eg rails/rails) and it will show contributors for that repo
  • it will not attempt to animate if we're outputting to a pipe/file
  • specifying -s makes this intelligible by a screen reader
  • unfortunately on windows it just prints static text; the terminal there was /very/ slow at redrawing so as to preclude animation.
  • it will fail in spectacular and fun ways for weird/small terminal sizes. at least, narrow widths.
  • the code is REALLY bad because i wrote it in a very big rush

@vilmibm
Copy link
Copy Markdown
Contributor Author

vilmibm commented Apr 27, 2020

bug: stops animating too soon if window is too short. should just be skipping lines if startx < 0

fixed

@vilmibm
Copy link
Copy Markdown
Contributor Author

vilmibm commented May 4, 2020

we've decided to ship this after a short todo list:

  • write usage/help
  • add flag to disable animation and print something screen reader friendly
  • default to static thank you on windows because of animation delay looking really unpleasant
  • consolidate screen clearing code since we don't need to worry about windows

this commit is the result of April's hack day. it animates a thank you
for a project's contributors.

it only really works on linux/mac; animation woes led me to just make it
static and plain on windows.
@vilmibm vilmibm requested review from mislav and probablycorey May 4, 2020 20:49
Copy link
Copy Markdown
Contributor

@mislav mislav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fun!

return line
}

func twinkle(starLine string) string {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Best function ✨

lines = append(lines, logins...)
lines = append(lines, "( <3 press ctrl-c to quit <3 )")

termWidth, termHeight, err := terminal.GetSize(int(outFile.Fd()))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that GetSize might not work on cygwin, where we had to use tput cols to measure terminal width

if w, _, err := terminal.GetSize(int(outFile.Fd())); err == nil {
ttyWidth = w
} else if isCygwin {
tputCmd := exec.Command("tput", "cols")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when we run under cygwin wouldn't we still see a runtime of windows?

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")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: if the overhead of shelling out turns out to be too slow for us (with a 300ms refresh rate, that might not yet be the case), you can consider clearing not by running a command, but by sending terminal escape codes

  • fmt.Print("\033c") - clears the screen (for me, iTerm macOS)
  • fmt.Println("\x1b[%dG") - since we draw the entire screen every time, we could get away without clearing. this repositions the printing cursor to the start of the terminal

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah nice; it has looked fine for me on linux (so i didn't investigate speeding up) but this is great to know


gh credits # see a credits animation for this project
gh credits owner/repo # see a credits animation for owner/repo
gh credts -s # display a static thank you instead of animating
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo


RootCmd.AddCommand(creditsCmd)

creditsCmd.Flags().BoolP("static", "s", false, "Print a static version of the credits")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps instead of "static" in the description, we can say "Print a non-animated version of the credits"? If a person hasn't run credits yet, they might not realize that the default is animated, so it would be puzzling as to what "static" means


func init() {
rand.Seed(time.Now().UnixNano())
isWindows = runtime.GOOS == "windows"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this check is cheap and as such I don't think it needs to be cached in a variable

`

func init() {
rand.Seed(time.Now().UnixNano())
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: since we only need to randomize seed for credits output, can we instead move this line to credits() function (and perhaps only to the animation portion)?

This is to reduce doing things in package-level init if possible

rand.Seed(time.Now().UnixNano())
isWindows = runtime.GOOS == "windows"

RootCmd.AddCommand(creditsCmd)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At first I was thinking that maybe this command belongs to under gh repo, e.g. gh repo credits [<repo>]. Since this command basically displays information about a given repository, grouping it together with other gh repo commands made sense to me.

However, since the default is showing credits for cli/cli (as opposed to using the current repo), for that reason alone I think it deserves to stay as top-level command.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that was my thinking as well (about how it's mainly for seeing cli/cli's credits)

@vilmibm vilmibm merged commit 07dc617 into master May 8, 2020
@mislav mislav deleted the credits branch June 25, 2020 14:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

X Tutup