colorable_windows.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. package colorable
  2. import (
  3. "bytes"
  4. "io"
  5. "math"
  6. "os"
  7. "strconv"
  8. "strings"
  9. "syscall"
  10. "unsafe"
  11. "github.com/mattn/go-isatty"
  12. )
  13. const (
  14. foregroundBlue = 0x1
  15. foregroundGreen = 0x2
  16. foregroundRed = 0x4
  17. foregroundIntensity = 0x8
  18. foregroundMask = (foregroundRed | foregroundBlue | foregroundGreen | foregroundIntensity)
  19. backgroundBlue = 0x10
  20. backgroundGreen = 0x20
  21. backgroundRed = 0x40
  22. backgroundIntensity = 0x80
  23. backgroundMask = (backgroundRed | backgroundBlue | backgroundGreen | backgroundIntensity)
  24. )
  25. type wchar uint16
  26. type short int16
  27. type dword uint32
  28. type word uint16
  29. type coord struct {
  30. x short
  31. y short
  32. }
  33. type smallRect struct {
  34. left short
  35. top short
  36. right short
  37. bottom short
  38. }
  39. type consoleScreenBufferInfo struct {
  40. size coord
  41. cursorPosition coord
  42. attributes word
  43. window smallRect
  44. maximumWindowSize coord
  45. }
  46. type consoleCursorInfo struct {
  47. size dword
  48. visible int32
  49. }
  50. var (
  51. kernel32 = syscall.NewLazyDLL("kernel32.dll")
  52. procGetConsoleScreenBufferInfo = kernel32.NewProc("GetConsoleScreenBufferInfo")
  53. procSetConsoleTextAttribute = kernel32.NewProc("SetConsoleTextAttribute")
  54. procSetConsoleCursorPosition = kernel32.NewProc("SetConsoleCursorPosition")
  55. procFillConsoleOutputCharacter = kernel32.NewProc("FillConsoleOutputCharacterW")
  56. procFillConsoleOutputAttribute = kernel32.NewProc("FillConsoleOutputAttribute")
  57. procGetConsoleCursorInfo = kernel32.NewProc("GetConsoleCursorInfo")
  58. procSetConsoleCursorInfo = kernel32.NewProc("SetConsoleCursorInfo")
  59. )
  60. type Writer struct {
  61. out io.Writer
  62. handle syscall.Handle
  63. lastbuf bytes.Buffer
  64. oldattr word
  65. oldpos coord
  66. }
  67. 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, oldpos: coord{0, 0}}
  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.NewReader(data)
  348. var bw [1]byte
  349. loop:
  350. for {
  351. r1, _, err := procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  352. if r1 == 0 {
  353. break loop
  354. }
  355. c1, err := er.ReadByte()
  356. if err != nil {
  357. break loop
  358. }
  359. if c1 != 0x1b {
  360. bw[0] = c1
  361. w.out.Write(bw[:])
  362. continue
  363. }
  364. c2, err := er.ReadByte()
  365. if err != nil {
  366. w.lastbuf.WriteByte(c1)
  367. break loop
  368. }
  369. if c2 != 0x5b {
  370. w.lastbuf.WriteByte(c1)
  371. w.lastbuf.WriteByte(c2)
  372. continue
  373. }
  374. var buf bytes.Buffer
  375. var m byte
  376. for {
  377. c, err := er.ReadByte()
  378. if err != nil {
  379. w.lastbuf.WriteByte(c1)
  380. w.lastbuf.WriteByte(c2)
  381. w.lastbuf.Write(buf.Bytes())
  382. break loop
  383. }
  384. if ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '@' {
  385. m = c
  386. break
  387. }
  388. buf.Write([]byte(string(c)))
  389. }
  390. var csbi consoleScreenBufferInfo
  391. switch m {
  392. case 'A':
  393. n, err = strconv.Atoi(buf.String())
  394. if err != nil {
  395. continue
  396. }
  397. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  398. csbi.cursorPosition.y -= short(n)
  399. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  400. case 'B':
  401. n, err = strconv.Atoi(buf.String())
  402. if err != nil {
  403. continue
  404. }
  405. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  406. csbi.cursorPosition.y += short(n)
  407. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  408. case 'C':
  409. n, err = strconv.Atoi(buf.String())
  410. if err != nil {
  411. continue
  412. }
  413. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  414. csbi.cursorPosition.x -= short(n)
  415. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  416. case 'D':
  417. n, err = strconv.Atoi(buf.String())
  418. if err != nil {
  419. continue
  420. }
  421. if n, err = strconv.Atoi(buf.String()); err == nil {
  422. var csbi consoleScreenBufferInfo
  423. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  424. csbi.cursorPosition.x += short(n)
  425. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  426. }
  427. case 'E':
  428. n, err = strconv.Atoi(buf.String())
  429. if err != nil {
  430. continue
  431. }
  432. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  433. csbi.cursorPosition.x = 0
  434. csbi.cursorPosition.y += short(n)
  435. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  436. case 'F':
  437. n, err = strconv.Atoi(buf.String())
  438. if err != nil {
  439. continue
  440. }
  441. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  442. csbi.cursorPosition.x = 0
  443. csbi.cursorPosition.y -= short(n)
  444. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  445. case 'G':
  446. n, err = strconv.Atoi(buf.String())
  447. if err != nil {
  448. continue
  449. }
  450. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  451. csbi.cursorPosition.x = short(n - 1)
  452. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  453. case 'H':
  454. token := strings.Split(buf.String(), ";")
  455. if len(token) != 2 {
  456. continue
  457. }
  458. n1, err := strconv.Atoi(token[0])
  459. if err != nil {
  460. continue
  461. }
  462. n2, err := strconv.Atoi(token[1])
  463. if err != nil {
  464. continue
  465. }
  466. csbi.cursorPosition.x = short(n2 - 1)
  467. csbi.cursorPosition.y = short(n1 - 1)
  468. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  469. case 'J':
  470. n, err := strconv.Atoi(buf.String())
  471. if err != nil {
  472. continue
  473. }
  474. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  475. var cursor coord
  476. switch n {
  477. case 0:
  478. cursor = coord{x: csbi.cursorPosition.x, y: csbi.cursorPosition.y}
  479. case 1:
  480. cursor = coord{x: csbi.window.left, y: csbi.window.top}
  481. case 2:
  482. cursor = coord{x: csbi.window.left, y: csbi.window.top}
  483. }
  484. var count, written dword
  485. count = dword(csbi.size.x - csbi.cursorPosition.x + (csbi.size.y-csbi.cursorPosition.y)*csbi.size.x)
  486. procFillConsoleOutputCharacter.Call(uintptr(w.handle), uintptr(' '), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  487. procFillConsoleOutputAttribute.Call(uintptr(w.handle), uintptr(csbi.attributes), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  488. case 'K':
  489. n, err := strconv.Atoi(buf.String())
  490. if err != nil {
  491. continue
  492. }
  493. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  494. var cursor coord
  495. switch n {
  496. case 0:
  497. cursor = coord{x: csbi.cursorPosition.x, y: csbi.cursorPosition.y}
  498. case 1:
  499. cursor = coord{x: csbi.window.left, y: csbi.window.top + csbi.cursorPosition.y}
  500. case 2:
  501. cursor = coord{x: csbi.window.left, y: csbi.window.top + csbi.cursorPosition.y}
  502. }
  503. var count, written dword
  504. count = dword(csbi.size.x - csbi.cursorPosition.x)
  505. procFillConsoleOutputCharacter.Call(uintptr(w.handle), uintptr(' '), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  506. procFillConsoleOutputAttribute.Call(uintptr(w.handle), uintptr(csbi.attributes), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  507. case 'm':
  508. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  509. attr := csbi.attributes
  510. cs := buf.String()
  511. if cs == "" {
  512. procSetConsoleTextAttribute.Call(uintptr(w.handle), uintptr(w.oldattr))
  513. continue
  514. }
  515. token := strings.Split(cs, ";")
  516. for i := 0; i < len(token); i++ {
  517. ns := token[i]
  518. if n, err = strconv.Atoi(ns); err == nil {
  519. switch {
  520. case n == 0 || n == 100:
  521. attr = w.oldattr
  522. case 1 <= n && n <= 5:
  523. attr |= foregroundIntensity
  524. case n == 7:
  525. attr = ((attr & foregroundMask) << 4) | ((attr & backgroundMask) >> 4)
  526. case 22 == n || n == 25 || n == 25:
  527. attr |= foregroundIntensity
  528. case n == 27:
  529. attr = ((attr & foregroundMask) << 4) | ((attr & backgroundMask) >> 4)
  530. case 30 <= n && n <= 37:
  531. attr &= backgroundMask
  532. if (n-30)&1 != 0 {
  533. attr |= foregroundRed
  534. }
  535. if (n-30)&2 != 0 {
  536. attr |= foregroundGreen
  537. }
  538. if (n-30)&4 != 0 {
  539. attr |= foregroundBlue
  540. }
  541. case n == 38: // set foreground color.
  542. if i < len(token)-2 && (token[i+1] == "5" || token[i+1] == "05") {
  543. if n256, err := strconv.Atoi(token[i+2]); err == nil {
  544. if n256foreAttr == nil {
  545. n256setup()
  546. }
  547. attr &= backgroundMask
  548. attr |= n256foreAttr[n256]
  549. i += 2
  550. }
  551. } else {
  552. attr = attr & (w.oldattr & backgroundMask)
  553. }
  554. case n == 39: // reset foreground color.
  555. attr &= backgroundMask
  556. attr |= w.oldattr & foregroundMask
  557. case 40 <= n && n <= 47:
  558. attr &= foregroundMask
  559. if (n-40)&1 != 0 {
  560. attr |= backgroundRed
  561. }
  562. if (n-40)&2 != 0 {
  563. attr |= backgroundGreen
  564. }
  565. if (n-40)&4 != 0 {
  566. attr |= backgroundBlue
  567. }
  568. case n == 48: // set background color.
  569. if i < len(token)-2 && token[i+1] == "5" {
  570. if n256, err := strconv.Atoi(token[i+2]); err == nil {
  571. if n256backAttr == nil {
  572. n256setup()
  573. }
  574. attr &= foregroundMask
  575. attr |= n256backAttr[n256]
  576. i += 2
  577. }
  578. } else {
  579. attr = attr & (w.oldattr & foregroundMask)
  580. }
  581. case n == 49: // reset foreground color.
  582. attr &= foregroundMask
  583. attr |= w.oldattr & backgroundMask
  584. case 90 <= n && n <= 97:
  585. attr = (attr & backgroundMask)
  586. attr |= foregroundIntensity
  587. if (n-90)&1 != 0 {
  588. attr |= foregroundRed
  589. }
  590. if (n-90)&2 != 0 {
  591. attr |= foregroundGreen
  592. }
  593. if (n-90)&4 != 0 {
  594. attr |= foregroundBlue
  595. }
  596. case 100 <= n && n <= 107:
  597. attr = (attr & foregroundMask)
  598. attr |= backgroundIntensity
  599. if (n-100)&1 != 0 {
  600. attr |= backgroundRed
  601. }
  602. if (n-100)&2 != 0 {
  603. attr |= backgroundGreen
  604. }
  605. if (n-100)&4 != 0 {
  606. attr |= backgroundBlue
  607. }
  608. }
  609. procSetConsoleTextAttribute.Call(uintptr(w.handle), uintptr(attr))
  610. }
  611. }
  612. case 'h':
  613. cs := buf.String()
  614. if cs == "?25" {
  615. var ci consoleCursorInfo
  616. procGetConsoleCursorInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&ci)))
  617. ci.visible = 1
  618. procSetConsoleCursorInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&ci)))
  619. }
  620. case 'l':
  621. cs := buf.String()
  622. if cs == "?25" {
  623. var ci consoleCursorInfo
  624. procGetConsoleCursorInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&ci)))
  625. ci.visible = 0
  626. procSetConsoleCursorInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&ci)))
  627. }
  628. case 's':
  629. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  630. w.oldpos = csbi.cursorPosition
  631. case 'u':
  632. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&w.oldpos)))
  633. }
  634. }
  635. return len(data) - w.lastbuf.Len(), nil
  636. }
  637. type consoleColor struct {
  638. rgb int
  639. red bool
  640. green bool
  641. blue bool
  642. intensity bool
  643. }
  644. func (c consoleColor) foregroundAttr() (attr word) {
  645. if c.red {
  646. attr |= foregroundRed
  647. }
  648. if c.green {
  649. attr |= foregroundGreen
  650. }
  651. if c.blue {
  652. attr |= foregroundBlue
  653. }
  654. if c.intensity {
  655. attr |= foregroundIntensity
  656. }
  657. return
  658. }
  659. func (c consoleColor) backgroundAttr() (attr word) {
  660. if c.red {
  661. attr |= backgroundRed
  662. }
  663. if c.green {
  664. attr |= backgroundGreen
  665. }
  666. if c.blue {
  667. attr |= backgroundBlue
  668. }
  669. if c.intensity {
  670. attr |= backgroundIntensity
  671. }
  672. return
  673. }
  674. var color16 = []consoleColor{
  675. consoleColor{0x000000, false, false, false, false},
  676. consoleColor{0x000080, false, false, true, false},
  677. consoleColor{0x008000, false, true, false, false},
  678. consoleColor{0x008080, false, true, true, false},
  679. consoleColor{0x800000, true, false, false, false},
  680. consoleColor{0x800080, true, false, true, false},
  681. consoleColor{0x808000, true, true, false, false},
  682. consoleColor{0xc0c0c0, true, true, true, false},
  683. consoleColor{0x808080, false, false, false, true},
  684. consoleColor{0x0000ff, false, false, true, true},
  685. consoleColor{0x00ff00, false, true, false, true},
  686. consoleColor{0x00ffff, false, true, true, true},
  687. consoleColor{0xff0000, true, false, false, true},
  688. consoleColor{0xff00ff, true, false, true, true},
  689. consoleColor{0xffff00, true, true, false, true},
  690. consoleColor{0xffffff, true, true, true, true},
  691. }
  692. type hsv struct {
  693. h, s, v float32
  694. }
  695. func (a hsv) dist(b hsv) float32 {
  696. dh := a.h - b.h
  697. switch {
  698. case dh > 0.5:
  699. dh = 1 - dh
  700. case dh < -0.5:
  701. dh = -1 - dh
  702. }
  703. ds := a.s - b.s
  704. dv := a.v - b.v
  705. return float32(math.Sqrt(float64(dh*dh + ds*ds + dv*dv)))
  706. }
  707. func toHSV(rgb int) hsv {
  708. r, g, b := float32((rgb&0xFF0000)>>16)/256.0,
  709. float32((rgb&0x00FF00)>>8)/256.0,
  710. float32(rgb&0x0000FF)/256.0
  711. min, max := minmax3f(r, g, b)
  712. h := max - min
  713. if h > 0 {
  714. if max == r {
  715. h = (g - b) / h
  716. if h < 0 {
  717. h += 6
  718. }
  719. } else if max == g {
  720. h = 2 + (b-r)/h
  721. } else {
  722. h = 4 + (r-g)/h
  723. }
  724. }
  725. h /= 6.0
  726. s := max - min
  727. if max != 0 {
  728. s /= max
  729. }
  730. v := max
  731. return hsv{h: h, s: s, v: v}
  732. }
  733. type hsvTable []hsv
  734. func toHSVTable(rgbTable []consoleColor) hsvTable {
  735. t := make(hsvTable, len(rgbTable))
  736. for i, c := range rgbTable {
  737. t[i] = toHSV(c.rgb)
  738. }
  739. return t
  740. }
  741. func (t hsvTable) find(rgb int) consoleColor {
  742. hsv := toHSV(rgb)
  743. n := 7
  744. l := float32(5.0)
  745. for i, p := range t {
  746. d := hsv.dist(p)
  747. if d < l {
  748. l, n = d, i
  749. }
  750. }
  751. return color16[n]
  752. }
  753. func minmax3f(a, b, c float32) (min, max float32) {
  754. if a < b {
  755. if b < c {
  756. return a, c
  757. } else if a < c {
  758. return a, b
  759. } else {
  760. return c, b
  761. }
  762. } else {
  763. if a < c {
  764. return b, c
  765. } else if b < c {
  766. return b, a
  767. } else {
  768. return c, a
  769. }
  770. }
  771. }
  772. var n256foreAttr []word
  773. var n256backAttr []word
  774. func n256setup() {
  775. n256foreAttr = make([]word, 256)
  776. n256backAttr = make([]word, 256)
  777. t := toHSVTable(color16)
  778. for i, rgb := range color256 {
  779. c := t.find(rgb)
  780. n256foreAttr[i] = c.foregroundAttr()
  781. n256backAttr[i] = c.backgroundAttr()
  782. }
  783. }