colorable_windows.go 23 KB

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