diff --git a/colors.go b/colors.go index 0d01805..74fbd05 100644 --- a/colors.go +++ b/colors.go @@ -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" -} diff --git a/colors_others.go b/colors_others.go new file mode 100644 index 0000000..2a8acf5 --- /dev/null +++ b/colors_others.go @@ -0,0 +1,8 @@ +//go:build !windows +// +build !windows + +package colors + +func enableWindowsANSIColor() { + // No-op for non-Windows platforms +} diff --git a/colors_windows.go b/colors_windows.go new file mode 100644 index 0000000..930da14 --- /dev/null +++ b/colors_windows.go @@ -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) +} diff --git a/go.mod b/go.mod index 2b5a48d..6c80b25 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/donuts-are-good/colors go 1.20 + +require golang.org/x/sys v0.7.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..7c22a2b --- /dev/null +++ b/go.sum @@ -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=