colorable_windows.go 18 KB

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