colorable_windows.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  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 provides 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 returns new instance of Writer which handles 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 returns new instance of Writer which handles escape sequence for stdout.
  94. func NewColorableStdout() io.Writer {
  95. return NewColorable(os.Stdout)
  96. }
  97. // NewColorableStderr returns new instance of Writer which handles 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 writes 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. if n < 1 {
  534. n = 1
  535. }
  536. procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))
  537. csbi.cursorPosition.x = short(n - 1)
  538. procSetConsoleCursorPosition.Call(uintptr(handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  539. case 'H', 'f':
  540. procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))
  541. if buf.Len() > 0 {
  542. token := strings.Split(buf.String(), ";")
  543. switch len(token) {
  544. case 1:
  545. n1, err := strconv.Atoi(token[0])
  546. if err != nil {
  547. continue
  548. }
  549. csbi.cursorPosition.y = short(n1 - 1)
  550. case 2:
  551. n1, err := strconv.Atoi(token[0])
  552. if err != nil {
  553. continue
  554. }
  555. n2, err := strconv.Atoi(token[1])
  556. if err != nil {
  557. continue
  558. }
  559. csbi.cursorPosition.x = short(n2 - 1)
  560. csbi.cursorPosition.y = short(n1 - 1)
  561. }
  562. } else {
  563. csbi.cursorPosition.y = 0
  564. }
  565. procSetConsoleCursorPosition.Call(uintptr(handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  566. case 'J':
  567. n := 0
  568. if buf.Len() > 0 {
  569. n, err = strconv.Atoi(buf.String())
  570. if err != nil {
  571. continue
  572. }
  573. }
  574. var count, written dword
  575. var cursor coord
  576. procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))
  577. switch n {
  578. case 0:
  579. cursor = coord{x: csbi.cursorPosition.x, y: csbi.cursorPosition.y}
  580. count = dword(csbi.size.x) - dword(csbi.cursorPosition.x) + dword(csbi.size.y-csbi.cursorPosition.y)*dword(csbi.size.x)
  581. case 1:
  582. cursor = coord{x: csbi.window.left, y: csbi.window.top}
  583. count = dword(csbi.size.x) - dword(csbi.cursorPosition.x) + dword(csbi.window.top-csbi.cursorPosition.y)*dword(csbi.size.x)
  584. case 2:
  585. cursor = coord{x: csbi.window.left, y: csbi.window.top}
  586. count = dword(csbi.size.x) - dword(csbi.cursorPosition.x) + dword(csbi.size.y-csbi.cursorPosition.y)*dword(csbi.size.x)
  587. }
  588. procFillConsoleOutputCharacter.Call(uintptr(handle), uintptr(' '), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  589. procFillConsoleOutputAttribute.Call(uintptr(handle), uintptr(csbi.attributes), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  590. case 'K':
  591. n := 0
  592. if buf.Len() > 0 {
  593. n, err = strconv.Atoi(buf.String())
  594. if err != nil {
  595. continue
  596. }
  597. }
  598. procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))
  599. var cursor coord
  600. var count, written dword
  601. switch n {
  602. case 0:
  603. cursor = coord{x: csbi.cursorPosition.x, y: csbi.cursorPosition.y}
  604. count = dword(csbi.size.x - csbi.cursorPosition.x)
  605. case 1:
  606. cursor = coord{x: csbi.window.left, y: csbi.cursorPosition.y}
  607. count = dword(csbi.size.x - csbi.cursorPosition.x)
  608. case 2:
  609. cursor = coord{x: csbi.window.left, y: csbi.cursorPosition.y}
  610. count = dword(csbi.size.x)
  611. }
  612. procFillConsoleOutputCharacter.Call(uintptr(handle), uintptr(' '), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  613. procFillConsoleOutputAttribute.Call(uintptr(handle), uintptr(csbi.attributes), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  614. case 'X':
  615. n := 0
  616. if buf.Len() > 0 {
  617. n, err = strconv.Atoi(buf.String())
  618. if err != nil {
  619. continue
  620. }
  621. }
  622. procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))
  623. var cursor coord
  624. var written dword
  625. cursor = coord{x: csbi.cursorPosition.x, y: csbi.cursorPosition.y}
  626. procFillConsoleOutputCharacter.Call(uintptr(handle), uintptr(' '), uintptr(n), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  627. procFillConsoleOutputAttribute.Call(uintptr(handle), uintptr(csbi.attributes), uintptr(n), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  628. case 'm':
  629. procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))
  630. attr := csbi.attributes
  631. cs := buf.String()
  632. if cs == "" {
  633. procSetConsoleTextAttribute.Call(uintptr(handle), uintptr(w.oldattr))
  634. continue
  635. }
  636. token := strings.Split(cs, ";")
  637. for i := 0; i < len(token); i++ {
  638. ns := token[i]
  639. if n, err = strconv.Atoi(ns); err == nil {
  640. switch {
  641. case n == 0 || n == 100:
  642. attr = w.oldattr
  643. case 1 <= n && n <= 5:
  644. attr |= foregroundIntensity
  645. case n == 7:
  646. attr = ((attr & foregroundMask) << 4) | ((attr & backgroundMask) >> 4)
  647. case n == 22 || n == 25:
  648. attr |= foregroundIntensity
  649. case n == 27:
  650. attr = ((attr & foregroundMask) << 4) | ((attr & backgroundMask) >> 4)
  651. case 30 <= n && n <= 37:
  652. attr &= backgroundMask
  653. if (n-30)&1 != 0 {
  654. attr |= foregroundRed
  655. }
  656. if (n-30)&2 != 0 {
  657. attr |= foregroundGreen
  658. }
  659. if (n-30)&4 != 0 {
  660. attr |= foregroundBlue
  661. }
  662. case n == 38: // set foreground color.
  663. if i < len(token)-2 && (token[i+1] == "5" || token[i+1] == "05") {
  664. if n256, err := strconv.Atoi(token[i+2]); err == nil {
  665. if n256foreAttr == nil {
  666. n256setup()
  667. }
  668. attr &= backgroundMask
  669. attr |= n256foreAttr[n256]
  670. i += 2
  671. }
  672. } else if len(token) == 5 && token[i+1] == "2" {
  673. var r, g, b int
  674. r, _ = strconv.Atoi(token[i+2])
  675. g, _ = strconv.Atoi(token[i+3])
  676. b, _ = strconv.Atoi(token[i+4])
  677. i += 4
  678. if r > 127 {
  679. attr |= foregroundRed
  680. }
  681. if g > 127 {
  682. attr |= foregroundGreen
  683. }
  684. if b > 127 {
  685. attr |= foregroundBlue
  686. }
  687. } else {
  688. attr = attr & (w.oldattr & backgroundMask)
  689. }
  690. case n == 39: // reset foreground color.
  691. attr &= backgroundMask
  692. attr |= w.oldattr & foregroundMask
  693. case 40 <= n && n <= 47:
  694. attr &= foregroundMask
  695. if (n-40)&1 != 0 {
  696. attr |= backgroundRed
  697. }
  698. if (n-40)&2 != 0 {
  699. attr |= backgroundGreen
  700. }
  701. if (n-40)&4 != 0 {
  702. attr |= backgroundBlue
  703. }
  704. case n == 48: // set background color.
  705. if i < len(token)-2 && token[i+1] == "5" {
  706. if n256, err := strconv.Atoi(token[i+2]); err == nil {
  707. if n256backAttr == nil {
  708. n256setup()
  709. }
  710. attr &= foregroundMask
  711. attr |= n256backAttr[n256]
  712. i += 2
  713. }
  714. } else if len(token) == 5 && token[i+1] == "2" {
  715. var r, g, b int
  716. r, _ = strconv.Atoi(token[i+2])
  717. g, _ = strconv.Atoi(token[i+3])
  718. b, _ = strconv.Atoi(token[i+4])
  719. i += 4
  720. if r > 127 {
  721. attr |= backgroundRed
  722. }
  723. if g > 127 {
  724. attr |= backgroundGreen
  725. }
  726. if b > 127 {
  727. attr |= backgroundBlue
  728. }
  729. } else {
  730. attr = attr & (w.oldattr & foregroundMask)
  731. }
  732. case n == 49: // reset foreground color.
  733. attr &= foregroundMask
  734. attr |= w.oldattr & backgroundMask
  735. case 90 <= n && n <= 97:
  736. attr = (attr & backgroundMask)
  737. attr |= foregroundIntensity
  738. if (n-90)&1 != 0 {
  739. attr |= foregroundRed
  740. }
  741. if (n-90)&2 != 0 {
  742. attr |= foregroundGreen
  743. }
  744. if (n-90)&4 != 0 {
  745. attr |= foregroundBlue
  746. }
  747. case 100 <= n && n <= 107:
  748. attr = (attr & foregroundMask)
  749. attr |= backgroundIntensity
  750. if (n-100)&1 != 0 {
  751. attr |= backgroundRed
  752. }
  753. if (n-100)&2 != 0 {
  754. attr |= backgroundGreen
  755. }
  756. if (n-100)&4 != 0 {
  757. attr |= backgroundBlue
  758. }
  759. }
  760. procSetConsoleTextAttribute.Call(uintptr(handle), uintptr(attr))
  761. }
  762. }
  763. case 'h':
  764. var ci consoleCursorInfo
  765. cs := buf.String()
  766. if cs == "5>" {
  767. procGetConsoleCursorInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&ci)))
  768. ci.visible = 0
  769. procSetConsoleCursorInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&ci)))
  770. } else if cs == "?25" {
  771. procGetConsoleCursorInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&ci)))
  772. ci.visible = 1
  773. procSetConsoleCursorInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&ci)))
  774. } else if cs == "?1049" {
  775. if w.althandle == 0 {
  776. h, _, _ := procCreateConsoleScreenBuffer.Call(uintptr(genericRead|genericWrite), 0, 0, uintptr(consoleTextmodeBuffer), 0, 0)
  777. w.althandle = syscall.Handle(h)
  778. if w.althandle != 0 {
  779. handle = w.althandle
  780. }
  781. }
  782. }
  783. case 'l':
  784. var ci consoleCursorInfo
  785. cs := buf.String()
  786. if cs == "5>" {
  787. procGetConsoleCursorInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&ci)))
  788. ci.visible = 1
  789. procSetConsoleCursorInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&ci)))
  790. } else if cs == "?25" {
  791. procGetConsoleCursorInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&ci)))
  792. ci.visible = 0
  793. procSetConsoleCursorInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&ci)))
  794. } else if cs == "?1049" {
  795. if w.althandle != 0 {
  796. syscall.CloseHandle(w.althandle)
  797. w.althandle = 0
  798. handle = w.handle
  799. }
  800. }
  801. case 's':
  802. procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))
  803. w.oldpos = csbi.cursorPosition
  804. case 'u':
  805. procSetConsoleCursorPosition.Call(uintptr(handle), *(*uintptr)(unsafe.Pointer(&w.oldpos)))
  806. }
  807. }
  808. return len(data), nil
  809. }
  810. type consoleColor struct {
  811. rgb int
  812. red bool
  813. green bool
  814. blue bool
  815. intensity bool
  816. }
  817. func (c consoleColor) foregroundAttr() (attr word) {
  818. if c.red {
  819. attr |= foregroundRed
  820. }
  821. if c.green {
  822. attr |= foregroundGreen
  823. }
  824. if c.blue {
  825. attr |= foregroundBlue
  826. }
  827. if c.intensity {
  828. attr |= foregroundIntensity
  829. }
  830. return
  831. }
  832. func (c consoleColor) backgroundAttr() (attr word) {
  833. if c.red {
  834. attr |= backgroundRed
  835. }
  836. if c.green {
  837. attr |= backgroundGreen
  838. }
  839. if c.blue {
  840. attr |= backgroundBlue
  841. }
  842. if c.intensity {
  843. attr |= backgroundIntensity
  844. }
  845. return
  846. }
  847. var color16 = []consoleColor{
  848. {0x000000, false, false, false, false},
  849. {0x000080, false, false, true, false},
  850. {0x008000, false, true, false, false},
  851. {0x008080, false, true, true, false},
  852. {0x800000, true, false, false, false},
  853. {0x800080, true, false, true, false},
  854. {0x808000, true, true, false, false},
  855. {0xc0c0c0, true, true, true, false},
  856. {0x808080, false, false, false, true},
  857. {0x0000ff, false, false, true, true},
  858. {0x00ff00, false, true, false, true},
  859. {0x00ffff, false, true, true, true},
  860. {0xff0000, true, false, false, true},
  861. {0xff00ff, true, false, true, true},
  862. {0xffff00, true, true, false, true},
  863. {0xffffff, true, true, true, true},
  864. }
  865. type hsv struct {
  866. h, s, v float32
  867. }
  868. func (a hsv) dist(b hsv) float32 {
  869. dh := a.h - b.h
  870. switch {
  871. case dh > 0.5:
  872. dh = 1 - dh
  873. case dh < -0.5:
  874. dh = -1 - dh
  875. }
  876. ds := a.s - b.s
  877. dv := a.v - b.v
  878. return float32(math.Sqrt(float64(dh*dh + ds*ds + dv*dv)))
  879. }
  880. func toHSV(rgb int) hsv {
  881. r, g, b := float32((rgb&0xFF0000)>>16)/256.0,
  882. float32((rgb&0x00FF00)>>8)/256.0,
  883. float32(rgb&0x0000FF)/256.0
  884. min, max := minmax3f(r, g, b)
  885. h := max - min
  886. if h > 0 {
  887. if max == r {
  888. h = (g - b) / h
  889. if h < 0 {
  890. h += 6
  891. }
  892. } else if max == g {
  893. h = 2 + (b-r)/h
  894. } else {
  895. h = 4 + (r-g)/h
  896. }
  897. }
  898. h /= 6.0
  899. s := max - min
  900. if max != 0 {
  901. s /= max
  902. }
  903. v := max
  904. return hsv{h: h, s: s, v: v}
  905. }
  906. type hsvTable []hsv
  907. func toHSVTable(rgbTable []consoleColor) hsvTable {
  908. t := make(hsvTable, len(rgbTable))
  909. for i, c := range rgbTable {
  910. t[i] = toHSV(c.rgb)
  911. }
  912. return t
  913. }
  914. func (t hsvTable) find(rgb int) consoleColor {
  915. hsv := toHSV(rgb)
  916. n := 7
  917. l := float32(5.0)
  918. for i, p := range t {
  919. d := hsv.dist(p)
  920. if d < l {
  921. l, n = d, i
  922. }
  923. }
  924. return color16[n]
  925. }
  926. func minmax3f(a, b, c float32) (min, max float32) {
  927. if a < b {
  928. if b < c {
  929. return a, c
  930. } else if a < c {
  931. return a, b
  932. } else {
  933. return c, b
  934. }
  935. } else {
  936. if a < c {
  937. return b, c
  938. } else if b < c {
  939. return b, a
  940. } else {
  941. return c, a
  942. }
  943. }
  944. }
  945. var n256foreAttr []word
  946. var n256backAttr []word
  947. func n256setup() {
  948. n256foreAttr = make([]word, 256)
  949. n256backAttr = make([]word, 256)
  950. t := toHSVTable(color16)
  951. for i, rgb := range color256 {
  952. c := t.find(rgb)
  953. n256foreAttr[i] = c.foregroundAttr()
  954. n256backAttr[i] = c.backgroundAttr()
  955. }
  956. }