colorable_windows.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. package colorable
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "math"
  7. "os"
  8. "strconv"
  9. "strings"
  10. "syscall"
  11. "unsafe"
  12. "github.com/mattn/go-isatty"
  13. )
  14. const (
  15. foregroundBlue = 0x1
  16. foregroundGreen = 0x2
  17. foregroundRed = 0x4
  18. foregroundIntensity = 0x8
  19. foregroundMask = (foregroundRed | foregroundBlue | foregroundGreen | foregroundIntensity)
  20. backgroundBlue = 0x10
  21. backgroundGreen = 0x20
  22. backgroundRed = 0x40
  23. backgroundIntensity = 0x80
  24. backgroundMask = (backgroundRed | backgroundBlue | backgroundGreen | backgroundIntensity)
  25. )
  26. type wchar uint16
  27. type short int16
  28. type dword uint32
  29. type word uint16
  30. type coord struct {
  31. x short
  32. y short
  33. }
  34. type smallRect struct {
  35. left short
  36. top short
  37. right short
  38. bottom short
  39. }
  40. type consoleScreenBufferInfo struct {
  41. size coord
  42. cursorPosition coord
  43. attributes word
  44. window smallRect
  45. maximumWindowSize coord
  46. }
  47. type consoleCursorInfo struct {
  48. size dword
  49. visible int32
  50. }
  51. var (
  52. kernel32 = syscall.NewLazyDLL("kernel32.dll")
  53. procGetConsoleScreenBufferInfo = kernel32.NewProc("GetConsoleScreenBufferInfo")
  54. procSetConsoleTextAttribute = kernel32.NewProc("SetConsoleTextAttribute")
  55. procSetConsoleCursorPosition = kernel32.NewProc("SetConsoleCursorPosition")
  56. procFillConsoleOutputCharacter = kernel32.NewProc("FillConsoleOutputCharacterW")
  57. procFillConsoleOutputAttribute = kernel32.NewProc("FillConsoleOutputAttribute")
  58. procGetConsoleCursorInfo = kernel32.NewProc("GetConsoleCursorInfo")
  59. procSetConsoleCursorInfo = kernel32.NewProc("SetConsoleCursorInfo")
  60. )
  61. type Writer struct {
  62. out io.Writer
  63. handle syscall.Handle
  64. lastbuf bytes.Buffer
  65. oldattr word
  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}
  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.NewBuffer(data)
  352. loop:
  353. for {
  354. r1, _, err := procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  355. if r1 == 0 {
  356. break loop
  357. }
  358. c1, _, err := er.ReadRune()
  359. if err != nil {
  360. break loop
  361. }
  362. if c1 != 0x1b {
  363. fmt.Fprint(w.out, string(c1))
  364. continue
  365. }
  366. c2, _, err := er.ReadRune()
  367. if err != nil {
  368. w.lastbuf.WriteRune(c1)
  369. break loop
  370. }
  371. if c2 != 0x5b {
  372. w.lastbuf.WriteRune(c1)
  373. w.lastbuf.WriteRune(c2)
  374. continue
  375. }
  376. var buf bytes.Buffer
  377. var m rune
  378. for {
  379. c, _, err := er.ReadRune()
  380. if err != nil {
  381. w.lastbuf.WriteRune(c1)
  382. w.lastbuf.WriteRune(c2)
  383. w.lastbuf.Write(buf.Bytes())
  384. break loop
  385. }
  386. if ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '@' {
  387. m = c
  388. break
  389. }
  390. buf.Write([]byte(string(c)))
  391. }
  392. var csbi consoleScreenBufferInfo
  393. switch m {
  394. case 'A':
  395. n, err = strconv.Atoi(buf.String())
  396. if err != nil {
  397. continue
  398. }
  399. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  400. csbi.cursorPosition.y -= short(n)
  401. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  402. case 'B':
  403. n, err = strconv.Atoi(buf.String())
  404. if err != nil {
  405. continue
  406. }
  407. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  408. csbi.cursorPosition.y += short(n)
  409. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  410. case 'C':
  411. n, err = strconv.Atoi(buf.String())
  412. if err != nil {
  413. continue
  414. }
  415. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  416. csbi.cursorPosition.x -= short(n)
  417. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  418. case 'D':
  419. n, err = strconv.Atoi(buf.String())
  420. if err != nil {
  421. continue
  422. }
  423. if n, err = strconv.Atoi(buf.String()); err == nil {
  424. var csbi consoleScreenBufferInfo
  425. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  426. csbi.cursorPosition.x += short(n)
  427. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  428. }
  429. case 'E':
  430. n, err = strconv.Atoi(buf.String())
  431. if err != nil {
  432. continue
  433. }
  434. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  435. csbi.cursorPosition.x = 0
  436. csbi.cursorPosition.y += short(n)
  437. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  438. case 'F':
  439. n, err = strconv.Atoi(buf.String())
  440. if err != nil {
  441. continue
  442. }
  443. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  444. csbi.cursorPosition.x = 0
  445. csbi.cursorPosition.y -= short(n)
  446. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  447. case 'G':
  448. n, err = strconv.Atoi(buf.String())
  449. if err != nil {
  450. continue
  451. }
  452. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  453. csbi.cursorPosition.x = short(n - 1)
  454. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  455. case 'H':
  456. token := strings.Split(buf.String(), ";")
  457. if len(token) != 2 {
  458. continue
  459. }
  460. n1, err := strconv.Atoi(token[0])
  461. if err != nil {
  462. continue
  463. }
  464. n2, err := strconv.Atoi(token[1])
  465. if err != nil {
  466. continue
  467. }
  468. csbi.cursorPosition.x = short(n2 - 1)
  469. csbi.cursorPosition.y = short(n1 - 1)
  470. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  471. case 'J':
  472. n, err := strconv.Atoi(buf.String())
  473. if err != nil {
  474. continue
  475. }
  476. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  477. var cursor coord
  478. switch n {
  479. case 0:
  480. cursor = coord{x: csbi.cursorPosition.x, y: csbi.cursorPosition.y}
  481. case 1:
  482. cursor = coord{x: csbi.window.left, y: csbi.window.top}
  483. case 2:
  484. cursor = coord{x: csbi.window.left, y: csbi.window.top}
  485. }
  486. var count, written dword
  487. count = dword(csbi.size.x - csbi.cursorPosition.x + (csbi.size.y-csbi.cursorPosition.y)*csbi.size.x)
  488. procFillConsoleOutputCharacter.Call(uintptr(w.handle), uintptr(' '), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  489. procFillConsoleOutputAttribute.Call(uintptr(w.handle), uintptr(csbi.attributes), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  490. case 'K':
  491. n, err := strconv.Atoi(buf.String())
  492. if err != nil {
  493. continue
  494. }
  495. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  496. var cursor coord
  497. switch n {
  498. case 0:
  499. cursor = coord{x: csbi.cursorPosition.x, y: csbi.cursorPosition.y}
  500. case 1:
  501. cursor = coord{x: csbi.window.left, y: csbi.window.top + csbi.cursorPosition.y}
  502. case 2:
  503. cursor = coord{x: csbi.window.left, y: csbi.window.top + csbi.cursorPosition.y}
  504. }
  505. var count, written dword
  506. count = dword(csbi.size.x - csbi.cursorPosition.x)
  507. procFillConsoleOutputCharacter.Call(uintptr(w.handle), uintptr(' '), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  508. procFillConsoleOutputAttribute.Call(uintptr(w.handle), uintptr(csbi.attributes), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  509. case 'm':
  510. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  511. attr := csbi.attributes
  512. cs := buf.String()
  513. if cs == "" {
  514. procSetConsoleTextAttribute.Call(uintptr(w.handle), uintptr(w.oldattr))
  515. continue
  516. }
  517. token := strings.Split(cs, ";")
  518. for i := 0; i < len(token); i++ {
  519. ns := token[i]
  520. if n, err = strconv.Atoi(ns); err == nil {
  521. switch {
  522. case n == 0 || n == 100:
  523. attr = w.oldattr
  524. case 1 <= n && n <= 5:
  525. attr |= foregroundIntensity
  526. case n == 7:
  527. attr = ((attr & foregroundMask) << 4) | ((attr & backgroundMask) >> 4)
  528. case 22 == n || n == 25 || n == 25:
  529. attr |= foregroundIntensity
  530. case n == 27:
  531. attr = ((attr & foregroundMask) << 4) | ((attr & backgroundMask) >> 4)
  532. case 30 <= n && n <= 37:
  533. attr &= backgroundMask
  534. if (n-30)&1 != 0 {
  535. attr |= foregroundRed
  536. }
  537. if (n-30)&2 != 0 {
  538. attr |= foregroundGreen
  539. }
  540. if (n-30)&4 != 0 {
  541. attr |= foregroundBlue
  542. }
  543. case n == 38: // set foreground color.
  544. if i < len(token)-2 && (token[i+1] == "5" || token[i+1] == "05") {
  545. if n256, err := strconv.Atoi(token[i+2]); err == nil {
  546. if n256foreAttr == nil {
  547. n256setup()
  548. }
  549. attr &= backgroundMask
  550. attr |= n256foreAttr[n256]
  551. i += 2
  552. }
  553. } else {
  554. attr = attr & (w.oldattr & backgroundMask)
  555. }
  556. case n == 39: // reset foreground color.
  557. attr &= backgroundMask
  558. attr |= w.oldattr & foregroundMask
  559. case 40 <= n && n <= 47:
  560. attr &= foregroundMask
  561. if (n-40)&1 != 0 {
  562. attr |= backgroundRed
  563. }
  564. if (n-40)&2 != 0 {
  565. attr |= backgroundGreen
  566. }
  567. if (n-40)&4 != 0 {
  568. attr |= backgroundBlue
  569. }
  570. case n == 48: // set background color.
  571. if i < len(token)-2 && token[i+1] == "5" {
  572. if n256, err := strconv.Atoi(token[i+2]); err == nil {
  573. if n256backAttr == nil {
  574. n256setup()
  575. }
  576. attr &= foregroundMask
  577. attr |= n256backAttr[n256]
  578. i += 2
  579. }
  580. } else {
  581. attr = attr & (w.oldattr & foregroundMask)
  582. }
  583. case n == 49: // reset foreground color.
  584. attr &= foregroundMask
  585. attr |= w.oldattr & backgroundMask
  586. case 90 <= n && n <= 97:
  587. attr = (attr & backgroundMask)
  588. attr |= foregroundIntensity
  589. if (n-90)&1 != 0 {
  590. attr |= foregroundRed
  591. }
  592. if (n-90)&2 != 0 {
  593. attr |= foregroundGreen
  594. }
  595. if (n-90)&4 != 0 {
  596. attr |= foregroundBlue
  597. }
  598. case 100 <= n && n <= 107:
  599. attr = (attr & foregroundMask)
  600. attr |= backgroundIntensity
  601. if (n-100)&1 != 0 {
  602. attr |= backgroundRed
  603. }
  604. if (n-100)&2 != 0 {
  605. attr |= backgroundGreen
  606. }
  607. if (n-100)&4 != 0 {
  608. attr |= backgroundBlue
  609. }
  610. }
  611. procSetConsoleTextAttribute.Call(uintptr(w.handle), uintptr(attr))
  612. }
  613. }
  614. case 'h':
  615. cs := buf.String()
  616. if cs == "?25" {
  617. var ci consoleCursorInfo
  618. procGetConsoleCursorInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&ci)))
  619. ci.visible = 1
  620. procSetConsoleCursorInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&ci)))
  621. }
  622. case 'l':
  623. cs := buf.String()
  624. if cs == "?25" {
  625. var ci consoleCursorInfo
  626. procGetConsoleCursorInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&ci)))
  627. ci.visible = 0
  628. procSetConsoleCursorInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&ci)))
  629. }
  630. }
  631. }
  632. return len(data) - w.lastbuf.Len(), nil
  633. }
  634. type consoleColor struct {
  635. rgb int
  636. red bool
  637. green bool
  638. blue bool
  639. intensity bool
  640. }
  641. func (c consoleColor) foregroundAttr() (attr word) {
  642. if c.red {
  643. attr |= foregroundRed
  644. }
  645. if c.green {
  646. attr |= foregroundGreen
  647. }
  648. if c.blue {
  649. attr |= foregroundBlue
  650. }
  651. if c.intensity {
  652. attr |= foregroundIntensity
  653. }
  654. return
  655. }
  656. func (c consoleColor) backgroundAttr() (attr word) {
  657. if c.red {
  658. attr |= backgroundRed
  659. }
  660. if c.green {
  661. attr |= backgroundGreen
  662. }
  663. if c.blue {
  664. attr |= backgroundBlue
  665. }
  666. if c.intensity {
  667. attr |= backgroundIntensity
  668. }
  669. return
  670. }
  671. var color16 = []consoleColor{
  672. consoleColor{0x000000, false, false, false, false},
  673. consoleColor{0x000080, false, false, true, false},
  674. consoleColor{0x008000, false, true, false, false},
  675. consoleColor{0x008080, false, true, true, false},
  676. consoleColor{0x800000, true, false, false, false},
  677. consoleColor{0x800080, true, false, true, false},
  678. consoleColor{0x808000, true, true, false, false},
  679. consoleColor{0xc0c0c0, true, true, true, false},
  680. consoleColor{0x808080, false, false, false, true},
  681. consoleColor{0x0000ff, false, false, true, true},
  682. consoleColor{0x00ff00, false, true, false, true},
  683. consoleColor{0x00ffff, false, true, true, true},
  684. consoleColor{0xff0000, true, false, false, true},
  685. consoleColor{0xff00ff, true, false, true, true},
  686. consoleColor{0xffff00, true, true, false, true},
  687. consoleColor{0xffffff, true, true, true, true},
  688. }
  689. type hsv struct {
  690. h, s, v float32
  691. }
  692. func (a hsv) dist(b hsv) float32 {
  693. dh := a.h - b.h
  694. switch {
  695. case dh > 0.5:
  696. dh = 1 - dh
  697. case dh < -0.5:
  698. dh = -1 - dh
  699. }
  700. ds := a.s - b.s
  701. dv := a.v - b.v
  702. return float32(math.Sqrt(float64(dh*dh + ds*ds + dv*dv)))
  703. }
  704. func toHSV(rgb int) hsv {
  705. r, g, b := float32((rgb&0xFF0000)>>16)/256.0,
  706. float32((rgb&0x00FF00)>>8)/256.0,
  707. float32(rgb&0x0000FF)/256.0
  708. min, max := minmax3f(r, g, b)
  709. h := max - min
  710. if h > 0 {
  711. if max == r {
  712. h = (g - b) / h
  713. if h < 0 {
  714. h += 6
  715. }
  716. } else if max == g {
  717. h = 2 + (b-r)/h
  718. } else {
  719. h = 4 + (r-g)/h
  720. }
  721. }
  722. h /= 6.0
  723. s := max - min
  724. if max != 0 {
  725. s /= max
  726. }
  727. v := max
  728. return hsv{h: h, s: s, v: v}
  729. }
  730. type hsvTable []hsv
  731. func toHSVTable(rgbTable []consoleColor) hsvTable {
  732. t := make(hsvTable, len(rgbTable))
  733. for i, c := range rgbTable {
  734. t[i] = toHSV(c.rgb)
  735. }
  736. return t
  737. }
  738. func (t hsvTable) find(rgb int) consoleColor {
  739. hsv := toHSV(rgb)
  740. n := 7
  741. l := float32(5.0)
  742. for i, p := range t {
  743. d := hsv.dist(p)
  744. if d < l {
  745. l, n = d, i
  746. }
  747. }
  748. return color16[n]
  749. }
  750. func minmax3f(a, b, c float32) (min, max float32) {
  751. if a < b {
  752. if b < c {
  753. return a, c
  754. } else if a < c {
  755. return a, b
  756. } else {
  757. return c, b
  758. }
  759. } else {
  760. if a < c {
  761. return b, c
  762. } else if b < c {
  763. return b, a
  764. } else {
  765. return c, a
  766. }
  767. }
  768. }
  769. var n256foreAttr []word
  770. var n256backAttr []word
  771. func n256setup() {
  772. n256foreAttr = make([]word, 256)
  773. n256backAttr = make([]word, 256)
  774. t := toHSVTable(color16)
  775. for i, rgb := range color256 {
  776. c := t.find(rgb)
  777. n256foreAttr[i] = c.foregroundAttr()
  778. n256backAttr[i] = c.backgroundAttr()
  779. }
  780. }