Skip to content

Commit

Permalink
100 squats if this doesnt work
Browse files Browse the repository at this point in the history
  • Loading branch information
donuts-are-good committed Apr 7, 2023
1 parent 1fb63b6 commit 1c93e1e
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 82 deletions.
101 changes: 19 additions & 82 deletions colors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,92 +4,29 @@ import (
"runtime"
)

// Colors is the default no-dlc color pack. you got your regular colors and your bright colors. You also get an eraser free with every pack, folks. You can't beat it.
type Colors struct {
Nc string
BrightBlack string
BrightRed string
BrightGreen string
BrightYellow string
BrightPurple string
BrightMagenta string
BrightCyan string
BrightWhite string
Black string
Red string
Green string
Yellow string
Purple string
Magenta string
Cyan string
White string
}

// Variables containing ANSI codes
var (
// C is a pointer to an instance of Colors
C *Colors
Nc = "\033[0m"
BrightBlack = "\033[1;30m"
BrightRed = "\033[1;31m"
BrightGreen = "\033[1;32m"
BrightYellow = "\033[1;33m"
BrightPurple = "\033[1;34m"
BrightMagenta = "\033[1;35m"
BrightCyan = "\033[1;36m"
BrightWhite = "\033[1;37m"
Black = "\033[0;30m"
Red = "\033[0;31m"
Green = "\033[0;32m"
Yellow = "\033[0;33m"
Purple = "\033[0;34m"
Magenta = "\033[0;35m"
Cyan = "\033[0;36m"
White = "\033[0;37m"
)

// init sets up the Colors struct with the corresponding escape codes for each color
func init() {
C = &Colors{
Nc: "\033[0m",
BrightBlack: "\033[1;30m",
BrightRed: "\033[1;31m",
BrightGreen: "\033[1;32m",
BrightYellow: "\033[1;33m",
BrightPurple: "\033[1;34m",
BrightMagenta: "\033[1;35m",
BrightCyan: "\033[1;36m",
BrightWhite: "\033[1;37m",
Black: "\033[0;30m",
Red: "\033[0;31m",
Green: "\033[0;32m",
Yellow: "\033[0;33m",
Purple: "\033[0;34m",
Magenta: "\033[0;35m",
Cyan: "\033[0;36m",
White: "\033[0;37m",
}
// if running on Windows, set all colors to an empty string to disable color
if runtime.GOOS == "windows" {
C.Nc = ""
C.BrightBlack = ""
C.BrightRed = ""
C.BrightGreen = ""
C.BrightYellow = ""
C.BrightPurple = ""
C.BrightMagenta = ""
C.BrightCyan = ""
C.BrightWhite = ""
C.Black = ""
C.Red = ""
C.Green = ""
C.Yellow = ""
C.Purple = ""
C.Magenta = ""
C.Cyan = ""
C.White = ""
enableWindowsANSIColor()
}
}

// reset sets all the colors in the Colors struct back to their original values
func (c *Colors) Reset() {
c.Nc = "\033[0m"
c.BrightBlack = "\033[1;30m"
c.BrightRed = "\033[1;31m"
c.BrightGreen = "\033[1;32m"
c.BrightYellow = "\033[1;33m"
c.BrightPurple = "\033[1;34m"
c.BrightMagenta = "\033[1;35m"
c.BrightCyan = "\033[1;36m"
c.BrightWhite = "\033[1;37m"
c.Black = "\033[0;30m"
c.Red = "\033[0;31m"
c.Green = "\033[0;32m"
c.Yellow = "\033[0;33m"
c.Purple = "\033[0;34m"
c.Magenta = "\033[0;35m"
c.Cyan = "\033[0;36m"
c.White = "\033[0;37m"
}
8 changes: 8 additions & 0 deletions colors_others.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build !windows
// +build !windows

package colors

func enableWindowsANSIColor() {
// No-op for non-Windows platforms
}
18 changes: 18 additions & 0 deletions colors_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//go:build windows
// +build windows

package colors

import (
"os"

"golang.org/x/sys/windows"
)

func enableWindowsANSIColor() {
stdout := windows.Handle(os.Stdout.Fd())
var originalMode uint32

windows.GetConsoleMode(stdout, &originalMode)
windows.SetConsoleMode(stdout, originalMode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING)
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/donuts-are-good/colors

go 1.20

require golang.org/x/sys v0.7.0
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

0 comments on commit 1c93e1e

Please sign in to comment.