colorable_windows.go 18 KB

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