You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

colorable_windows.go 23KB

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