You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

821 lines
19 KiB

  1. package colorable
  2. import (
  3. "bytes"
  4. "io"
  5. "math"
  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. type consoleCursorInfo struct {
  47. size dword
  48. visible int32
  49. }
  50. var (
  51. kernel32 = syscall.NewLazyDLL("kernel32.dll")
  52. procGetConsoleScreenBufferInfo = kernel32.NewProc("GetConsoleScreenBufferInfo")
  53. procSetConsoleTextAttribute = kernel32.NewProc("SetConsoleTextAttribute")
  54. procSetConsoleCursorPosition = kernel32.NewProc("SetConsoleCursorPosition")
  55. procFillConsoleOutputCharacter = kernel32.NewProc("FillConsoleOutputCharacterW")
  56. procFillConsoleOutputAttribute = kernel32.NewProc("FillConsoleOutputAttribute")
  57. procGetConsoleCursorInfo = kernel32.NewProc("GetConsoleCursorInfo")
  58. procSetConsoleCursorInfo = kernel32.NewProc("SetConsoleCursorInfo")
  59. )
  60. type Writer struct {
  61. out io.Writer
  62. handle syscall.Handle
  63. lastbuf bytes.Buffer
  64. oldattr word
  65. oldpos coord
  66. }
  67. // NewColorable return new instance of Writer which handle escape sequence from File.
  68. func NewColorable(file *os.File) io.Writer {
  69. if file == nil {
  70. panic("nil passed instead of *os.File to NewColorable()")
  71. }
  72. if isatty.IsTerminal(file.Fd()) {
  73. var csbi consoleScreenBufferInfo
  74. handle := syscall.Handle(file.Fd())
  75. procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))
  76. return &Writer{out: file, handle: handle, oldattr: csbi.attributes, oldpos: coord{0, 0}}
  77. } else {
  78. return file
  79. }
  80. }
  81. // NewColorableStdout return new instance of Writer which handle escape sequence for stdout.
  82. func NewColorableStdout() io.Writer {
  83. return NewColorable(os.Stdout)
  84. }
  85. // NewColorableStderr return new instance of Writer which handle escape sequence for stderr.
  86. func NewColorableStderr() io.Writer {
  87. return NewColorable(os.Stderr)
  88. }
  89. var color256 = map[int]int{
  90. 0: 0x000000,
  91. 1: 0x800000,
  92. 2: 0x008000,
  93. 3: 0x808000,
  94. 4: 0x000080,
  95. 5: 0x800080,
  96. 6: 0x008080,
  97. 7: 0xc0c0c0,
  98. 8: 0x808080,
  99. 9: 0xff0000,
  100. 10: 0x00ff00,
  101. 11: 0xffff00,
  102. 12: 0x0000ff,
  103. 13: 0xff00ff,
  104. 14: 0x00ffff,
  105. 15: 0xffffff,
  106. 16: 0x000000,
  107. 17: 0x00005f,
  108. 18: 0x000087,
  109. 19: 0x0000af,
  110. 20: 0x0000d7,
  111. 21: 0x0000ff,
  112. 22: 0x005f00,
  113. 23: 0x005f5f,
  114. 24: 0x005f87,
  115. 25: 0x005faf,
  116. 26: 0x005fd7,
  117. 27: 0x005fff,
  118. 28: 0x008700,
  119. 29: 0x00875f,
  120. 30: 0x008787,
  121. 31: 0x0087af,
  122. 32: 0x0087d7,
  123. 33: 0x0087ff,
  124. 34: 0x00af00,
  125. 35: 0x00af5f,
  126. 36: 0x00af87,
  127. 37: 0x00afaf,
  128. 38: 0x00afd7,
  129. 39: 0x00afff,
  130. 40: 0x00d700,
  131. 41: 0x00d75f,
  132. 42: 0x00d787,
  133. 43: 0x00d7af,
  134. 44: 0x00d7d7,
  135. 45: 0x00d7ff,
  136. 46: 0x00ff00,
  137. 47: 0x00ff5f,
  138. 48: 0x00ff87,
  139. 49: 0x00ffaf,
  140. 50: 0x00ffd7,
  141. 51: 0x00ffff,
  142. 52: 0x5f0000,
  143. 53: 0x5f005f,
  144. 54: 0x5f0087,
  145. 55: 0x5f00af,
  146. 56: 0x5f00d7,
  147. 57: 0x5f00ff,
  148. 58: 0x5f5f00,
  149. 59: 0x5f5f5f,
  150. 60: 0x5f5f87,
  151. 61: 0x5f5faf,
  152. 62: 0x5f5fd7,
  153. 63: 0x5f5fff,
  154. 64: 0x5f8700,
  155. 65: 0x5f875f,
  156. 66: 0x5f8787,
  157. 67: 0x5f87af,
  158. 68: 0x5f87d7,
  159. 69: 0x5f87ff,
  160. 70: 0x5faf00,
  161. 71: 0x5faf5f,
  162. 72: 0x5faf87,
  163. 73: 0x5fafaf,
  164. 74: 0x5fafd7,
  165. 75: 0x5fafff,
  166. 76: 0x5fd700,
  167. 77: 0x5fd75f,
  168. 78: 0x5fd787,
  169. 79: 0x5fd7af,
  170. 80: 0x5fd7d7,
  171. 81: 0x5fd7ff,
  172. 82: 0x5fff00,
  173. 83: 0x5fff5f,
  174. 84: 0x5fff87,
  175. 85: 0x5fffaf,
  176. 86: 0x5fffd7,
  177. 87: 0x5fffff,
  178. 88: 0x870000,
  179. 89: 0x87005f,
  180. 90: 0x870087,
  181. 91: 0x8700af,
  182. 92: 0x8700d7,
  183. 93: 0x8700ff,
  184. 94: 0x875f00,
  185. 95: 0x875f5f,
  186. 96: 0x875f87,
  187. 97: 0x875faf,
  188. 98: 0x875fd7,
  189. 99: 0x875fff,
  190. 100: 0x878700,
  191. 101: 0x87875f,
  192. 102: 0x878787,
  193. 103: 0x8787af,
  194. 104: 0x8787d7,
  195. 105: 0x8787ff,
  196. 106: 0x87af00,
  197. 107: 0x87af5f,
  198. 108: 0x87af87,
  199. 109: 0x87afaf,
  200. 110: 0x87afd7,
  201. 111: 0x87afff,
  202. 112: 0x87d700,
  203. 113: 0x87d75f,
  204. 114: 0x87d787,
  205. 115: 0x87d7af,
  206. 116: 0x87d7d7,
  207. 117: 0x87d7ff,
  208. 118: 0x87ff00,
  209. 119: 0x87ff5f,
  210. 120: 0x87ff87,
  211. 121: 0x87ffaf,
  212. 122: 0x87ffd7,
  213. 123: 0x87ffff,
  214. 124: 0xaf0000,
  215. 125: 0xaf005f,
  216. 126: 0xaf0087,
  217. 127: 0xaf00af,
  218. 128: 0xaf00d7,
  219. 129: 0xaf00ff,
  220. 130: 0xaf5f00,
  221. 131: 0xaf5f5f,
  222. 132: 0xaf5f87,
  223. 133: 0xaf5faf,
  224. 134: 0xaf5fd7,
  225. 135: 0xaf5fff,
  226. 136: 0xaf8700,
  227. 137: 0xaf875f,
  228. 138: 0xaf8787,
  229. 139: 0xaf87af,
  230. 140: 0xaf87d7,
  231. 141: 0xaf87ff,
  232. 142: 0xafaf00,
  233. 143: 0xafaf5f,
  234. 144: 0xafaf87,
  235. 145: 0xafafaf,
  236. 146: 0xafafd7,
  237. 147: 0xafafff,
  238. 148: 0xafd700,
  239. 149: 0xafd75f,
  240. 150: 0xafd787,
  241. 151: 0xafd7af,
  242. 152: 0xafd7d7,
  243. 153: 0xafd7ff,
  244. 154: 0xafff00,
  245. 155: 0xafff5f,
  246. 156: 0xafff87,
  247. 157: 0xafffaf,
  248. 158: 0xafffd7,
  249. 159: 0xafffff,
  250. 160: 0xd70000,
  251. 161: 0xd7005f,
  252. 162: 0xd70087,
  253. 163: 0xd700af,
  254. 164: 0xd700d7,
  255. 165: 0xd700ff,
  256. 166: 0xd75f00,
  257. 167: 0xd75f5f,
  258. 168: 0xd75f87,
  259. 169: 0xd75faf,
  260. 170: 0xd75fd7,
  261. 171: 0xd75fff,
  262. 172: 0xd78700,
  263. 173: 0xd7875f,
  264. 174: 0xd78787,
  265. 175: 0xd787af,
  266. 176: 0xd787d7,
  267. 177: 0xd787ff,
  268. 178: 0xd7af00,
  269. 179: 0xd7af5f,
  270. 180: 0xd7af87,
  271. 181: 0xd7afaf,
  272. 182: 0xd7afd7,
  273. 183: 0xd7afff,
  274. 184: 0xd7d700,
  275. 185: 0xd7d75f,
  276. 186: 0xd7d787,
  277. 187: 0xd7d7af,
  278. 188: 0xd7d7d7,
  279. 189: 0xd7d7ff,
  280. 190: 0xd7ff00,
  281. 191: 0xd7ff5f,
  282. 192: 0xd7ff87,
  283. 193: 0xd7ffaf,
  284. 194: 0xd7ffd7,
  285. 195: 0xd7ffff,
  286. 196: 0xff0000,
  287. 197: 0xff005f,
  288. 198: 0xff0087,
  289. 199: 0xff00af,
  290. 200: 0xff00d7,
  291. 201: 0xff00ff,
  292. 202: 0xff5f00,
  293. 203: 0xff5f5f,
  294. 204: 0xff5f87,
  295. 205: 0xff5faf,
  296. 206: 0xff5fd7,
  297. 207: 0xff5fff,
  298. 208: 0xff8700,
  299. 209: 0xff875f,
  300. 210: 0xff8787,
  301. 211: 0xff87af,
  302. 212: 0xff87d7,
  303. 213: 0xff87ff,
  304. 214: 0xffaf00,
  305. 215: 0xffaf5f,
  306. 216: 0xffaf87,
  307. 217: 0xffafaf,
  308. 218: 0xffafd7,
  309. 219: 0xffafff,
  310. 220: 0xffd700,
  311. 221: 0xffd75f,
  312. 222: 0xffd787,
  313. 223: 0xffd7af,
  314. 224: 0xffd7d7,
  315. 225: 0xffd7ff,
  316. 226: 0xffff00,
  317. 227: 0xffff5f,
  318. 228: 0xffff87,
  319. 229: 0xffffaf,
  320. 230: 0xffffd7,
  321. 231: 0xffffff,
  322. 232: 0x080808,
  323. 233: 0x121212,
  324. 234: 0x1c1c1c,
  325. 235: 0x262626,
  326. 236: 0x303030,
  327. 237: 0x3a3a3a,
  328. 238: 0x444444,
  329. 239: 0x4e4e4e,
  330. 240: 0x585858,
  331. 241: 0x626262,
  332. 242: 0x6c6c6c,
  333. 243: 0x767676,
  334. 244: 0x808080,
  335. 245: 0x8a8a8a,
  336. 246: 0x949494,
  337. 247: 0x9e9e9e,
  338. 248: 0xa8a8a8,
  339. 249: 0xb2b2b2,
  340. 250: 0xbcbcbc,
  341. 251: 0xc6c6c6,
  342. 252: 0xd0d0d0,
  343. 253: 0xdadada,
  344. 254: 0xe4e4e4,
  345. 255: 0xeeeeee,
  346. }
  347. // Write write data on console
  348. func (w *Writer) Write(data []byte) (n int, err error) {
  349. var csbi consoleScreenBufferInfo
  350. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  351. er := bytes.NewReader(data)
  352. var bw [1]byte
  353. loop:
  354. for {
  355. r1, _, err := procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  356. if r1 == 0 {
  357. break loop
  358. }
  359. c1, err := er.ReadByte()
  360. if err != nil {
  361. break loop
  362. }
  363. if c1 != 0x1b {
  364. bw[0] = c1
  365. w.out.Write(bw[:])
  366. continue
  367. }
  368. c2, err := er.ReadByte()
  369. if err != nil {
  370. w.lastbuf.WriteByte(c1)
  371. break loop
  372. }
  373. if c2 != 0x5b {
  374. w.lastbuf.WriteByte(c1)
  375. w.lastbuf.WriteByte(c2)
  376. continue
  377. }
  378. var buf bytes.Buffer
  379. var m byte
  380. for {
  381. c, err := er.ReadByte()
  382. if err != nil {
  383. w.lastbuf.WriteByte(c1)
  384. w.lastbuf.WriteByte(c2)
  385. w.lastbuf.Write(buf.Bytes())
  386. break loop
  387. }
  388. if ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '@' {
  389. m = c
  390. break
  391. }
  392. buf.Write([]byte(string(c)))
  393. }
  394. var csbi consoleScreenBufferInfo
  395. switch m {
  396. case 'A':
  397. n, err = strconv.Atoi(buf.String())
  398. if err != nil {
  399. continue
  400. }
  401. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  402. csbi.cursorPosition.y -= short(n)
  403. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  404. case 'B':
  405. n, err = strconv.Atoi(buf.String())
  406. if err != nil {
  407. continue
  408. }
  409. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  410. csbi.cursorPosition.y += short(n)
  411. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  412. case 'C':
  413. n, err = strconv.Atoi(buf.String())
  414. if err != nil {
  415. continue
  416. }
  417. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  418. csbi.cursorPosition.x -= short(n)
  419. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  420. case 'D':
  421. n, err = strconv.Atoi(buf.String())
  422. if err != nil {
  423. continue
  424. }
  425. if n, err = strconv.Atoi(buf.String()); err == nil {
  426. var csbi consoleScreenBufferInfo
  427. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  428. csbi.cursorPosition.x += short(n)
  429. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  430. }
  431. case 'E':
  432. n, err = strconv.Atoi(buf.String())
  433. if err != nil {
  434. continue
  435. }
  436. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  437. csbi.cursorPosition.x = 0
  438. csbi.cursorPosition.y += short(n)
  439. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  440. case 'F':
  441. n, err = strconv.Atoi(buf.String())
  442. if err != nil {
  443. continue
  444. }
  445. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  446. csbi.cursorPosition.x = 0
  447. csbi.cursorPosition.y -= short(n)
  448. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  449. case 'G':
  450. n, err = strconv.Atoi(buf.String())
  451. if err != nil {
  452. continue
  453. }
  454. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  455. csbi.cursorPosition.x = short(n - 1)
  456. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  457. case 'H':
  458. token := strings.Split(buf.String(), ";")
  459. if len(token) != 2 {
  460. continue
  461. }
  462. n1, err := strconv.Atoi(token[0])
  463. if err != nil {
  464. continue
  465. }
  466. n2, err := strconv.Atoi(token[1])
  467. if err != nil {
  468. continue
  469. }
  470. csbi.cursorPosition.x = short(n2 - 1)
  471. csbi.cursorPosition.y = short(n1 - 1)
  472. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  473. case 'J':
  474. n, err := strconv.Atoi(buf.String())
  475. if err != nil {
  476. continue
  477. }
  478. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  479. var cursor coord
  480. switch n {
  481. case 0:
  482. cursor = coord{x: csbi.cursorPosition.x, y: csbi.cursorPosition.y}
  483. case 1:
  484. cursor = coord{x: csbi.window.left, y: csbi.window.top}
  485. case 2:
  486. cursor = coord{x: csbi.window.left, y: csbi.window.top}
  487. }
  488. var count, written dword
  489. count = dword(csbi.size.x - csbi.cursorPosition.x + (csbi.size.y-csbi.cursorPosition.y)*csbi.size.x)
  490. procFillConsoleOutputCharacter.Call(uintptr(w.handle), uintptr(' '), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  491. procFillConsoleOutputAttribute.Call(uintptr(w.handle), uintptr(csbi.attributes), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  492. case 'K':
  493. n, err := strconv.Atoi(buf.String())
  494. if err != nil {
  495. continue
  496. }
  497. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  498. var cursor coord
  499. switch n {
  500. case 0:
  501. cursor = coord{x: csbi.cursorPosition.x, y: csbi.cursorPosition.y}
  502. case 1:
  503. cursor = coord{x: csbi.window.left, y: csbi.window.top + csbi.cursorPosition.y}
  504. case 2:
  505. cursor = coord{x: csbi.window.left, y: csbi.window.top + csbi.cursorPosition.y}
  506. }
  507. var count, written dword
  508. count = dword(csbi.size.x - csbi.cursorPosition.x)
  509. procFillConsoleOutputCharacter.Call(uintptr(w.handle), uintptr(' '), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  510. procFillConsoleOutputAttribute.Call(uintptr(w.handle), uintptr(csbi.attributes), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  511. case 'm':
  512. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  513. attr := csbi.attributes
  514. cs := buf.String()
  515. if cs == "" {
  516. procSetConsoleTextAttribute.Call(uintptr(w.handle), uintptr(w.oldattr))
  517. continue
  518. }
  519. token := strings.Split(cs, ";")
  520. for i := 0; i < len(token); i++ {
  521. ns := token[i]
  522. if n, err = strconv.Atoi(ns); err == nil {
  523. switch {
  524. case n == 0 || n == 100:
  525. attr = w.oldattr
  526. case 1 <= n && n <= 5:
  527. attr |= foregroundIntensity
  528. case n == 7:
  529. attr = ((attr & foregroundMask) << 4) | ((attr & backgroundMask) >> 4)
  530. case 22 == n || n == 25 || n == 25:
  531. attr |= foregroundIntensity
  532. case n == 27:
  533. attr = ((attr & foregroundMask) << 4) | ((attr & backgroundMask) >> 4)
  534. case 30 <= n && n <= 37:
  535. attr &= backgroundMask
  536. if (n-30)&1 != 0 {
  537. attr |= foregroundRed
  538. }
  539. if (n-30)&2 != 0 {
  540. attr |= foregroundGreen
  541. }
  542. if (n-30)&4 != 0 {
  543. attr |= foregroundBlue
  544. }
  545. case n == 38: // set foreground color.
  546. if i < len(token)-2 && (token[i+1] == "5" || token[i+1] == "05") {
  547. if n256, err := strconv.Atoi(token[i+2]); err == nil {
  548. if n256foreAttr == nil {
  549. n256setup()
  550. }
  551. attr &= backgroundMask
  552. attr |= n256foreAttr[n256]
  553. i += 2
  554. }
  555. } else {
  556. attr = attr & (w.oldattr & backgroundMask)
  557. }
  558. case n == 39: // reset foreground color.
  559. attr &= backgroundMask
  560. attr |= w.oldattr & foregroundMask
  561. case 40 <= n && n <= 47:
  562. attr &= foregroundMask
  563. if (n-40)&1 != 0 {
  564. attr |= backgroundRed
  565. }
  566. if (n-40)&2 != 0 {
  567. attr |= backgroundGreen
  568. }
  569. if (n-40)&4 != 0 {
  570. attr |= backgroundBlue
  571. }
  572. case n == 48: // set background color.
  573. if i < len(token)-2 && token[i+1] == "5" {
  574. if n256, err := strconv.Atoi(token[i+2]); err == nil {
  575. if n256backAttr == nil {
  576. n256setup()
  577. }
  578. attr &= foregroundMask
  579. attr |= n256backAttr[n256]
  580. i += 2
  581. }
  582. } else {
  583. attr = attr & (w.oldattr & foregroundMask)
  584. }
  585. case n == 49: // reset foreground color.
  586. attr &= foregroundMask
  587. attr |= w.oldattr & backgroundMask
  588. case 90 <= n && n <= 97:
  589. attr = (attr & backgroundMask)
  590. attr |= foregroundIntensity
  591. if (n-90)&1 != 0 {
  592. attr |= foregroundRed
  593. }
  594. if (n-90)&2 != 0 {
  595. attr |= foregroundGreen
  596. }
  597. if (n-90)&4 != 0 {
  598. attr |= foregroundBlue
  599. }
  600. case 100 <= n && n <= 107:
  601. attr = (attr & foregroundMask)
  602. attr |= backgroundIntensity
  603. if (n-100)&1 != 0 {
  604. attr |= backgroundRed
  605. }
  606. if (n-100)&2 != 0 {
  607. attr |= backgroundGreen
  608. }
  609. if (n-100)&4 != 0 {
  610. attr |= backgroundBlue
  611. }
  612. }
  613. procSetConsoleTextAttribute.Call(uintptr(w.handle), uintptr(attr))
  614. }
  615. }
  616. case 'h':
  617. cs := buf.String()
  618. if cs == "?25" {
  619. var ci consoleCursorInfo
  620. procGetConsoleCursorInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&ci)))
  621. ci.visible = 1
  622. procSetConsoleCursorInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&ci)))
  623. }
  624. case 'l':
  625. cs := buf.String()
  626. if cs == "?25" {
  627. var ci consoleCursorInfo
  628. procGetConsoleCursorInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&ci)))
  629. ci.visible = 0
  630. procSetConsoleCursorInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&ci)))
  631. }
  632. case 's':
  633. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  634. w.oldpos = csbi.cursorPosition
  635. case 'u':
  636. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&w.oldpos)))
  637. }
  638. }
  639. return len(data) - w.lastbuf.Len(), nil
  640. }
  641. type consoleColor struct {
  642. rgb int
  643. red bool
  644. green bool
  645. blue bool
  646. intensity bool
  647. }
  648. func (c consoleColor) foregroundAttr() (attr word) {
  649. if c.red {
  650. attr |= foregroundRed
  651. }
  652. if c.green {
  653. attr |= foregroundGreen
  654. }
  655. if c.blue {
  656. attr |= foregroundBlue
  657. }
  658. if c.intensity {
  659. attr |= foregroundIntensity
  660. }
  661. return
  662. }
  663. func (c consoleColor) backgroundAttr() (attr word) {
  664. if c.red {
  665. attr |= backgroundRed
  666. }
  667. if c.green {
  668. attr |= backgroundGreen
  669. }
  670. if c.blue {
  671. attr |= backgroundBlue
  672. }
  673. if c.intensity {
  674. attr |= backgroundIntensity
  675. }
  676. return
  677. }
  678. var color16 = []consoleColor{
  679. consoleColor{0x000000, false, false, false, false},
  680. consoleColor{0x000080, false, false, true, false},
  681. consoleColor{0x008000, false, true, false, false},
  682. consoleColor{0x008080, false, true, true, false},
  683. consoleColor{0x800000, true, false, false, false},
  684. consoleColor{0x800080, true, false, true, false},
  685. consoleColor{0x808000, true, true, false, false},
  686. consoleColor{0xc0c0c0, true, true, true, false},
  687. consoleColor{0x808080, false, false, false, true},
  688. consoleColor{0x0000ff, false, false, true, true},
  689. consoleColor{0x00ff00, false, true, false, true},
  690. consoleColor{0x00ffff, false, true, true, true},
  691. consoleColor{0xff0000, true, false, false, true},
  692. consoleColor{0xff00ff, true, false, true, true},
  693. consoleColor{0xffff00, true, true, false, true},
  694. consoleColor{0xffffff, true, true, true, true},
  695. }
  696. type hsv struct {
  697. h, s, v float32
  698. }
  699. func (a hsv) dist(b hsv) float32 {
  700. dh := a.h - b.h
  701. switch {
  702. case dh > 0.5:
  703. dh = 1 - dh
  704. case dh < -0.5:
  705. dh = -1 - dh
  706. }
  707. ds := a.s - b.s
  708. dv := a.v - b.v
  709. return float32(math.Sqrt(float64(dh*dh + ds*ds + dv*dv)))
  710. }
  711. func toHSV(rgb int) hsv {
  712. r, g, b := float32((rgb&0xFF0000)>>16)/256.0,
  713. float32((rgb&0x00FF00)>>8)/256.0,
  714. float32(rgb&0x0000FF)/256.0
  715. min, max := minmax3f(r, g, b)
  716. h := max - min
  717. if h > 0 {
  718. if max == r {
  719. h = (g - b) / h
  720. if h < 0 {
  721. h += 6
  722. }
  723. } else if max == g {
  724. h = 2 + (b-r)/h
  725. } else {
  726. h = 4 + (r-g)/h
  727. }
  728. }
  729. h /= 6.0
  730. s := max - min
  731. if max != 0 {
  732. s /= max
  733. }
  734. v := max
  735. return hsv{h: h, s: s, v: v}
  736. }
  737. type hsvTable []hsv
  738. func toHSVTable(rgbTable []consoleColor) hsvTable {
  739. t := make(hsvTable, len(rgbTable))
  740. for i, c := range rgbTable {
  741. t[i] = toHSV(c.rgb)
  742. }
  743. return t
  744. }
  745. func (t hsvTable) find(rgb int) consoleColor {
  746. hsv := toHSV(rgb)
  747. n := 7
  748. l := float32(5.0)
  749. for i, p := range t {
  750. d := hsv.dist(p)
  751. if d < l {
  752. l, n = d, i
  753. }
  754. }
  755. return color16[n]
  756. }
  757. func minmax3f(a, b, c float32) (min, max float32) {
  758. if a < b {
  759. if b < c {
  760. return a, c
  761. } else if a < c {
  762. return a, b
  763. } else {
  764. return c, b
  765. }
  766. } else {
  767. if a < c {
  768. return b, c
  769. } else if b < c {
  770. return b, a
  771. } else {
  772. return c, a
  773. }
  774. }
  775. }
  776. var n256foreAttr []word
  777. var n256backAttr []word
  778. func n256setup() {
  779. n256foreAttr = make([]word, 256)
  780. n256backAttr = make([]word, 256)
  781. t := toHSVTable(color16)
  782. for i, rgb := range color256 {
  783. c := t.find(rgb)
  784. n256foreAttr[i] = c.foregroundAttr()
  785. n256backAttr[i] = c.backgroundAttr()
  786. }
  787. }