colorable_windows.go 18 KB

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