colorable_windows.go 18 KB

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