colorable_windows.go 11 KB

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