colorable_windows.go 18 KB

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