colorable_windows.go 22 KB

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