Skip to content

Commit

Permalink
docs(example): minor example edits
Browse files Browse the repository at this point in the history
  • Loading branch information
meowgorithm committed Sep 16, 2024
1 parent 773a0e5 commit 17d6666
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func main() {
// Find the color profile for stdout.
// Detect the color profile for stdout.
p := colorprofile.Detect(os.Stdout, os.Environ())
fmt.Printf("Your color profile is what we call '%s'.\n\n", p)

Expand All @@ -33,12 +33,12 @@ func main() {

// Here's a nice color.
myCuteColor := color.RGBA{0x6b, 0x50, 0xff, 0xff} // #6b50ff
fmt.Printf("A cute color we like is: #%x%x%x.\n\n", myCuteColor.R, myCuteColor.G, myCuteColor.B)
fmt.Printf("A cute color we like is: %s.\n\n", colorToHex(myCuteColor))

// Let's convert it to the detected color profile.
theColorWeNeed := p.Convert(myCuteColor)
fmt.Printf("This terminal needs it to be a %T, at best...\n", theColorWeNeed)
fmt.Printf("...which would be %#v.\n\n", theColorWeNeed)
fmt.Printf("This terminal needs that color to be a %T, at best.\n", theColorWeNeed)
fmt.Printf("In this case that color is %s.\n\n", colorToHex(theColorWeNeed))

// Now let's convert it to a color profile that only supports up to 256
// colors.
Expand All @@ -48,15 +48,20 @@ func main() {
// But really, who has time to convert? Not you? Well, kiddo, here's
// a magical writer that will just auto-convert whatever ANSI you throw at
// it to the appropriate color profile.
myFancyANSI := "\x1b[38;2;107;80;255mCute puppy!!\x1b[m\n"
myFancyANSI := "\x1b[38;2;107;80;255mCute puppy!!\x1b[m"
w := colorprofile.NewWriter(os.Stdout, os.Environ())
w.Printf(myFancyANSI)
w.Println("This terminal:", myFancyANSI)

Check failure on line 53 in example/main.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `w.Println` is not checked (errcheck)

Check failure on line 53 in example/main.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `w.Println` is not checked (errcheck)

// But we're old school. Make the writer only use 4-bit ANSI, 1980s style.
w.Profile = colorprofile.ANSI
w.Printf(myFancyANSI)
w.Println("4-bit ANSI:", myFancyANSI)

Check failure on line 57 in example/main.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `w.Println` is not checked (errcheck)

Check failure on line 57 in example/main.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `w.Println` is not checked (errcheck)

// That's too modern. Let's go back to MIT in the 1970s.
// That's way too modern. Let's go back to MIT in the 1970s.
w.Profile = colorprofile.NoTTY
w.Printf(myFancyANSI) // not so fancy anymore
w.Println("No TTY :(", myFancyANSI) // less fancy

Check failure on line 61 in example/main.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `w.Println` is not checked (errcheck)

Check failure on line 61 in example/main.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `w.Println` is not checked (errcheck)
}

func colorToHex(c color.Color) string {
r, g, b, _ := c.RGBA()
return fmt.Sprintf("#%02x%02x%02x", r>>8, g>>8, b>>8)

Check failure on line 66 in example/main.go

View workflow job for this annotation

GitHub Actions / lint-soft

Magic number: 8, in <argument> detected (gomnd)

Check failure on line 66 in example/main.go

View workflow job for this annotation

GitHub Actions / lint-soft

Magic number: 8, in <argument> detected (gomnd)
}

0 comments on commit 17d6666

Please sign in to comment.