colorable_windows.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. package colorable
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "os"
  7. "strconv"
  8. "strings"
  9. "syscall"
  10. "unsafe"
  11. )
  12. const (
  13. foregroundBlue = 0x1
  14. foregroundGreen = 0x2
  15. foregroundRed = 0x4
  16. foregroundIntensity = 0x8
  17. foregroundMask = (foregroundRed | foregroundBlue | foregroundGreen | foregroundIntensity)
  18. backgroundBlue = 0x10
  19. backgroundGreen = 0x20
  20. backgroundRed = 0x40
  21. backgroundIntensity = 0x80
  22. backgroundMask = (backgroundRed | backgroundBlue | backgroundGreen | backgroundIntensity)
  23. )
  24. type wchar uint16
  25. type short int16
  26. type dword uint32
  27. type word uint16
  28. type coord struct {
  29. x short
  30. y short
  31. }
  32. type smallRect struct {
  33. left short
  34. top short
  35. right short
  36. bottom short
  37. }
  38. type consoleScreenBufferInfo struct {
  39. size coord
  40. cursorPosition coord
  41. attributes word
  42. window smallRect
  43. maximumWindowSize coord
  44. }
  45. var (
  46. kernel32 = syscall.NewLazyDLL("kernel32.dll")
  47. procGetConsoleScreenBufferInfo = kernel32.NewProc("GetConsoleScreenBufferInfo")
  48. procSetConsoleTextAttribute = kernel32.NewProc("SetConsoleTextAttribute")
  49. )
  50. type Writer struct {
  51. out syscall.Handle
  52. lastbuf bytes.Buffer
  53. oldattr word
  54. }
  55. func NewColorableStdout() io.Writer {
  56. var csbi consoleScreenBufferInfo
  57. out := syscall.Handle(os.Stdout.Fd())
  58. procGetConsoleScreenBufferInfo.Call(uintptr(out), uintptr(unsafe.Pointer(&csbi)))
  59. return &Writer{out: out, oldattr: csbi.attributes}
  60. }
  61. func NewColorableStderr() io.Writer {
  62. return &Writer{out: syscall.Handle(os.Stderr.Fd())}
  63. }
  64. var color256 = map[int]int{
  65. 0: 0x000000,
  66. 1: 0x800000,
  67. 2: 0x008000,
  68. 3: 0x808000,
  69. 4: 0x000080,
  70. 5: 0x800080,
  71. 6: 0x008080,
  72. 7: 0xc0c0c0,
  73. 8: 0x808080,
  74. 9: 0xff0000,
  75. 10: 0x00ff00,
  76. 11: 0xffff00,
  77. 12: 0x0000ff,
  78. 13: 0xff00ff,
  79. 14: 0x00ffff,
  80. 15: 0xffffff,
  81. 16: 0x000000,
  82. 17: 0x00005f,
  83. 18: 0x000087,
  84. 19: 0x0000af,
  85. 20: 0x0000d7,
  86. 21: 0x0000ff,
  87. 22: 0x005f00,
  88. 23: 0x005f5f,
  89. 24: 0x005f87,
  90. 25: 0x005faf,
  91. 26: 0x005fd7,
  92. 27: 0x005fff,
  93. 28: 0x008700,
  94. 29: 0x00875f,
  95. 30: 0x008787,
  96. 31: 0x0087af,
  97. 32: 0x0087d7,
  98. 33: 0x0087ff,
  99. 34: 0x00af00,
  100. 35: 0x00af5f,
  101. 36: 0x00af87,
  102. 37: 0x00afaf,
  103. 38: 0x00afd7,
  104. 39: 0x00afff,
  105. 40: 0x00d700,
  106. 41: 0x00d75f,
  107. 42: 0x00d787,
  108. 43: 0x00d7af,
  109. 44: 0x00d7d7,
  110. 45: 0x00d7ff,
  111. 46: 0x00ff00,
  112. 47: 0x00ff5f,
  113. 48: 0x00ff87,
  114. 49: 0x00ffaf,
  115. 50: 0x00ffd7,
  116. 51: 0x00ffff,
  117. 52: 0x5f0000,
  118. 53: 0x5f005f,
  119. 54: 0x5f0087,
  120. 55: 0x5f00af,
  121. 56: 0x5f00d7,
  122. 57: 0x5f00ff,
  123. 58: 0x5f5f00,
  124. 59: 0x5f5f5f,
  125. 60: 0x5f5f87,
  126. 61: 0x5f5faf,
  127. 62: 0x5f5fd7,
  128. 63: 0x5f5fff,
  129. 64: 0x5f8700,
  130. 65: 0x5f875f,
  131. 66: 0x5f8787,
  132. 67: 0x5f87af,
  133. 68: 0x5f87d7,
  134. 69: 0x5f87ff,
  135. 70: 0x5faf00,
  136. 71: 0x5faf5f,
  137. 72: 0x5faf87,
  138. 73: 0x5fafaf,
  139. 74: 0x5fafd7,
  140. 75: 0x5fafff,
  141. 76: 0x5fd700,
  142. 77: 0x5fd75f,
  143. 78: 0x5fd787,
  144. 79: 0x5fd7af,
  145. 80: 0x5fd7d7,
  146. 81: 0x5fd7ff,
  147. 82: 0x5fff00,
  148. 83: 0x5fff5f,
  149. 84: 0x5fff87,
  150. 85: 0x5fffaf,
  151. 86: 0x5fffd7,
  152. 87: 0x5fffff,
  153. 88: 0x870000,
  154. 89: 0x87005f,
  155. 90: 0x870087,
  156. 91: 0x8700af,
  157. 92: 0x8700d7,
  158. 93: 0x8700ff,
  159. 94: 0x875f00,
  160. 95: 0x875f5f,
  161. 96: 0x875f87,
  162. 97: 0x875faf,
  163. 98: 0x875fd7,
  164. 99: 0x875fff,
  165. 100: 0x878700,
  166. 101: 0x87875f,
  167. 102: 0x878787,
  168. 103: 0x8787af,
  169. 104: 0x8787d7,
  170. 105: 0x8787ff,
  171. 106: 0x87af00,
  172. 107: 0x87af5f,
  173. 108: 0x87af87,
  174. 109: 0x87afaf,
  175. 110: 0x87afd7,
  176. 111: 0x87afff,
  177. 112: 0x87d700,
  178. 113: 0x87d75f,
  179. 114: 0x87d787,
  180. 115: 0x87d7af,
  181. 116: 0x87d7d7,
  182. 117: 0x87d7ff,
  183. 118: 0x87ff00,
  184. 119: 0x87ff5f,
  185. 120: 0x87ff87,
  186. 121: 0x87ffaf,
  187. 122: 0x87ffd7,
  188. 123: 0x87ffff,
  189. 124: 0xaf0000,
  190. 125: 0xaf005f,
  191. 126: 0xaf0087,
  192. 127: 0xaf00af,
  193. 128: 0xaf00d7,
  194. 129: 0xaf00ff,
  195. 130: 0xaf5f00,
  196. 131: 0xaf5f5f,
  197. 132: 0xaf5f87,
  198. 133: 0xaf5faf,
  199. 134: 0xaf5fd7,
  200. 135: 0xaf5fff,
  201. 136: 0xaf8700,
  202. 137: 0xaf875f,
  203. 138: 0xaf8787,
  204. 139: 0xaf87af,
  205. 140: 0xaf87d7,
  206. 141: 0xaf87ff,
  207. 142: 0xafaf00,
  208. 143: 0xafaf5f,
  209. 144: 0xafaf87,
  210. 145: 0xafafaf,
  211. 146: 0xafafd7,
  212. 147: 0xafafff,
  213. 148: 0xafd700,
  214. 149: 0xafd75f,
  215. 150: 0xafd787,
  216. 151: 0xafd7af,
  217. 152: 0xafd7d7,
  218. 153: 0xafd7ff,
  219. 154: 0xafff00,
  220. 155: 0xafff5f,
  221. 156: 0xafff87,
  222. 157: 0xafffaf,
  223. 158: 0xafffd7,
  224. 159: 0xafffff,
  225. 160: 0xd70000,
  226. 161: 0xd7005f,
  227. 162: 0xd70087,
  228. 163: 0xd700af,
  229. 164: 0xd700d7,
  230. 165: 0xd700ff,
  231. 166: 0xd75f00,
  232. 167: 0xd75f5f,
  233. 168: 0xd75f87,
  234. 169: 0xd75faf,
  235. 170: 0xd75fd7,
  236. 171: 0xd75fff,
  237. 172: 0xd78700,
  238. 173: 0xd7875f,
  239. 174: 0xd78787,
  240. 175: 0xd787af,
  241. 176: 0xd787d7,
  242. 177: 0xd787ff,
  243. 178: 0xd7af00,
  244. 179: 0xd7af5f,
  245. 180: 0xd7af87,
  246. 181: 0xd7afaf,
  247. 182: 0xd7afd7,
  248. 183: 0xd7afff,
  249. 184: 0xd7d700,
  250. 185: 0xd7d75f,
  251. 186: 0xd7d787,
  252. 187: 0xd7d7af,
  253. 188: 0xd7d7d7,
  254. 189: 0xd7d7ff,
  255. 190: 0xd7ff00,
  256. 191: 0xd7ff5f,
  257. 192: 0xd7ff87,
  258. 193: 0xd7ffaf,
  259. 194: 0xd7ffd7,
  260. 195: 0xd7ffff,
  261. 196: 0xff0000,
  262. 197: 0xff005f,
  263. 198: 0xff0087,
  264. 199: 0xff00af,
  265. 200: 0xff00d7,
  266. 201: 0xff00ff,
  267. 202: 0xff5f00,
  268. 203: 0xff5f5f,
  269. 204: 0xff5f87,
  270. 205: 0xff5faf,
  271. 206: 0xff5fd7,
  272. 207: 0xff5fff,
  273. 208: 0xff8700,
  274. 209: 0xff875f,
  275. 210: 0xff8787,
  276. 211: 0xff87af,
  277. 212: 0xff87d7,
  278. 213: 0xff87ff,
  279. 214: 0xffaf00,
  280. 215: 0xffaf5f,
  281. 216: 0xffaf87,
  282. 217: 0xffafaf,
  283. 218: 0xffafd7,
  284. 219: 0xffafff,
  285. 220: 0xffd700,
  286. 221: 0xffd75f,
  287. 222: 0xffd787,
  288. 223: 0xffd7af,
  289. 224: 0xffd7d7,
  290. 225: 0xffd7ff,
  291. 226: 0xffff00,
  292. 227: 0xffff5f,
  293. 228: 0xffff87,
  294. 229: 0xffffaf,
  295. 230: 0xffffd7,
  296. 231: 0xffffff,
  297. 232: 0x080808,
  298. 233: 0x121212,
  299. 234: 0x1c1c1c,
  300. 235: 0x262626,
  301. 236: 0x303030,
  302. 237: 0x3a3a3a,
  303. 238: 0x444444,
  304. 239: 0x4e4e4e,
  305. 240: 0x585858,
  306. 241: 0x626262,
  307. 242: 0x6c6c6c,
  308. 243: 0x767676,
  309. 244: 0x808080,
  310. 245: 0x8a8a8a,
  311. 246: 0x949494,
  312. 247: 0x9e9e9e,
  313. 248: 0xa8a8a8,
  314. 249: 0xb2b2b2,
  315. 250: 0xbcbcbc,
  316. 251: 0xc6c6c6,
  317. 252: 0xd0d0d0,
  318. 253: 0xdadada,
  319. 254: 0xe4e4e4,
  320. 255: 0xeeeeee,
  321. }
  322. func (w *Writer) Write(data []byte) (n int, err error) {
  323. var csbi consoleScreenBufferInfo
  324. procGetConsoleScreenBufferInfo.Call(uintptr(w.out), uintptr(unsafe.Pointer(&csbi)))
  325. er := bytes.NewBuffer(data)
  326. loop:
  327. for {
  328. r1, _, err := procGetConsoleScreenBufferInfo.Call(uintptr(w.out), uintptr(unsafe.Pointer(&csbi)))
  329. if r1 == 0 {
  330. break loop
  331. }
  332. c1, _, err := er.ReadRune()
  333. if err != nil {
  334. break loop
  335. }
  336. if c1 != 0x1b {
  337. fmt.Print(string(c1))
  338. continue
  339. }
  340. c2, _, err := er.ReadRune()
  341. if err != nil {
  342. w.lastbuf.WriteRune(c1)
  343. break loop
  344. }
  345. if c2 != 0x5b {
  346. w.lastbuf.WriteRune(c1)
  347. w.lastbuf.WriteRune(c2)
  348. continue
  349. }
  350. var buf bytes.Buffer
  351. var m rune
  352. for {
  353. c, _, err := er.ReadRune()
  354. if err != nil {
  355. w.lastbuf.WriteRune(c1)
  356. w.lastbuf.WriteRune(c2)
  357. w.lastbuf.Write(buf.Bytes())
  358. break loop
  359. }
  360. if ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '@' {
  361. m = c
  362. break
  363. }
  364. buf.Write([]byte(string(c)))
  365. }
  366. switch m {
  367. case 'm':
  368. attr := csbi.attributes
  369. cs := buf.String()
  370. if cs == "" {
  371. procSetConsoleTextAttribute.Call(uintptr(w.out), uintptr(w.oldattr))
  372. continue
  373. }
  374. token := strings.Split(cs, ";")
  375. for i, ns := range token {
  376. if n, err = strconv.Atoi(ns); err == nil {
  377. switch {
  378. case n == 0 || n == 100:
  379. attr = w.oldattr
  380. case 1 <= n && n <= 5:
  381. attr |= foregroundIntensity
  382. case n == 7:
  383. attr = ((attr & foregroundMask) << 4) | ((attr & backgroundMask) >> 4)
  384. case 22 == n || n == 25 || n == 25:
  385. attr |= foregroundIntensity
  386. case n == 27:
  387. attr = ((attr & foregroundMask) << 4) | ((attr & backgroundMask) >> 4)
  388. case 30 <= n && n <= 37:
  389. attr = (attr & backgroundMask)
  390. if (n-30)&1 != 0 {
  391. attr |= foregroundRed
  392. }
  393. if (n-30)&2 != 0 {
  394. attr |= foregroundGreen
  395. }
  396. if (n-30)&4 != 0 {
  397. attr |= foregroundBlue
  398. }
  399. case n == 38:
  400. if i < len(token)-2 && token[i+1] == "5" {
  401. if n256, err := strconv.Atoi(token[i+2]); err == nil {
  402. attr = (attr & backgroundMask)
  403. rgb := color256[n256]
  404. r, g, b := (rgb & 0xFF0000) >> 16, (rgb & 0x00FF00) >> 8, rgb & 0x0000FF
  405. if r > 127 {
  406. attr |= foregroundRed
  407. }
  408. if g > 127 {
  409. attr |= foregroundGreen
  410. }
  411. if b > 127 {
  412. attr |= foregroundBlue
  413. }
  414. i += 2
  415. }
  416. }
  417. case n == 39:
  418. if i < len(token)-2 && token[i+1] == "5" {
  419. if n256, err := strconv.Atoi(token[i+2]); err == nil {
  420. attr = (attr & foregroundMask)
  421. rgb := color256[n256]
  422. r, g, b := (rgb & 0xFF0000) >> 16, (rgb & 0x00FF00) >> 8, rgb & 0x0000FF
  423. if r > 127 {
  424. attr |= backgroundRed
  425. }
  426. if g > 127 {
  427. attr |= backgroundGreen
  428. }
  429. if b > 127 {
  430. attr |= backgroundBlue
  431. }
  432. i += 2
  433. }
  434. }
  435. case 40 <= n && n <= 47:
  436. attr = (attr & foregroundMask)
  437. if (n-40)&1 != 0 {
  438. attr |= backgroundRed
  439. }
  440. if (n-40)&2 != 0 {
  441. attr |= backgroundGreen
  442. }
  443. if (n-40)&4 != 0 {
  444. attr |= backgroundBlue
  445. }
  446. }
  447. procSetConsoleTextAttribute.Call(uintptr(w.out), uintptr(attr))
  448. }
  449. }
  450. }
  451. }
  452. return len(data) - w.lastbuf.Len(), nil
  453. }