colorable_windows.go 22 KB

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