colorable_windows.go 23 KB

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