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. 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, err := strconv.Atoi(buf.String())
  502. if err != nil {
  503. continue
  504. }
  505. var cursor coord
  506. switch n {
  507. case 0:
  508. cursor = coord{x: csbi.cursorPosition.x, y: csbi.cursorPosition.y}
  509. case 1:
  510. cursor = coord{x: csbi.window.left, y: csbi.window.top + csbi.cursorPosition.y}
  511. case 2:
  512. cursor = coord{x: csbi.window.left, y: csbi.window.top + csbi.cursorPosition.y}
  513. }
  514. var count, written dword
  515. count = dword(csbi.size.x - csbi.cursorPosition.x)
  516. procFillConsoleOutputCharacter.Call(uintptr(w.handle), uintptr(' '), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  517. procFillConsoleOutputAttribute.Call(uintptr(w.handle), uintptr(csbi.attributes), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  518. case 'm':
  519. attr := csbi.attributes
  520. cs := buf.String()
  521. if cs == "" {
  522. procSetConsoleTextAttribute.Call(uintptr(w.handle), uintptr(w.oldattr))
  523. continue
  524. }
  525. token := strings.Split(cs, ";")
  526. for i := 0; i < len(token); i++ {
  527. ns := token[i]
  528. if n, err = strconv.Atoi(ns); err == nil {
  529. switch {
  530. case n == 0 || n == 100:
  531. attr = w.oldattr
  532. case 1 <= n && n <= 5:
  533. attr |= foregroundIntensity
  534. case n == 7:
  535. attr = ((attr & foregroundMask) << 4) | ((attr & backgroundMask) >> 4)
  536. case 22 == n || n == 25 || n == 25:
  537. attr |= foregroundIntensity
  538. case n == 27:
  539. attr = ((attr & foregroundMask) << 4) | ((attr & backgroundMask) >> 4)
  540. case 30 <= n && n <= 37:
  541. attr &= backgroundMask
  542. if (n-30)&1 != 0 {
  543. attr |= foregroundRed
  544. }
  545. if (n-30)&2 != 0 {
  546. attr |= foregroundGreen
  547. }
  548. if (n-30)&4 != 0 {
  549. attr |= foregroundBlue
  550. }
  551. case n == 38: // set foreground color.
  552. if i < len(token)-2 && (token[i+1] == "5" || token[i+1] == "05") {
  553. if n256, err := strconv.Atoi(token[i+2]); err == nil {
  554. if n256foreAttr == nil {
  555. n256setup()
  556. }
  557. attr &= backgroundMask
  558. attr |= n256foreAttr[n256]
  559. i += 2
  560. }
  561. } else {
  562. attr = attr & (w.oldattr & backgroundMask)
  563. }
  564. case n == 39: // reset foreground color.
  565. attr &= backgroundMask
  566. attr |= w.oldattr & foregroundMask
  567. case 40 <= n && n <= 47:
  568. attr &= foregroundMask
  569. if (n-40)&1 != 0 {
  570. attr |= backgroundRed
  571. }
  572. if (n-40)&2 != 0 {
  573. attr |= backgroundGreen
  574. }
  575. if (n-40)&4 != 0 {
  576. attr |= backgroundBlue
  577. }
  578. case n == 48: // set background color.
  579. if i < len(token)-2 && token[i+1] == "5" {
  580. if n256, err := strconv.Atoi(token[i+2]); err == nil {
  581. if n256backAttr == nil {
  582. n256setup()
  583. }
  584. attr &= foregroundMask
  585. attr |= n256backAttr[n256]
  586. i += 2
  587. }
  588. } else {
  589. attr = attr & (w.oldattr & foregroundMask)
  590. }
  591. case n == 49: // reset foreground color.
  592. attr &= foregroundMask
  593. attr |= w.oldattr & backgroundMask
  594. case 90 <= n && n <= 97:
  595. attr = (attr & backgroundMask)
  596. attr |= foregroundIntensity
  597. if (n-90)&1 != 0 {
  598. attr |= foregroundRed
  599. }
  600. if (n-90)&2 != 0 {
  601. attr |= foregroundGreen
  602. }
  603. if (n-90)&4 != 0 {
  604. attr |= foregroundBlue
  605. }
  606. case 100 <= n && n <= 107:
  607. attr = (attr & foregroundMask)
  608. attr |= backgroundIntensity
  609. if (n-100)&1 != 0 {
  610. attr |= backgroundRed
  611. }
  612. if (n-100)&2 != 0 {
  613. attr |= backgroundGreen
  614. }
  615. if (n-100)&4 != 0 {
  616. attr |= backgroundBlue
  617. }
  618. }
  619. procSetConsoleTextAttribute.Call(uintptr(w.handle), uintptr(attr))
  620. }
  621. }
  622. case 'h':
  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 = 1
  628. procSetConsoleCursorInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&ci)))
  629. }
  630. case 'l':
  631. cs := buf.String()
  632. if cs == "?25" {
  633. var ci consoleCursorInfo
  634. procGetConsoleCursorInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&ci)))
  635. ci.visible = 0
  636. procSetConsoleCursorInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&ci)))
  637. }
  638. case 's':
  639. w.oldpos = csbi.cursorPosition
  640. case 'u':
  641. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&w.oldpos)))
  642. }
  643. }
  644. return len(data), nil
  645. }
  646. type consoleColor struct {
  647. rgb int
  648. red bool
  649. green bool
  650. blue bool
  651. intensity bool
  652. }
  653. func (c consoleColor) foregroundAttr() (attr word) {
  654. if c.red {
  655. attr |= foregroundRed
  656. }
  657. if c.green {
  658. attr |= foregroundGreen
  659. }
  660. if c.blue {
  661. attr |= foregroundBlue
  662. }
  663. if c.intensity {
  664. attr |= foregroundIntensity
  665. }
  666. return
  667. }
  668. func (c consoleColor) backgroundAttr() (attr word) {
  669. if c.red {
  670. attr |= backgroundRed
  671. }
  672. if c.green {
  673. attr |= backgroundGreen
  674. }
  675. if c.blue {
  676. attr |= backgroundBlue
  677. }
  678. if c.intensity {
  679. attr |= backgroundIntensity
  680. }
  681. return
  682. }
  683. var color16 = []consoleColor{
  684. consoleColor{0x000000, false, false, false, false},
  685. consoleColor{0x000080, false, false, true, false},
  686. consoleColor{0x008000, false, true, false, false},
  687. consoleColor{0x008080, false, true, true, false},
  688. consoleColor{0x800000, true, false, false, false},
  689. consoleColor{0x800080, true, false, true, false},
  690. consoleColor{0x808000, true, true, false, false},
  691. consoleColor{0xc0c0c0, true, true, true, false},
  692. consoleColor{0x808080, false, false, false, true},
  693. consoleColor{0x0000ff, false, false, true, true},
  694. consoleColor{0x00ff00, false, true, false, true},
  695. consoleColor{0x00ffff, false, true, true, true},
  696. consoleColor{0xff0000, true, false, false, true},
  697. consoleColor{0xff00ff, true, false, true, true},
  698. consoleColor{0xffff00, true, true, false, true},
  699. consoleColor{0xffffff, true, true, true, true},
  700. }
  701. type hsv struct {
  702. h, s, v float32
  703. }
  704. func (a hsv) dist(b hsv) float32 {
  705. dh := a.h - b.h
  706. switch {
  707. case dh > 0.5:
  708. dh = 1 - dh
  709. case dh < -0.5:
  710. dh = -1 - dh
  711. }
  712. ds := a.s - b.s
  713. dv := a.v - b.v
  714. return float32(math.Sqrt(float64(dh*dh + ds*ds + dv*dv)))
  715. }
  716. func toHSV(rgb int) hsv {
  717. r, g, b := float32((rgb&0xFF0000)>>16)/256.0,
  718. float32((rgb&0x00FF00)>>8)/256.0,
  719. float32(rgb&0x0000FF)/256.0
  720. min, max := minmax3f(r, g, b)
  721. h := max - min
  722. if h > 0 {
  723. if max == r {
  724. h = (g - b) / h
  725. if h < 0 {
  726. h += 6
  727. }
  728. } else if max == g {
  729. h = 2 + (b-r)/h
  730. } else {
  731. h = 4 + (r-g)/h
  732. }
  733. }
  734. h /= 6.0
  735. s := max - min
  736. if max != 0 {
  737. s /= max
  738. }
  739. v := max
  740. return hsv{h: h, s: s, v: v}
  741. }
  742. type hsvTable []hsv
  743. func toHSVTable(rgbTable []consoleColor) hsvTable {
  744. t := make(hsvTable, len(rgbTable))
  745. for i, c := range rgbTable {
  746. t[i] = toHSV(c.rgb)
  747. }
  748. return t
  749. }
  750. func (t hsvTable) find(rgb int) consoleColor {
  751. hsv := toHSV(rgb)
  752. n := 7
  753. l := float32(5.0)
  754. for i, p := range t {
  755. d := hsv.dist(p)
  756. if d < l {
  757. l, n = d, i
  758. }
  759. }
  760. return color16[n]
  761. }
  762. func minmax3f(a, b, c float32) (min, max float32) {
  763. if a < b {
  764. if b < c {
  765. return a, c
  766. } else if a < c {
  767. return a, b
  768. } else {
  769. return c, b
  770. }
  771. } else {
  772. if a < c {
  773. return b, c
  774. } else if b < c {
  775. return b, a
  776. } else {
  777. return c, a
  778. }
  779. }
  780. }
  781. var n256foreAttr []word
  782. var n256backAttr []word
  783. func n256setup() {
  784. n256foreAttr = make([]word, 256)
  785. n256backAttr = make([]word, 256)
  786. t := toHSVTable(color16)
  787. for i, rgb := range color256 {
  788. c := t.find(rgb)
  789. n256foreAttr[i] = c.foregroundAttr()
  790. n256backAttr[i] = c.backgroundAttr()
  791. }
  792. }