|
| 1 | +package gostyle |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "os/exec" |
| 7 | + "strings" |
| 8 | + "syscall" |
| 9 | + "unsafe" |
| 10 | +) |
| 11 | + |
| 12 | +// please don't skid or you're a noob lol |
| 13 | +var ( |
| 14 | + k32 = syscall.NewLazyDLL("kernel32.dll") |
| 15 | + procSetConsoleMode = k32.NewProc("SetConsoleMode") |
| 16 | + procGetConsoleMode = k32.NewProc("GetConsoleMode") |
| 17 | + pgsh = k32.NewProc("GetStdHandle") |
| 18 | + pgcci = k32.NewProc("GetConsoleCursorInfo") |
| 19 | + pscci = k32.NewProc("SetConsoleCursorInfo") |
| 20 | + // these are static colors, made for simple printing they are not assosicated with gradients colors at all. |
| 21 | + Reset = "\033[0m" |
| 22 | + Red = "\033[31m" |
| 23 | + Green = "\033[32m" |
| 24 | + Yellow = "\033[33m" |
| 25 | + Blue = "\033[34m" |
| 26 | + Magenta = "\033[35m" |
| 27 | + Cyan = "\033[36m" |
| 28 | + White = "\033[37m" |
| 29 | + // these are gradient colors, there arent assosicated with static ones, these are the colors that have transitions |
| 30 | + BLACK_TO_WHITE = []int{0, 0, 0, 255, 255, 255} |
| 31 | + BLACK_TO_RED = []int{0, 0, 0, 255, 0, 0} |
| 32 | + BLACK_TO_GREEN = []int{0, 0, 0, 0, 255, 0} |
| 33 | + BLACK_TO_BLUE = []int{0, 0, 0, 0, 0, 255} |
| 34 | + WHITE_TO_BLACK = []int{255, 255, 255, 0, 0, 0} |
| 35 | + WHITE_TO_RED = []int{255, 255, 255, 255, 0, 0} |
| 36 | + WHITE_TO_GREEN = []int{255, 255, 255, 0, 255, 0} |
| 37 | + WHITE_TO_BLUE = []int{255, 255, 255, 0, 0, 255} |
| 38 | + RED_TO_BLACK = []int{255, 0, 0, 0, 0, 0} |
| 39 | + RED_TO_WHITE = []int{255, 0, 0, 255, 255, 255} |
| 40 | + RED_TO_YELLOW = []int{255, 0, 0, 255, 255, 0} |
| 41 | + RED_TO_PURPLE = []int{255, 0, 0, 255, 0, 255} |
| 42 | + GREEN_TO_BLACK = []int{0, 255, 0, 0, 0, 0} |
| 43 | + GREEN_TO_WHITE = []int{0, 255, 0, 255, 255, 255} |
| 44 | + GREEN_TO_YELLOW = []int{0, 255, 0, 255, 255, 0} |
| 45 | + GREEN_TO_CYAN = []int{0, 255, 0, 0, 255, 255} |
| 46 | + BLUE_TO_BLACK = []int{0, 0, 255, 0, 0, 0} |
| 47 | + BLUE_TO_WHITE = []int{0, 0, 255, 255, 255, 255} |
| 48 | + BLUE_TO_CYAN = []int{0, 0, 255, 0, 255, 255} |
| 49 | + BLUE_TO_PURPLE = []int{0, 0, 255, 255, 0, 255} |
| 50 | + YELLOW_TO_RED = []int{255, 255, 0, 255, 0, 0} |
| 51 | + YELLOW_TO_GREEN = []int{255, 255, 0, 0, 255, 0} |
| 52 | + PURPLE_TO_RED = []int{255, 0, 255, 255, 0, 0} |
| 53 | + PURPLE_TO_BLUE = []int{255, 0, 255, 0, 0, 255} |
| 54 | + CYAN_TO_GREEN = []int{0, 255, 255, 0, 255, 0} |
| 55 | + CYAN_TO_BLUE = []int{0, 255, 255, 0, 0, 255} |
| 56 | + |
| 57 | + // this is init, this makes sure that colors print out successfully |
| 58 | + ENABLE_VIRTUAL_TERMINAL_PROCESSING uint32 = 0x0004 |
| 59 | +) |
| 60 | + |
| 61 | +/* |
| 62 | + GradientFade applies a gradient color effect to text. |
| 63 | +*/ |
| 64 | +func GradientFade(text string, colors []int) string { |
| 65 | + var result string |
| 66 | + for i, char := range text { |
| 67 | + currentR := int(float64(colors[0]) + (float64(colors[3]-colors[0])/float64(len(text)-1))*float64(i)) |
| 68 | + currentG := int(float64(colors[1]) + (float64(colors[4]-colors[1])/float64(len(text)-1))*float64(i)) |
| 69 | + currentB := int(float64(colors[2]) + (float64(colors[5]-colors[2])/float64(len(text)-1))*float64(i)) |
| 70 | + |
| 71 | + result += fmt.Sprintf("\x1b[38;2;%d;%d;%dm%c", currentR, currentG, currentB, char) |
| 72 | + } |
| 73 | + result += Reset |
| 74 | + return result |
| 75 | +} |
| 76 | + |
| 77 | +/* |
| 78 | + Init initializes the console with ANSI color support. |
| 79 | +*/ |
| 80 | + |
| 81 | +func Init() error { |
| 82 | + handle := syscall.Handle(os.Stdout.Fd()) |
| 83 | + |
| 84 | + var mode uint32 |
| 85 | + ret, _, err := procGetConsoleMode.Call(uintptr(handle), uintptr(unsafe.Pointer(&mode))) |
| 86 | + if ret == 0 { |
| 87 | + return err |
| 88 | + } |
| 89 | + |
| 90 | + mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING |
| 91 | + |
| 92 | + ret, _, err = procSetConsoleMode.Call(uintptr(handle), uintptr(mode)) |
| 93 | + if ret == 0 { |
| 94 | + return err |
| 95 | + } |
| 96 | + |
| 97 | + return nil |
| 98 | +} |
| 99 | + |
| 100 | +const ( |
| 101 | + soh = uint32(-11 & 0xFFFFFFFF) |
| 102 | +) |
| 103 | + |
| 104 | +type CONSOLE_CURSOR_INFO struct { |
| 105 | + Size uint32 |
| 106 | + Visible int32 |
| 107 | +} |
| 108 | +// gets console handle |
| 109 | +func getConsoleHandle() uintptr { |
| 110 | + handle, _, _ := pgsh.Call(uintptr(soh)) |
| 111 | + return handle |
| 112 | +} |
| 113 | +// hide console cursror |
| 114 | +func HideCursor() { |
| 115 | + handle := getConsoleHandle() |
| 116 | + var ci CONSOLE_CURSOR_INFO |
| 117 | + ci.Size = uint32(unsafe.Sizeof(ci)) |
| 118 | + pgcci.Call(handle, uintptr(unsafe.Pointer(&ci))) |
| 119 | + ci.Visible = 0 |
| 120 | + pscci.Call(handle, uintptr(unsafe.Pointer(&ci))) |
| 121 | +} |
| 122 | +// show console cursor |
| 123 | +func ShowCursor() { |
| 124 | + handle := getConsoleHandle() |
| 125 | + var ci CONSOLE_CURSOR_INFO |
| 126 | + ci.Size = uint32(unsafe.Sizeof(ci)) |
| 127 | + pgcci.Call(handle, uintptr(unsafe.Pointer(&ci))) |
| 128 | + ci.Visible = 1 |
| 129 | + pscci.Call(handle, uintptr(unsafe.Pointer(&ci))) |
| 130 | +} |
| 131 | + |
| 132 | +/* |
| 133 | + CenterText centers text within the console window. |
| 134 | +*/ |
| 135 | + |
| 136 | +func CenterText(text string) string { |
| 137 | + width, _ := getConsoleWindowSize() |
| 138 | + lines := strings.Split(text, "\n") |
| 139 | + var centeredText string |
| 140 | + for _, line := range lines { |
| 141 | + // Calculate visible length after applying color codes |
| 142 | + visibleLength := len(line) - strings.Count(line, "\033[")*7 |
| 143 | + |
| 144 | + centeredText += fmt.Sprintf("%*s%s\n", (width-visibleLength)/2, "", line) |
| 145 | + } |
| 146 | + return centeredText |
| 147 | +} |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | +/* |
| 152 | + WriteColorized writes colorized text to the console, optionally centering it. |
| 153 | +*/ |
| 154 | +func WriteColorized(text, color string, center bool) { |
| 155 | + if center { |
| 156 | + text = CenterText(text) |
| 157 | + } |
| 158 | + formattedText := Colorize(text, color) |
| 159 | + fmt.Println(formattedText) |
| 160 | +} |
| 161 | + |
| 162 | + |
| 163 | +/* |
| 164 | + getConsoleWindowSize retrieves the console window size. |
| 165 | +*/ |
| 166 | +func getConsoleWindowSize() (width, height int) { |
| 167 | + kernel32 := syscall.NewLazyDLL("kernel32.dll") |
| 168 | + proc := kernel32.NewProc("GetConsoleScreenBufferInfo") |
| 169 | + handle, _, _ := kernel32.NewProc("GetStdHandle").Call(uintptr(uint32(uintptr(0xfffffff5)))) |
| 170 | + |
| 171 | + var info struct{ SizeX, SizeY int16 } |
| 172 | + |
| 173 | + ret, _, _ := proc.Call(handle, uintptr(unsafe.Pointer(&info))) |
| 174 | + if ret == 0 { |
| 175 | + return -1, -1 |
| 176 | + } |
| 177 | + // wow no wayyy.. |
| 178 | + return int(info.SizeX), int(info.SizeY) |
| 179 | +} |
| 180 | + |
| 181 | + |
| 182 | +/* |
| 183 | + Colorize applies color to text based on the provided color string. |
| 184 | +*/ |
| 185 | +func Colorize(text, color string) string { |
| 186 | + color = strings.ToLower(color) |
| 187 | + switch color { |
| 188 | + case "red": |
| 189 | + return Red + text + Reset |
| 190 | + case "green": |
| 191 | + return Green + text + Reset |
| 192 | + case "yellow": |
| 193 | + return Yellow + text + Reset |
| 194 | + case "blue": |
| 195 | + return Blue + text + Reset |
| 196 | + case "magenta": |
| 197 | + return Magenta + text + Reset |
| 198 | + case "cyan": |
| 199 | + return Cyan + text + Reset |
| 200 | + case "white": |
| 201 | + return White + text + Reset |
| 202 | + default: |
| 203 | + return text |
| 204 | + } |
| 205 | +} |
| 206 | +/* |
| 207 | + Write writes text to the console, optionally applying colors and centering it. |
| 208 | +*/ |
| 209 | +func Write(text string, colors []int, center bool) { |
| 210 | + if center { |
| 211 | + text = CenterText(text) |
| 212 | + } |
| 213 | + if len(colors) > 0 { |
| 214 | + text = GradientFade(text, colors) |
| 215 | + } |
| 216 | + lines := strings.Split(text, "\n") |
| 217 | + for _, line := range lines { |
| 218 | + fmt.Println(line) |
| 219 | + } |
| 220 | +} |
| 221 | +/* |
| 222 | + ClearConsole clears the console screen. |
| 223 | +*/ |
| 224 | + |
| 225 | +func ClearConsole() { |
| 226 | +cmd := exec.Command("cmd", "/c", "cls") |
| 227 | +cmd.Stdout = os.Stdout |
| 228 | +cmd.Run() |
| 229 | +} |
0 commit comments