colorable_windows.go 24 KB

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