colorable_windows.go 19 KB

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