colorable_windows.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  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. c1, err := er.ReadByte()
  395. if err != nil {
  396. break loop
  397. }
  398. if c1 != 0x1b {
  399. bw[0] = c1
  400. w.out.Write(bw[:])
  401. continue
  402. }
  403. c2, err := er.ReadByte()
  404. if err != nil {
  405. break loop
  406. }
  407. if c2 == ']' {
  408. if err := doTitleSequence(er); err != nil {
  409. break loop
  410. }
  411. continue
  412. }
  413. if c2 != 0x5b {
  414. continue
  415. }
  416. var buf bytes.Buffer
  417. var m byte
  418. for {
  419. c, err := er.ReadByte()
  420. if err != nil {
  421. break loop
  422. }
  423. if ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '@' {
  424. m = c
  425. break
  426. }
  427. buf.Write([]byte(string(c)))
  428. }
  429. switch m {
  430. case 'A':
  431. n, err = strconv.Atoi(buf.String())
  432. if err != nil {
  433. continue
  434. }
  435. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  436. csbi.cursorPosition.y -= short(n)
  437. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  438. case 'B':
  439. n, err = strconv.Atoi(buf.String())
  440. if err != nil {
  441. continue
  442. }
  443. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  444. csbi.cursorPosition.y += short(n)
  445. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  446. case 'C':
  447. n, err = strconv.Atoi(buf.String())
  448. if err != nil {
  449. continue
  450. }
  451. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  452. csbi.cursorPosition.x += short(n)
  453. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  454. case 'D':
  455. n, err = strconv.Atoi(buf.String())
  456. if err != nil {
  457. continue
  458. }
  459. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  460. csbi.cursorPosition.x -= short(n)
  461. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  462. case 'E':
  463. n, err = strconv.Atoi(buf.String())
  464. if err != nil {
  465. continue
  466. }
  467. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  468. csbi.cursorPosition.x = 0
  469. csbi.cursorPosition.y += short(n)
  470. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  471. case 'F':
  472. n, err = strconv.Atoi(buf.String())
  473. if err != nil {
  474. continue
  475. }
  476. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  477. csbi.cursorPosition.x = 0
  478. csbi.cursorPosition.y -= short(n)
  479. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  480. case 'G':
  481. n, err = strconv.Atoi(buf.String())
  482. if err != nil {
  483. continue
  484. }
  485. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  486. csbi.cursorPosition.x = short(n - 1)
  487. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  488. case 'H', 'f':
  489. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  490. if buf.Len() > 0 {
  491. token := strings.Split(buf.String(), ";")
  492. switch len(token) {
  493. case 1:
  494. n1, err := strconv.Atoi(token[0])
  495. if err != nil {
  496. continue
  497. }
  498. csbi.cursorPosition.y = short(n1 - 1)
  499. case 2:
  500. n1, err := strconv.Atoi(token[0])
  501. if err != nil {
  502. continue
  503. }
  504. n2, err := strconv.Atoi(token[1])
  505. if err != nil {
  506. continue
  507. }
  508. csbi.cursorPosition.x = short(n2 - 1)
  509. csbi.cursorPosition.y = short(n1 - 1)
  510. }
  511. } else {
  512. csbi.cursorPosition.y = 0
  513. }
  514. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  515. case 'J':
  516. n := 0
  517. if buf.Len() > 0 {
  518. n, err = strconv.Atoi(buf.String())
  519. if err != nil {
  520. continue
  521. }
  522. }
  523. var count, written dword
  524. var cursor coord
  525. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  526. switch n {
  527. case 0:
  528. cursor = coord{x: csbi.cursorPosition.x, y: csbi.cursorPosition.y}
  529. count = dword(csbi.size.x - csbi.cursorPosition.x + (csbi.size.y-csbi.cursorPosition.y)*csbi.size.x)
  530. case 1:
  531. cursor = coord{x: csbi.window.left, y: csbi.window.top}
  532. count = dword(csbi.size.x - csbi.cursorPosition.x + (csbi.window.top-csbi.cursorPosition.y)*csbi.size.x)
  533. case 2:
  534. cursor = coord{x: csbi.window.left, y: csbi.window.top}
  535. count = dword(csbi.size.x - csbi.cursorPosition.x + (csbi.size.y-csbi.cursorPosition.y)*csbi.size.x)
  536. }
  537. procFillConsoleOutputCharacter.Call(uintptr(w.handle), uintptr(' '), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  538. procFillConsoleOutputAttribute.Call(uintptr(w.handle), uintptr(csbi.attributes), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  539. case 'K':
  540. n := 0
  541. if buf.Len() > 0 {
  542. n, err = strconv.Atoi(buf.String())
  543. if err != nil {
  544. continue
  545. }
  546. }
  547. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  548. var cursor coord
  549. var count, written dword
  550. switch n {
  551. case 0:
  552. cursor = coord{x: csbi.cursorPosition.x + 1, y: csbi.cursorPosition.y}
  553. count = dword(csbi.size.x - csbi.cursorPosition.x - 1)
  554. case 1:
  555. cursor = coord{x: csbi.window.left, y: csbi.window.top + csbi.cursorPosition.y}
  556. count = dword(csbi.size.x - csbi.cursorPosition.x)
  557. case 2:
  558. cursor = coord{x: csbi.window.left, y: csbi.window.top + csbi.cursorPosition.y}
  559. count = dword(csbi.size.x)
  560. }
  561. procFillConsoleOutputCharacter.Call(uintptr(w.handle), uintptr(' '), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  562. procFillConsoleOutputAttribute.Call(uintptr(w.handle), uintptr(csbi.attributes), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  563. case 'm':
  564. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  565. attr := csbi.attributes
  566. cs := buf.String()
  567. if cs == "" {
  568. procSetConsoleTextAttribute.Call(uintptr(w.handle), uintptr(w.oldattr))
  569. continue
  570. }
  571. token := strings.Split(cs, ";")
  572. for i := 0; i < len(token); i++ {
  573. ns := token[i]
  574. if n, err = strconv.Atoi(ns); err == nil {
  575. switch {
  576. case n == 0 || n == 100:
  577. attr = w.oldattr
  578. case 1 <= n && n <= 5:
  579. attr |= foregroundIntensity
  580. case n == 7:
  581. attr = ((attr & foregroundMask) << 4) | ((attr & backgroundMask) >> 4)
  582. case n == 22 || n == 25:
  583. attr |= foregroundIntensity
  584. case n == 27:
  585. attr = ((attr & foregroundMask) << 4) | ((attr & backgroundMask) >> 4)
  586. case 30 <= n && n <= 37:
  587. attr &= backgroundMask
  588. if (n-30)&1 != 0 {
  589. attr |= foregroundRed
  590. }
  591. if (n-30)&2 != 0 {
  592. attr |= foregroundGreen
  593. }
  594. if (n-30)&4 != 0 {
  595. attr |= foregroundBlue
  596. }
  597. case n == 38: // set foreground color.
  598. if i < len(token)-2 && (token[i+1] == "5" || token[i+1] == "05") {
  599. if n256, err := strconv.Atoi(token[i+2]); err == nil {
  600. if n256foreAttr == nil {
  601. n256setup()
  602. }
  603. attr &= backgroundMask
  604. attr |= n256foreAttr[n256]
  605. i += 2
  606. }
  607. } else if len(token) == 5 && token[i+1] == "2" {
  608. var r, g, b int
  609. r, _ = strconv.Atoi(token[i+2])
  610. g, _ = strconv.Atoi(token[i+3])
  611. b, _ = strconv.Atoi(token[i+4])
  612. i += 4
  613. if r > 127 {
  614. attr |= foregroundRed
  615. }
  616. if g > 127 {
  617. attr |= foregroundGreen
  618. }
  619. if b > 127 {
  620. attr |= foregroundBlue
  621. }
  622. } else {
  623. attr = attr & (w.oldattr & backgroundMask)
  624. }
  625. case n == 39: // reset foreground color.
  626. attr &= backgroundMask
  627. attr |= w.oldattr & foregroundMask
  628. case 40 <= n && n <= 47:
  629. attr &= foregroundMask
  630. if (n-40)&1 != 0 {
  631. attr |= backgroundRed
  632. }
  633. if (n-40)&2 != 0 {
  634. attr |= backgroundGreen
  635. }
  636. if (n-40)&4 != 0 {
  637. attr |= backgroundBlue
  638. }
  639. case n == 48: // set background color.
  640. if i < len(token)-2 && token[i+1] == "5" {
  641. if n256, err := strconv.Atoi(token[i+2]); err == nil {
  642. if n256backAttr == nil {
  643. n256setup()
  644. }
  645. attr &= foregroundMask
  646. attr |= n256backAttr[n256]
  647. i += 2
  648. }
  649. } else if len(token) == 5 && token[i+1] == "2" {
  650. var r, g, b int
  651. r, _ = strconv.Atoi(token[i+2])
  652. g, _ = strconv.Atoi(token[i+3])
  653. b, _ = strconv.Atoi(token[i+4])
  654. i += 4
  655. if r > 127 {
  656. attr |= backgroundRed
  657. }
  658. if g > 127 {
  659. attr |= backgroundGreen
  660. }
  661. if b > 127 {
  662. attr |= backgroundBlue
  663. }
  664. } else {
  665. attr = attr & (w.oldattr & foregroundMask)
  666. }
  667. case n == 49: // reset foreground color.
  668. attr &= foregroundMask
  669. attr |= w.oldattr & backgroundMask
  670. case 90 <= n && n <= 97:
  671. attr = (attr & backgroundMask)
  672. attr |= foregroundIntensity
  673. if (n-90)&1 != 0 {
  674. attr |= foregroundRed
  675. }
  676. if (n-90)&2 != 0 {
  677. attr |= foregroundGreen
  678. }
  679. if (n-90)&4 != 0 {
  680. attr |= foregroundBlue
  681. }
  682. case 100 <= n && n <= 107:
  683. attr = (attr & foregroundMask)
  684. attr |= backgroundIntensity
  685. if (n-100)&1 != 0 {
  686. attr |= backgroundRed
  687. }
  688. if (n-100)&2 != 0 {
  689. attr |= backgroundGreen
  690. }
  691. if (n-100)&4 != 0 {
  692. attr |= backgroundBlue
  693. }
  694. }
  695. procSetConsoleTextAttribute.Call(uintptr(w.handle), uintptr(attr))
  696. }
  697. }
  698. case 'h':
  699. var ci consoleCursorInfo
  700. cs := buf.String()
  701. if cs == "5>" {
  702. procGetConsoleCursorInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&ci)))
  703. ci.visible = 0
  704. procSetConsoleCursorInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&ci)))
  705. } else if cs == "?25" {
  706. procGetConsoleCursorInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&ci)))
  707. ci.visible = 1
  708. procSetConsoleCursorInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&ci)))
  709. }
  710. case 'l':
  711. var ci consoleCursorInfo
  712. cs := buf.String()
  713. if cs == "5>" {
  714. procGetConsoleCursorInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&ci)))
  715. ci.visible = 1
  716. procSetConsoleCursorInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&ci)))
  717. } else if cs == "?25" {
  718. procGetConsoleCursorInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&ci)))
  719. ci.visible = 0
  720. procSetConsoleCursorInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&ci)))
  721. }
  722. case 's':
  723. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  724. w.oldpos = csbi.cursorPosition
  725. case 'u':
  726. procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&w.oldpos)))
  727. }
  728. }
  729. return len(data), nil
  730. }
  731. type consoleColor struct {
  732. rgb int
  733. red bool
  734. green bool
  735. blue bool
  736. intensity bool
  737. }
  738. func (c consoleColor) foregroundAttr() (attr word) {
  739. if c.red {
  740. attr |= foregroundRed
  741. }
  742. if c.green {
  743. attr |= foregroundGreen
  744. }
  745. if c.blue {
  746. attr |= foregroundBlue
  747. }
  748. if c.intensity {
  749. attr |= foregroundIntensity
  750. }
  751. return
  752. }
  753. func (c consoleColor) backgroundAttr() (attr word) {
  754. if c.red {
  755. attr |= backgroundRed
  756. }
  757. if c.green {
  758. attr |= backgroundGreen
  759. }
  760. if c.blue {
  761. attr |= backgroundBlue
  762. }
  763. if c.intensity {
  764. attr |= backgroundIntensity
  765. }
  766. return
  767. }
  768. var color16 = []consoleColor{
  769. {0x000000, false, false, false, false},
  770. {0x000080, false, false, true, false},
  771. {0x008000, false, true, false, false},
  772. {0x008080, false, true, true, false},
  773. {0x800000, true, false, false, false},
  774. {0x800080, true, false, true, false},
  775. {0x808000, true, true, false, false},
  776. {0xc0c0c0, true, true, true, false},
  777. {0x808080, false, false, false, true},
  778. {0x0000ff, false, false, true, true},
  779. {0x00ff00, false, true, false, true},
  780. {0x00ffff, false, true, true, true},
  781. {0xff0000, true, false, false, true},
  782. {0xff00ff, true, false, true, true},
  783. {0xffff00, true, true, false, true},
  784. {0xffffff, true, true, true, true},
  785. }
  786. type hsv struct {
  787. h, s, v float32
  788. }
  789. func (a hsv) dist(b hsv) float32 {
  790. dh := a.h - b.h
  791. switch {
  792. case dh > 0.5:
  793. dh = 1 - dh
  794. case dh < -0.5:
  795. dh = -1 - dh
  796. }
  797. ds := a.s - b.s
  798. dv := a.v - b.v
  799. return float32(math.Sqrt(float64(dh*dh + ds*ds + dv*dv)))
  800. }
  801. func toHSV(rgb int) hsv {
  802. r, g, b := float32((rgb&0xFF0000)>>16)/256.0,
  803. float32((rgb&0x00FF00)>>8)/256.0,
  804. float32(rgb&0x0000FF)/256.0
  805. min, max := minmax3f(r, g, b)
  806. h := max - min
  807. if h > 0 {
  808. if max == r {
  809. h = (g - b) / h
  810. if h < 0 {
  811. h += 6
  812. }
  813. } else if max == g {
  814. h = 2 + (b-r)/h
  815. } else {
  816. h = 4 + (r-g)/h
  817. }
  818. }
  819. h /= 6.0
  820. s := max - min
  821. if max != 0 {
  822. s /= max
  823. }
  824. v := max
  825. return hsv{h: h, s: s, v: v}
  826. }
  827. type hsvTable []hsv
  828. func toHSVTable(rgbTable []consoleColor) hsvTable {
  829. t := make(hsvTable, len(rgbTable))
  830. for i, c := range rgbTable {
  831. t[i] = toHSV(c.rgb)
  832. }
  833. return t
  834. }
  835. func (t hsvTable) find(rgb int) consoleColor {
  836. hsv := toHSV(rgb)
  837. n := 7
  838. l := float32(5.0)
  839. for i, p := range t {
  840. d := hsv.dist(p)
  841. if d < l {
  842. l, n = d, i
  843. }
  844. }
  845. return color16[n]
  846. }
  847. func minmax3f(a, b, c float32) (min, max float32) {
  848. if a < b {
  849. if b < c {
  850. return a, c
  851. } else if a < c {
  852. return a, b
  853. } else {
  854. return c, b
  855. }
  856. } else {
  857. if a < c {
  858. return b, c
  859. } else if b < c {
  860. return b, a
  861. } else {
  862. return c, a
  863. }
  864. }
  865. }
  866. var n256foreAttr []word
  867. var n256backAttr []word
  868. func n256setup() {
  869. n256foreAttr = make([]word, 256)
  870. n256backAttr = make([]word, 256)
  871. t := toHSVTable(color16)
  872. for i, rgb := range color256 {
  873. c := t.find(rgb)
  874. n256foreAttr[i] = c.foregroundAttr()
  875. n256backAttr[i] = c.backgroundAttr()
  876. }
  877. }