colorable_windows.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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. // Writer provide colorable Writer to the console
  61. type Writer struct {
  62. out io.Writer
  63. handle syscall.Handle
  64. lastbuf bytes.Buffer
  65. oldattr word
  66. oldpos coord
  67. }
  68. // NewColorable return new instance of Writer which handle escape sequence from File.
  69. func NewColorable(file *os.File) io.Writer {
  70. if file == nil {
  71. panic("nil passed instead of *os.File to NewColorable()")
  72. }
  73. if isatty.IsTerminal(file.Fd()) {
  74. var csbi consoleScreenBufferInfo
  75. handle := syscall.Handle(file.Fd())
  76. procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))
  77. return &Writer{out: file, handle: handle, oldattr: csbi.attributes, oldpos: coord{0, 0}}
  78. }
  79. return file
  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. switch m {
  395. case 'A':
  396. n, err = strconv.Atoi(buf.String())
  397. if err != nil {
  398. continue
  399. }
  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. csbi.cursorPosition.y += short(n)
  408. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  409. case 'C':
  410. n, err = strconv.Atoi(buf.String())
  411. if err != nil {
  412. continue
  413. }
  414. csbi.cursorPosition.x -= short(n)
  415. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  416. case 'D':
  417. n, err = strconv.Atoi(buf.String())
  418. if err != nil {
  419. continue
  420. }
  421. if n, err = strconv.Atoi(buf.String()); err == nil {
  422. var csbi consoleScreenBufferInfo
  423. csbi.cursorPosition.x += short(n)
  424. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  425. }
  426. case 'E':
  427. n, err = strconv.Atoi(buf.String())
  428. if err != nil {
  429. continue
  430. }
  431. csbi.cursorPosition.x = 0
  432. csbi.cursorPosition.y += short(n)
  433. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  434. case 'F':
  435. n, err = strconv.Atoi(buf.String())
  436. if err != nil {
  437. continue
  438. }
  439. csbi.cursorPosition.x = 0
  440. csbi.cursorPosition.y -= short(n)
  441. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  442. case 'G':
  443. n, err = strconv.Atoi(buf.String())
  444. if err != nil {
  445. continue
  446. }
  447. csbi.cursorPosition.x = short(n - 1)
  448. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  449. case 'H':
  450. if buf.Len() > 0 {
  451. token := strings.Split(buf.String(), ";")
  452. switch len(token) {
  453. case 1:
  454. n1, err := strconv.Atoi(token[0])
  455. if err != nil {
  456. continue
  457. }
  458. csbi.cursorPosition.y = short(n1 - 1)
  459. case 2:
  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. }
  471. } else {
  472. csbi.cursorPosition.y = 0
  473. }
  474. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  475. case 'J':
  476. n := 0
  477. if buf.Len() > 0 {
  478. n, err = strconv.Atoi(buf.String())
  479. if err != nil {
  480. continue
  481. }
  482. }
  483. var count, written dword
  484. var cursor coord
  485. switch n {
  486. case 0:
  487. cursor = coord{x: csbi.cursorPosition.x, y: csbi.cursorPosition.y}
  488. count = dword(csbi.size.x - csbi.cursorPosition.x + (csbi.size.y-csbi.cursorPosition.y)*csbi.size.x)
  489. case 1:
  490. cursor = coord{x: csbi.window.left, y: csbi.window.top}
  491. count = dword(csbi.size.x - csbi.cursorPosition.x + (csbi.window.top-csbi.cursorPosition.y)*csbi.size.x)
  492. case 2:
  493. cursor = coord{x: csbi.window.left, y: csbi.window.top}
  494. count = dword(csbi.size.x - csbi.cursorPosition.x + (csbi.size.y-csbi.cursorPosition.y)*csbi.size.x)
  495. }
  496. procFillConsoleOutputCharacter.Call(uintptr(w.handle), uintptr(' '), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  497. procFillConsoleOutputAttribute.Call(uintptr(w.handle), uintptr(csbi.attributes), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  498. case 'K':
  499. n := 0
  500. if buf.Len() > 0 {
  501. n, err = strconv.Atoi(buf.String())
  502. if err != nil {
  503. continue
  504. }
  505. }
  506. var cursor coord
  507. switch n {
  508. case 0:
  509. cursor = coord{x: csbi.cursorPosition.x, y: csbi.cursorPosition.y}
  510. case 1:
  511. cursor = coord{x: csbi.window.left, y: csbi.window.top + csbi.cursorPosition.y}
  512. case 2:
  513. cursor = coord{x: csbi.window.left, y: csbi.window.top + csbi.cursorPosition.y}
  514. }
  515. var count, written dword
  516. count = dword(csbi.size.x - csbi.cursorPosition.x)
  517. procFillConsoleOutputCharacter.Call(uintptr(w.handle), uintptr(' '), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  518. procFillConsoleOutputAttribute.Call(uintptr(w.handle), uintptr(csbi.attributes), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  519. case 'm':
  520. attr := csbi.attributes
  521. cs := buf.String()
  522. if cs == "" {
  523. procSetConsoleTextAttribute.Call(uintptr(w.handle), uintptr(w.oldattr))
  524. continue
  525. }
  526. token := strings.Split(cs, ";")
  527. for i := 0; i < len(token); i++ {
  528. ns := token[i]
  529. if n, err = strconv.Atoi(ns); err == nil {
  530. switch {
  531. case n == 0 || n == 100:
  532. attr = w.oldattr
  533. case 1 <= n && n <= 5:
  534. attr |= foregroundIntensity
  535. case n == 7:
  536. attr = ((attr & foregroundMask) << 4) | ((attr & backgroundMask) >> 4)
  537. case n == 22 || n == 25:
  538. attr |= foregroundIntensity
  539. case n == 27:
  540. attr = ((attr & foregroundMask) << 4) | ((attr & backgroundMask) >> 4)
  541. case 30 <= n && n <= 37:
  542. attr &= backgroundMask
  543. if (n-30)&1 != 0 {
  544. attr |= foregroundRed
  545. }
  546. if (n-30)&2 != 0 {
  547. attr |= foregroundGreen
  548. }
  549. if (n-30)&4 != 0 {
  550. attr |= foregroundBlue
  551. }
  552. case n == 38: // set foreground color.
  553. if i < len(token)-2 && (token[i+1] == "5" || token[i+1] == "05") {
  554. if n256, err := strconv.Atoi(token[i+2]); err == nil {
  555. if n256foreAttr == nil {
  556. n256setup()
  557. }
  558. attr &= backgroundMask
  559. attr |= n256foreAttr[n256]
  560. i += 2
  561. }
  562. } else {
  563. attr = attr & (w.oldattr & backgroundMask)
  564. }
  565. case n == 39: // reset foreground color.
  566. attr &= backgroundMask
  567. attr |= w.oldattr & foregroundMask
  568. case 40 <= n && n <= 47:
  569. attr &= foregroundMask
  570. if (n-40)&1 != 0 {
  571. attr |= backgroundRed
  572. }
  573. if (n-40)&2 != 0 {
  574. attr |= backgroundGreen
  575. }
  576. if (n-40)&4 != 0 {
  577. attr |= backgroundBlue
  578. }
  579. case n == 48: // set background color.
  580. if i < len(token)-2 && token[i+1] == "5" {
  581. if n256, err := strconv.Atoi(token[i+2]); err == nil {
  582. if n256backAttr == nil {
  583. n256setup()
  584. }
  585. attr &= foregroundMask
  586. attr |= n256backAttr[n256]
  587. i += 2
  588. }
  589. } else {
  590. attr = attr & (w.oldattr & foregroundMask)
  591. }
  592. case n == 49: // reset foreground color.
  593. attr &= foregroundMask
  594. attr |= w.oldattr & backgroundMask
  595. case 90 <= n && n <= 97:
  596. attr = (attr & backgroundMask)
  597. attr |= foregroundIntensity
  598. if (n-90)&1 != 0 {
  599. attr |= foregroundRed
  600. }
  601. if (n-90)&2 != 0 {
  602. attr |= foregroundGreen
  603. }
  604. if (n-90)&4 != 0 {
  605. attr |= foregroundBlue
  606. }
  607. case 100 <= n && n <= 107:
  608. attr = (attr & foregroundMask)
  609. attr |= backgroundIntensity
  610. if (n-100)&1 != 0 {
  611. attr |= backgroundRed
  612. }
  613. if (n-100)&2 != 0 {
  614. attr |= backgroundGreen
  615. }
  616. if (n-100)&4 != 0 {
  617. attr |= backgroundBlue
  618. }
  619. }
  620. procSetConsoleTextAttribute.Call(uintptr(w.handle), uintptr(attr))
  621. }
  622. }
  623. case 'h':
  624. cs := buf.String()
  625. if cs == "?25" {
  626. var ci consoleCursorInfo
  627. procGetConsoleCursorInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&ci)))
  628. ci.visible = 1
  629. procSetConsoleCursorInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&ci)))
  630. }
  631. case 'l':
  632. cs := buf.String()
  633. if cs == "?25" {
  634. var ci consoleCursorInfo
  635. procGetConsoleCursorInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&ci)))
  636. ci.visible = 0
  637. procSetConsoleCursorInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&ci)))
  638. }
  639. case 's':
  640. w.oldpos = csbi.cursorPosition
  641. case 'u':
  642. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&w.oldpos)))
  643. }
  644. }
  645. return len(data), nil
  646. }
  647. type consoleColor struct {
  648. rgb int
  649. red bool
  650. green bool
  651. blue bool
  652. intensity bool
  653. }
  654. func (c consoleColor) foregroundAttr() (attr word) {
  655. if c.red {
  656. attr |= foregroundRed
  657. }
  658. if c.green {
  659. attr |= foregroundGreen
  660. }
  661. if c.blue {
  662. attr |= foregroundBlue
  663. }
  664. if c.intensity {
  665. attr |= foregroundIntensity
  666. }
  667. return
  668. }
  669. func (c consoleColor) backgroundAttr() (attr word) {
  670. if c.red {
  671. attr |= backgroundRed
  672. }
  673. if c.green {
  674. attr |= backgroundGreen
  675. }
  676. if c.blue {
  677. attr |= backgroundBlue
  678. }
  679. if c.intensity {
  680. attr |= backgroundIntensity
  681. }
  682. return
  683. }
  684. var color16 = []consoleColor{
  685. {0x000000, false, false, false, false},
  686. {0x000080, false, false, true, false},
  687. {0x008000, false, true, false, false},
  688. {0x008080, false, true, true, false},
  689. {0x800000, true, false, false, false},
  690. {0x800080, true, false, true, false},
  691. {0x808000, true, true, false, false},
  692. {0xc0c0c0, true, true, true, false},
  693. {0x808080, false, false, false, true},
  694. {0x0000ff, false, false, true, true},
  695. {0x00ff00, false, true, false, true},
  696. {0x00ffff, false, true, true, true},
  697. {0xff0000, true, false, false, true},
  698. {0xff00ff, true, false, true, true},
  699. {0xffff00, true, true, false, true},
  700. {0xffffff, true, true, true, true},
  701. }
  702. type hsv struct {
  703. h, s, v float32
  704. }
  705. func (a hsv) dist(b hsv) float32 {
  706. dh := a.h - b.h
  707. switch {
  708. case dh > 0.5:
  709. dh = 1 - dh
  710. case dh < -0.5:
  711. dh = -1 - dh
  712. }
  713. ds := a.s - b.s
  714. dv := a.v - b.v
  715. return float32(math.Sqrt(float64(dh*dh + ds*ds + dv*dv)))
  716. }
  717. func toHSV(rgb int) hsv {
  718. r, g, b := float32((rgb&0xFF0000)>>16)/256.0,
  719. float32((rgb&0x00FF00)>>8)/256.0,
  720. float32(rgb&0x0000FF)/256.0
  721. min, max := minmax3f(r, g, b)
  722. h := max - min
  723. if h > 0 {
  724. if max == r {
  725. h = (g - b) / h
  726. if h < 0 {
  727. h += 6
  728. }
  729. } else if max == g {
  730. h = 2 + (b-r)/h
  731. } else {
  732. h = 4 + (r-g)/h
  733. }
  734. }
  735. h /= 6.0
  736. s := max - min
  737. if max != 0 {
  738. s /= max
  739. }
  740. v := max
  741. return hsv{h: h, s: s, v: v}
  742. }
  743. type hsvTable []hsv
  744. func toHSVTable(rgbTable []consoleColor) hsvTable {
  745. t := make(hsvTable, len(rgbTable))
  746. for i, c := range rgbTable {
  747. t[i] = toHSV(c.rgb)
  748. }
  749. return t
  750. }
  751. func (t hsvTable) find(rgb int) consoleColor {
  752. hsv := toHSV(rgb)
  753. n := 7
  754. l := float32(5.0)
  755. for i, p := range t {
  756. d := hsv.dist(p)
  757. if d < l {
  758. l, n = d, i
  759. }
  760. }
  761. return color16[n]
  762. }
  763. func minmax3f(a, b, c float32) (min, max float32) {
  764. if a < b {
  765. if b < c {
  766. return a, c
  767. } else if a < c {
  768. return a, b
  769. } else {
  770. return c, b
  771. }
  772. } else {
  773. if a < c {
  774. return b, c
  775. } else if b < c {
  776. return b, a
  777. } else {
  778. return c, a
  779. }
  780. }
  781. }
  782. var n256foreAttr []word
  783. var n256backAttr []word
  784. func n256setup() {
  785. n256foreAttr = make([]word, 256)
  786. n256backAttr = make([]word, 256)
  787. t := toHSVTable(color16)
  788. for i, rgb := range color256 {
  789. c := t.find(rgb)
  790. n256foreAttr[i] = c.foregroundAttr()
  791. n256backAttr[i] = c.backgroundAttr()
  792. }
  793. }