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.
 
 
 
 
 
 

1044 lines
24 KiB

  1. // +build windows
  2. // +build !appengine
  3. package colorable
  4. import (
  5. "bytes"
  6. "io"
  7. "math"
  8. "os"
  9. "strconv"
  10. "strings"
  11. "sync"
  12. "syscall"
  13. "unsafe"
  14. "github.com/mattn/go-isatty"
  15. )
  16. const (
  17. foregroundBlue = 0x1
  18. foregroundGreen = 0x2
  19. foregroundRed = 0x4
  20. foregroundIntensity = 0x8
  21. foregroundMask = (foregroundRed | foregroundBlue | foregroundGreen | foregroundIntensity)
  22. backgroundBlue = 0x10
  23. backgroundGreen = 0x20
  24. backgroundRed = 0x40
  25. backgroundIntensity = 0x80
  26. backgroundMask = (backgroundRed | backgroundBlue | backgroundGreen | backgroundIntensity)
  27. commonLvbUnderscore = 0x8000
  28. cENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x4
  29. )
  30. const (
  31. genericRead = 0x80000000
  32. genericWrite = 0x40000000
  33. )
  34. const (
  35. consoleTextmodeBuffer = 0x1
  36. )
  37. type wchar uint16
  38. type short int16
  39. type dword uint32
  40. type word uint16
  41. type coord struct {
  42. x short
  43. y short
  44. }
  45. type smallRect struct {
  46. left short
  47. top short
  48. right short
  49. bottom short
  50. }
  51. type consoleScreenBufferInfo struct {
  52. size coord
  53. cursorPosition coord
  54. attributes word
  55. window smallRect
  56. maximumWindowSize coord
  57. }
  58. type consoleCursorInfo struct {
  59. size dword
  60. visible int32
  61. }
  62. var (
  63. kernel32 = syscall.NewLazyDLL("kernel32.dll")
  64. procGetConsoleScreenBufferInfo = kernel32.NewProc("GetConsoleScreenBufferInfo")
  65. procSetConsoleTextAttribute = kernel32.NewProc("SetConsoleTextAttribute")
  66. procSetConsoleCursorPosition = kernel32.NewProc("SetConsoleCursorPosition")
  67. procFillConsoleOutputCharacter = kernel32.NewProc("FillConsoleOutputCharacterW")
  68. procFillConsoleOutputAttribute = kernel32.NewProc("FillConsoleOutputAttribute")
  69. procGetConsoleCursorInfo = kernel32.NewProc("GetConsoleCursorInfo")
  70. procSetConsoleCursorInfo = kernel32.NewProc("SetConsoleCursorInfo")
  71. procSetConsoleTitle = kernel32.NewProc("SetConsoleTitleW")
  72. procGetConsoleMode = kernel32.NewProc("GetConsoleMode")
  73. procSetConsoleMode = kernel32.NewProc("SetConsoleMode")
  74. procCreateConsoleScreenBuffer = kernel32.NewProc("CreateConsoleScreenBuffer")
  75. )
  76. // Writer provides colorable Writer to the console
  77. type Writer struct {
  78. out io.Writer
  79. handle syscall.Handle
  80. althandle syscall.Handle
  81. oldattr word
  82. oldpos coord
  83. rest bytes.Buffer
  84. mutex sync.Mutex
  85. }
  86. // NewColorable returns new instance of Writer which handles escape sequence from File.
  87. func NewColorable(file *os.File) io.Writer {
  88. if file == nil {
  89. panic("nil passed instead of *os.File to NewColorable()")
  90. }
  91. if isatty.IsTerminal(file.Fd()) {
  92. var mode uint32
  93. if r, _, _ := procGetConsoleMode.Call(file.Fd(), uintptr(unsafe.Pointer(&mode))); r != 0 && mode&cENABLE_VIRTUAL_TERMINAL_PROCESSING != 0 {
  94. return file
  95. }
  96. var csbi consoleScreenBufferInfo
  97. handle := syscall.Handle(file.Fd())
  98. procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))
  99. return &Writer{out: file, handle: handle, oldattr: csbi.attributes, oldpos: coord{0, 0}}
  100. }
  101. return file
  102. }
  103. // NewColorableStdout returns new instance of Writer which handles escape sequence for stdout.
  104. func NewColorableStdout() io.Writer {
  105. return NewColorable(os.Stdout)
  106. }
  107. // NewColorableStderr returns new instance of Writer which handles escape sequence for stderr.
  108. func NewColorableStderr() io.Writer {
  109. return NewColorable(os.Stderr)
  110. }
  111. var color256 = map[int]int{
  112. 0: 0x000000,
  113. 1: 0x800000,
  114. 2: 0x008000,
  115. 3: 0x808000,
  116. 4: 0x000080,
  117. 5: 0x800080,
  118. 6: 0x008080,
  119. 7: 0xc0c0c0,
  120. 8: 0x808080,
  121. 9: 0xff0000,
  122. 10: 0x00ff00,
  123. 11: 0xffff00,
  124. 12: 0x0000ff,
  125. 13: 0xff00ff,
  126. 14: 0x00ffff,
  127. 15: 0xffffff,
  128. 16: 0x000000,
  129. 17: 0x00005f,
  130. 18: 0x000087,
  131. 19: 0x0000af,
  132. 20: 0x0000d7,
  133. 21: 0x0000ff,
  134. 22: 0x005f00,
  135. 23: 0x005f5f,
  136. 24: 0x005f87,
  137. 25: 0x005faf,
  138. 26: 0x005fd7,
  139. 27: 0x005fff,
  140. 28: 0x008700,
  141. 29: 0x00875f,
  142. 30: 0x008787,
  143. 31: 0x0087af,
  144. 32: 0x0087d7,
  145. 33: 0x0087ff,
  146. 34: 0x00af00,
  147. 35: 0x00af5f,
  148. 36: 0x00af87,
  149. 37: 0x00afaf,
  150. 38: 0x00afd7,
  151. 39: 0x00afff,
  152. 40: 0x00d700,
  153. 41: 0x00d75f,
  154. 42: 0x00d787,
  155. 43: 0x00d7af,
  156. 44: 0x00d7d7,
  157. 45: 0x00d7ff,
  158. 46: 0x00ff00,
  159. 47: 0x00ff5f,
  160. 48: 0x00ff87,
  161. 49: 0x00ffaf,
  162. 50: 0x00ffd7,
  163. 51: 0x00ffff,
  164. 52: 0x5f0000,
  165. 53: 0x5f005f,
  166. 54: 0x5f0087,
  167. 55: 0x5f00af,
  168. 56: 0x5f00d7,
  169. 57: 0x5f00ff,
  170. 58: 0x5f5f00,
  171. 59: 0x5f5f5f,
  172. 60: 0x5f5f87,
  173. 61: 0x5f5faf,
  174. 62: 0x5f5fd7,
  175. 63: 0x5f5fff,
  176. 64: 0x5f8700,
  177. 65: 0x5f875f,
  178. 66: 0x5f8787,
  179. 67: 0x5f87af,
  180. 68: 0x5f87d7,
  181. 69: 0x5f87ff,
  182. 70: 0x5faf00,
  183. 71: 0x5faf5f,
  184. 72: 0x5faf87,
  185. 73: 0x5fafaf,
  186. 74: 0x5fafd7,
  187. 75: 0x5fafff,
  188. 76: 0x5fd700,
  189. 77: 0x5fd75f,
  190. 78: 0x5fd787,
  191. 79: 0x5fd7af,
  192. 80: 0x5fd7d7,
  193. 81: 0x5fd7ff,
  194. 82: 0x5fff00,
  195. 83: 0x5fff5f,
  196. 84: 0x5fff87,
  197. 85: 0x5fffaf,
  198. 86: 0x5fffd7,
  199. 87: 0x5fffff,
  200. 88: 0x870000,
  201. 89: 0x87005f,
  202. 90: 0x870087,
  203. 91: 0x8700af,
  204. 92: 0x8700d7,
  205. 93: 0x8700ff,
  206. 94: 0x875f00,
  207. 95: 0x875f5f,
  208. 96: 0x875f87,
  209. 97: 0x875faf,
  210. 98: 0x875fd7,
  211. 99: 0x875fff,
  212. 100: 0x878700,
  213. 101: 0x87875f,
  214. 102: 0x878787,
  215. 103: 0x8787af,
  216. 104: 0x8787d7,
  217. 105: 0x8787ff,
  218. 106: 0x87af00,
  219. 107: 0x87af5f,
  220. 108: 0x87af87,
  221. 109: 0x87afaf,
  222. 110: 0x87afd7,
  223. 111: 0x87afff,
  224. 112: 0x87d700,
  225. 113: 0x87d75f,
  226. 114: 0x87d787,
  227. 115: 0x87d7af,
  228. 116: 0x87d7d7,
  229. 117: 0x87d7ff,
  230. 118: 0x87ff00,
  231. 119: 0x87ff5f,
  232. 120: 0x87ff87,
  233. 121: 0x87ffaf,
  234. 122: 0x87ffd7,
  235. 123: 0x87ffff,
  236. 124: 0xaf0000,
  237. 125: 0xaf005f,
  238. 126: 0xaf0087,
  239. 127: 0xaf00af,
  240. 128: 0xaf00d7,
  241. 129: 0xaf00ff,
  242. 130: 0xaf5f00,
  243. 131: 0xaf5f5f,
  244. 132: 0xaf5f87,
  245. 133: 0xaf5faf,
  246. 134: 0xaf5fd7,
  247. 135: 0xaf5fff,
  248. 136: 0xaf8700,
  249. 137: 0xaf875f,
  250. 138: 0xaf8787,
  251. 139: 0xaf87af,
  252. 140: 0xaf87d7,
  253. 141: 0xaf87ff,
  254. 142: 0xafaf00,
  255. 143: 0xafaf5f,
  256. 144: 0xafaf87,
  257. 145: 0xafafaf,
  258. 146: 0xafafd7,
  259. 147: 0xafafff,
  260. 148: 0xafd700,
  261. 149: 0xafd75f,
  262. 150: 0xafd787,
  263. 151: 0xafd7af,
  264. 152: 0xafd7d7,
  265. 153: 0xafd7ff,
  266. 154: 0xafff00,
  267. 155: 0xafff5f,
  268. 156: 0xafff87,
  269. 157: 0xafffaf,
  270. 158: 0xafffd7,
  271. 159: 0xafffff,
  272. 160: 0xd70000,
  273. 161: 0xd7005f,
  274. 162: 0xd70087,
  275. 163: 0xd700af,
  276. 164: 0xd700d7,
  277. 165: 0xd700ff,
  278. 166: 0xd75f00,
  279. 167: 0xd75f5f,
  280. 168: 0xd75f87,
  281. 169: 0xd75faf,
  282. 170: 0xd75fd7,
  283. 171: 0xd75fff,
  284. 172: 0xd78700,
  285. 173: 0xd7875f,
  286. 174: 0xd78787,
  287. 175: 0xd787af,
  288. 176: 0xd787d7,
  289. 177: 0xd787ff,
  290. 178: 0xd7af00,
  291. 179: 0xd7af5f,
  292. 180: 0xd7af87,
  293. 181: 0xd7afaf,
  294. 182: 0xd7afd7,
  295. 183: 0xd7afff,
  296. 184: 0xd7d700,
  297. 185: 0xd7d75f,
  298. 186: 0xd7d787,
  299. 187: 0xd7d7af,
  300. 188: 0xd7d7d7,
  301. 189: 0xd7d7ff,
  302. 190: 0xd7ff00,
  303. 191: 0xd7ff5f,
  304. 192: 0xd7ff87,
  305. 193: 0xd7ffaf,
  306. 194: 0xd7ffd7,
  307. 195: 0xd7ffff,
  308. 196: 0xff0000,
  309. 197: 0xff005f,
  310. 198: 0xff0087,
  311. 199: 0xff00af,
  312. 200: 0xff00d7,
  313. 201: 0xff00ff,
  314. 202: 0xff5f00,
  315. 203: 0xff5f5f,
  316. 204: 0xff5f87,
  317. 205: 0xff5faf,
  318. 206: 0xff5fd7,
  319. 207: 0xff5fff,
  320. 208: 0xff8700,
  321. 209: 0xff875f,
  322. 210: 0xff8787,
  323. 211: 0xff87af,
  324. 212: 0xff87d7,
  325. 213: 0xff87ff,
  326. 214: 0xffaf00,
  327. 215: 0xffaf5f,
  328. 216: 0xffaf87,
  329. 217: 0xffafaf,
  330. 218: 0xffafd7,
  331. 219: 0xffafff,
  332. 220: 0xffd700,
  333. 221: 0xffd75f,
  334. 222: 0xffd787,
  335. 223: 0xffd7af,
  336. 224: 0xffd7d7,
  337. 225: 0xffd7ff,
  338. 226: 0xffff00,
  339. 227: 0xffff5f,
  340. 228: 0xffff87,
  341. 229: 0xffffaf,
  342. 230: 0xffffd7,
  343. 231: 0xffffff,
  344. 232: 0x080808,
  345. 233: 0x121212,
  346. 234: 0x1c1c1c,
  347. 235: 0x262626,
  348. 236: 0x303030,
  349. 237: 0x3a3a3a,
  350. 238: 0x444444,
  351. 239: 0x4e4e4e,
  352. 240: 0x585858,
  353. 241: 0x626262,
  354. 242: 0x6c6c6c,
  355. 243: 0x767676,
  356. 244: 0x808080,
  357. 245: 0x8a8a8a,
  358. 246: 0x949494,
  359. 247: 0x9e9e9e,
  360. 248: 0xa8a8a8,
  361. 249: 0xb2b2b2,
  362. 250: 0xbcbcbc,
  363. 251: 0xc6c6c6,
  364. 252: 0xd0d0d0,
  365. 253: 0xdadada,
  366. 254: 0xe4e4e4,
  367. 255: 0xeeeeee,
  368. }
  369. // `\033]0;TITLESTR\007`
  370. func doTitleSequence(er *bytes.Reader) error {
  371. var c byte
  372. var err error
  373. c, err = er.ReadByte()
  374. if err != nil {
  375. return err
  376. }
  377. if c != '0' && c != '2' {
  378. return nil
  379. }
  380. c, err = er.ReadByte()
  381. if err != nil {
  382. return err
  383. }
  384. if c != ';' {
  385. return nil
  386. }
  387. title := make([]byte, 0, 80)
  388. for {
  389. c, err = er.ReadByte()
  390. if err != nil {
  391. return err
  392. }
  393. if c == 0x07 || c == '\n' {
  394. break
  395. }
  396. title = append(title, c)
  397. }
  398. if len(title) > 0 {
  399. title8, err := syscall.UTF16PtrFromString(string(title))
  400. if err == nil {
  401. procSetConsoleTitle.Call(uintptr(unsafe.Pointer(title8)))
  402. }
  403. }
  404. return nil
  405. }
  406. // returns Atoi(s) unless s == "" in which case it returns def
  407. func atoiWithDefault(s string, def int) (int, error) {
  408. if s == "" {
  409. return def, nil
  410. }
  411. return strconv.Atoi(s)
  412. }
  413. // Write writes data on console
  414. func (w *Writer) Write(data []byte) (n int, err error) {
  415. w.mutex.Lock()
  416. defer w.mutex.Unlock()
  417. var csbi consoleScreenBufferInfo
  418. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  419. handle := w.handle
  420. var er *bytes.Reader
  421. if w.rest.Len() > 0 {
  422. var rest bytes.Buffer
  423. w.rest.WriteTo(&rest)
  424. w.rest.Reset()
  425. rest.Write(data)
  426. er = bytes.NewReader(rest.Bytes())
  427. } else {
  428. er = bytes.NewReader(data)
  429. }
  430. var bw [1]byte
  431. loop:
  432. for {
  433. c1, err := er.ReadByte()
  434. if err != nil {
  435. break loop
  436. }
  437. if c1 != 0x1b {
  438. bw[0] = c1
  439. w.out.Write(bw[:])
  440. continue
  441. }
  442. c2, err := er.ReadByte()
  443. if err != nil {
  444. break loop
  445. }
  446. switch c2 {
  447. case '>':
  448. continue
  449. case ']':
  450. w.rest.WriteByte(c1)
  451. w.rest.WriteByte(c2)
  452. er.WriteTo(&w.rest)
  453. if bytes.IndexByte(w.rest.Bytes(), 0x07) == -1 {
  454. break loop
  455. }
  456. er = bytes.NewReader(w.rest.Bytes()[2:])
  457. err := doTitleSequence(er)
  458. if err != nil {
  459. break loop
  460. }
  461. w.rest.Reset()
  462. continue
  463. // https://github.com/mattn/go-colorable/issues/27
  464. case '7':
  465. procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))
  466. w.oldpos = csbi.cursorPosition
  467. continue
  468. case '8':
  469. procSetConsoleCursorPosition.Call(uintptr(handle), *(*uintptr)(unsafe.Pointer(&w.oldpos)))
  470. continue
  471. case 0x5b:
  472. // execute part after switch
  473. default:
  474. continue
  475. }
  476. w.rest.WriteByte(c1)
  477. w.rest.WriteByte(c2)
  478. er.WriteTo(&w.rest)
  479. var buf bytes.Buffer
  480. var m byte
  481. for i, c := range w.rest.Bytes()[2:] {
  482. if ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '@' {
  483. m = c
  484. er = bytes.NewReader(w.rest.Bytes()[2+i+1:])
  485. w.rest.Reset()
  486. break
  487. }
  488. buf.Write([]byte(string(c)))
  489. }
  490. if m == 0 {
  491. break loop
  492. }
  493. switch m {
  494. case 'A':
  495. n, err = atoiWithDefault(buf.String(), 1)
  496. if err != nil {
  497. continue
  498. }
  499. procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))
  500. csbi.cursorPosition.y -= short(n)
  501. procSetConsoleCursorPosition.Call(uintptr(handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  502. case 'B':
  503. n, err = atoiWithDefault(buf.String(), 1)
  504. if err != nil {
  505. continue
  506. }
  507. procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))
  508. csbi.cursorPosition.y += short(n)
  509. procSetConsoleCursorPosition.Call(uintptr(handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  510. case 'C':
  511. n, err = atoiWithDefault(buf.String(), 1)
  512. if err != nil {
  513. continue
  514. }
  515. procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))
  516. csbi.cursorPosition.x += short(n)
  517. procSetConsoleCursorPosition.Call(uintptr(handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  518. case 'D':
  519. n, err = atoiWithDefault(buf.String(), 1)
  520. if err != nil {
  521. continue
  522. }
  523. procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))
  524. csbi.cursorPosition.x -= short(n)
  525. if csbi.cursorPosition.x < 0 {
  526. csbi.cursorPosition.x = 0
  527. }
  528. procSetConsoleCursorPosition.Call(uintptr(handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  529. case 'E':
  530. n, err = strconv.Atoi(buf.String())
  531. if err != nil {
  532. continue
  533. }
  534. procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))
  535. csbi.cursorPosition.x = 0
  536. csbi.cursorPosition.y += short(n)
  537. procSetConsoleCursorPosition.Call(uintptr(handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  538. case 'F':
  539. n, err = strconv.Atoi(buf.String())
  540. if err != nil {
  541. continue
  542. }
  543. procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))
  544. csbi.cursorPosition.x = 0
  545. csbi.cursorPosition.y -= short(n)
  546. procSetConsoleCursorPosition.Call(uintptr(handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  547. case 'G':
  548. n, err = strconv.Atoi(buf.String())
  549. if err != nil {
  550. continue
  551. }
  552. if n < 1 {
  553. n = 1
  554. }
  555. procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))
  556. csbi.cursorPosition.x = short(n - 1)
  557. procSetConsoleCursorPosition.Call(uintptr(handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  558. case 'H', 'f':
  559. procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))
  560. if buf.Len() > 0 {
  561. token := strings.Split(buf.String(), ";")
  562. switch len(token) {
  563. case 1:
  564. n1, err := strconv.Atoi(token[0])
  565. if err != nil {
  566. continue
  567. }
  568. csbi.cursorPosition.y = short(n1 - 1)
  569. case 2:
  570. n1, err := strconv.Atoi(token[0])
  571. if err != nil {
  572. continue
  573. }
  574. n2, err := strconv.Atoi(token[1])
  575. if err != nil {
  576. continue
  577. }
  578. csbi.cursorPosition.x = short(n2 - 1)
  579. csbi.cursorPosition.y = short(n1 - 1)
  580. }
  581. } else {
  582. csbi.cursorPosition.y = 0
  583. }
  584. procSetConsoleCursorPosition.Call(uintptr(handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  585. case 'J':
  586. n := 0
  587. if buf.Len() > 0 {
  588. n, err = strconv.Atoi(buf.String())
  589. if err != nil {
  590. continue
  591. }
  592. }
  593. var count, written dword
  594. var cursor coord
  595. procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))
  596. switch n {
  597. case 0:
  598. cursor = coord{x: csbi.cursorPosition.x, y: csbi.cursorPosition.y}
  599. count = dword(csbi.size.x) - dword(csbi.cursorPosition.x) + dword(csbi.size.y-csbi.cursorPosition.y)*dword(csbi.size.x)
  600. case 1:
  601. cursor = coord{x: csbi.window.left, y: csbi.window.top}
  602. count = dword(csbi.size.x) - dword(csbi.cursorPosition.x) + dword(csbi.window.top-csbi.cursorPosition.y)*dword(csbi.size.x)
  603. case 2:
  604. cursor = coord{x: csbi.window.left, y: csbi.window.top}
  605. count = dword(csbi.size.x) - dword(csbi.cursorPosition.x) + dword(csbi.size.y-csbi.cursorPosition.y)*dword(csbi.size.x)
  606. }
  607. procFillConsoleOutputCharacter.Call(uintptr(handle), uintptr(' '), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  608. procFillConsoleOutputAttribute.Call(uintptr(handle), uintptr(csbi.attributes), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  609. case 'K':
  610. n := 0
  611. if buf.Len() > 0 {
  612. n, err = strconv.Atoi(buf.String())
  613. if err != nil {
  614. continue
  615. }
  616. }
  617. procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))
  618. var cursor coord
  619. var count, written dword
  620. switch n {
  621. case 0:
  622. cursor = coord{x: csbi.cursorPosition.x, y: csbi.cursorPosition.y}
  623. count = dword(csbi.size.x - csbi.cursorPosition.x)
  624. case 1:
  625. cursor = coord{x: csbi.window.left, y: csbi.cursorPosition.y}
  626. count = dword(csbi.size.x - csbi.cursorPosition.x)
  627. case 2:
  628. cursor = coord{x: csbi.window.left, y: csbi.cursorPosition.y}
  629. count = dword(csbi.size.x)
  630. }
  631. procFillConsoleOutputCharacter.Call(uintptr(handle), uintptr(' '), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  632. procFillConsoleOutputAttribute.Call(uintptr(handle), uintptr(csbi.attributes), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  633. case 'X':
  634. n := 0
  635. if buf.Len() > 0 {
  636. n, err = strconv.Atoi(buf.String())
  637. if err != nil {
  638. continue
  639. }
  640. }
  641. procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))
  642. var cursor coord
  643. var written dword
  644. cursor = coord{x: csbi.cursorPosition.x, y: csbi.cursorPosition.y}
  645. procFillConsoleOutputCharacter.Call(uintptr(handle), uintptr(' '), uintptr(n), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  646. procFillConsoleOutputAttribute.Call(uintptr(handle), uintptr(csbi.attributes), uintptr(n), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  647. case 'm':
  648. procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))
  649. attr := csbi.attributes
  650. cs := buf.String()
  651. if cs == "" {
  652. procSetConsoleTextAttribute.Call(uintptr(handle), uintptr(w.oldattr))
  653. continue
  654. }
  655. token := strings.Split(cs, ";")
  656. for i := 0; i < len(token); i++ {
  657. ns := token[i]
  658. if n, err = strconv.Atoi(ns); err == nil {
  659. switch {
  660. case n == 0 || n == 100:
  661. attr = w.oldattr
  662. case n == 4:
  663. attr |= commonLvbUnderscore
  664. case (1 <= n && n <= 3) || n == 5:
  665. attr |= foregroundIntensity
  666. case n == 7 || n == 27:
  667. attr =
  668. (attr &^ (foregroundMask | backgroundMask)) |
  669. ((attr & foregroundMask) << 4) |
  670. ((attr & backgroundMask) >> 4)
  671. case n == 22:
  672. attr &^= foregroundIntensity
  673. case n == 24:
  674. attr &^= commonLvbUnderscore
  675. case 30 <= n && n <= 37:
  676. attr &= backgroundMask
  677. if (n-30)&1 != 0 {
  678. attr |= foregroundRed
  679. }
  680. if (n-30)&2 != 0 {
  681. attr |= foregroundGreen
  682. }
  683. if (n-30)&4 != 0 {
  684. attr |= foregroundBlue
  685. }
  686. case n == 38: // set foreground color.
  687. if i < len(token)-2 && (token[i+1] == "5" || token[i+1] == "05") {
  688. if n256, err := strconv.Atoi(token[i+2]); err == nil {
  689. if n256foreAttr == nil {
  690. n256setup()
  691. }
  692. attr &= backgroundMask
  693. attr |= n256foreAttr[n256]
  694. i += 2
  695. }
  696. } else if len(token) == 5 && token[i+1] == "2" {
  697. var r, g, b int
  698. r, _ = strconv.Atoi(token[i+2])
  699. g, _ = strconv.Atoi(token[i+3])
  700. b, _ = strconv.Atoi(token[i+4])
  701. i += 4
  702. if r > 127 {
  703. attr |= foregroundRed
  704. }
  705. if g > 127 {
  706. attr |= foregroundGreen
  707. }
  708. if b > 127 {
  709. attr |= foregroundBlue
  710. }
  711. } else {
  712. attr = attr & (w.oldattr & backgroundMask)
  713. }
  714. case n == 39: // reset foreground color.
  715. attr &= backgroundMask
  716. attr |= w.oldattr & foregroundMask
  717. case 40 <= n && n <= 47:
  718. attr &= foregroundMask
  719. if (n-40)&1 != 0 {
  720. attr |= backgroundRed
  721. }
  722. if (n-40)&2 != 0 {
  723. attr |= backgroundGreen
  724. }
  725. if (n-40)&4 != 0 {
  726. attr |= backgroundBlue
  727. }
  728. case n == 48: // set background color.
  729. if i < len(token)-2 && token[i+1] == "5" {
  730. if n256, err := strconv.Atoi(token[i+2]); err == nil {
  731. if n256backAttr == nil {
  732. n256setup()
  733. }
  734. attr &= foregroundMask
  735. attr |= n256backAttr[n256]
  736. i += 2
  737. }
  738. } else if len(token) == 5 && token[i+1] == "2" {
  739. var r, g, b int
  740. r, _ = strconv.Atoi(token[i+2])
  741. g, _ = strconv.Atoi(token[i+3])
  742. b, _ = strconv.Atoi(token[i+4])
  743. i += 4
  744. if r > 127 {
  745. attr |= backgroundRed
  746. }
  747. if g > 127 {
  748. attr |= backgroundGreen
  749. }
  750. if b > 127 {
  751. attr |= backgroundBlue
  752. }
  753. } else {
  754. attr = attr & (w.oldattr & foregroundMask)
  755. }
  756. case n == 49: // reset foreground color.
  757. attr &= foregroundMask
  758. attr |= w.oldattr & backgroundMask
  759. case 90 <= n && n <= 97:
  760. attr = (attr & backgroundMask)
  761. attr |= foregroundIntensity
  762. if (n-90)&1 != 0 {
  763. attr |= foregroundRed
  764. }
  765. if (n-90)&2 != 0 {
  766. attr |= foregroundGreen
  767. }
  768. if (n-90)&4 != 0 {
  769. attr |= foregroundBlue
  770. }
  771. case 100 <= n && n <= 107:
  772. attr = (attr & foregroundMask)
  773. attr |= backgroundIntensity
  774. if (n-100)&1 != 0 {
  775. attr |= backgroundRed
  776. }
  777. if (n-100)&2 != 0 {
  778. attr |= backgroundGreen
  779. }
  780. if (n-100)&4 != 0 {
  781. attr |= backgroundBlue
  782. }
  783. }
  784. procSetConsoleTextAttribute.Call(uintptr(handle), uintptr(attr))
  785. }
  786. }
  787. case 'h':
  788. var ci consoleCursorInfo
  789. cs := buf.String()
  790. if cs == "5>" {
  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 == "?25" {
  795. procGetConsoleCursorInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&ci)))
  796. ci.visible = 1
  797. procSetConsoleCursorInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&ci)))
  798. } else if cs == "?1049" {
  799. if w.althandle == 0 {
  800. h, _, _ := procCreateConsoleScreenBuffer.Call(uintptr(genericRead|genericWrite), 0, 0, uintptr(consoleTextmodeBuffer), 0, 0)
  801. w.althandle = syscall.Handle(h)
  802. if w.althandle != 0 {
  803. handle = w.althandle
  804. }
  805. }
  806. }
  807. case 'l':
  808. var ci consoleCursorInfo
  809. cs := buf.String()
  810. if cs == "5>" {
  811. procGetConsoleCursorInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&ci)))
  812. ci.visible = 1
  813. procSetConsoleCursorInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&ci)))
  814. } else if cs == "?25" {
  815. procGetConsoleCursorInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&ci)))
  816. ci.visible = 0
  817. procSetConsoleCursorInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&ci)))
  818. } else if cs == "?1049" {
  819. if w.althandle != 0 {
  820. syscall.CloseHandle(w.althandle)
  821. w.althandle = 0
  822. handle = w.handle
  823. }
  824. }
  825. case 's':
  826. procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))
  827. w.oldpos = csbi.cursorPosition
  828. case 'u':
  829. procSetConsoleCursorPosition.Call(uintptr(handle), *(*uintptr)(unsafe.Pointer(&w.oldpos)))
  830. }
  831. }
  832. return len(data), nil
  833. }
  834. type consoleColor struct {
  835. rgb int
  836. red bool
  837. green bool
  838. blue bool
  839. intensity bool
  840. }
  841. func (c consoleColor) foregroundAttr() (attr word) {
  842. if c.red {
  843. attr |= foregroundRed
  844. }
  845. if c.green {
  846. attr |= foregroundGreen
  847. }
  848. if c.blue {
  849. attr |= foregroundBlue
  850. }
  851. if c.intensity {
  852. attr |= foregroundIntensity
  853. }
  854. return
  855. }
  856. func (c consoleColor) backgroundAttr() (attr word) {
  857. if c.red {
  858. attr |= backgroundRed
  859. }
  860. if c.green {
  861. attr |= backgroundGreen
  862. }
  863. if c.blue {
  864. attr |= backgroundBlue
  865. }
  866. if c.intensity {
  867. attr |= backgroundIntensity
  868. }
  869. return
  870. }
  871. var color16 = []consoleColor{
  872. {0x000000, false, false, false, false},
  873. {0x000080, false, false, true, false},
  874. {0x008000, false, true, false, false},
  875. {0x008080, false, true, true, false},
  876. {0x800000, true, false, false, false},
  877. {0x800080, true, false, true, false},
  878. {0x808000, true, true, false, false},
  879. {0xc0c0c0, true, true, true, false},
  880. {0x808080, false, false, false, true},
  881. {0x0000ff, false, false, true, true},
  882. {0x00ff00, false, true, false, true},
  883. {0x00ffff, false, true, true, true},
  884. {0xff0000, true, false, false, true},
  885. {0xff00ff, true, false, true, true},
  886. {0xffff00, true, true, false, true},
  887. {0xffffff, true, true, true, true},
  888. }
  889. type hsv struct {
  890. h, s, v float32
  891. }
  892. func (a hsv) dist(b hsv) float32 {
  893. dh := a.h - b.h
  894. switch {
  895. case dh > 0.5:
  896. dh = 1 - dh
  897. case dh < -0.5:
  898. dh = -1 - dh
  899. }
  900. ds := a.s - b.s
  901. dv := a.v - b.v
  902. return float32(math.Sqrt(float64(dh*dh + ds*ds + dv*dv)))
  903. }
  904. func toHSV(rgb int) hsv {
  905. r, g, b := float32((rgb&0xFF0000)>>16)/256.0,
  906. float32((rgb&0x00FF00)>>8)/256.0,
  907. float32(rgb&0x0000FF)/256.0
  908. min, max := minmax3f(r, g, b)
  909. h := max - min
  910. if h > 0 {
  911. if max == r {
  912. h = (g - b) / h
  913. if h < 0 {
  914. h += 6
  915. }
  916. } else if max == g {
  917. h = 2 + (b-r)/h
  918. } else {
  919. h = 4 + (r-g)/h
  920. }
  921. }
  922. h /= 6.0
  923. s := max - min
  924. if max != 0 {
  925. s /= max
  926. }
  927. v := max
  928. return hsv{h: h, s: s, v: v}
  929. }
  930. type hsvTable []hsv
  931. func toHSVTable(rgbTable []consoleColor) hsvTable {
  932. t := make(hsvTable, len(rgbTable))
  933. for i, c := range rgbTable {
  934. t[i] = toHSV(c.rgb)
  935. }
  936. return t
  937. }
  938. func (t hsvTable) find(rgb int) consoleColor {
  939. hsv := toHSV(rgb)
  940. n := 7
  941. l := float32(5.0)
  942. for i, p := range t {
  943. d := hsv.dist(p)
  944. if d < l {
  945. l, n = d, i
  946. }
  947. }
  948. return color16[n]
  949. }
  950. func minmax3f(a, b, c float32) (min, max float32) {
  951. if a < b {
  952. if b < c {
  953. return a, c
  954. } else if a < c {
  955. return a, b
  956. } else {
  957. return c, b
  958. }
  959. } else {
  960. if a < c {
  961. return b, c
  962. } else if b < c {
  963. return b, a
  964. } else {
  965. return c, a
  966. }
  967. }
  968. }
  969. var n256foreAttr []word
  970. var n256backAttr []word
  971. func n256setup() {
  972. n256foreAttr = make([]word, 256)
  973. n256backAttr = make([]word, 256)
  974. t := toHSVTable(color16)
  975. for i, rgb := range color256 {
  976. c := t.find(rgb)
  977. n256foreAttr[i] = c.foregroundAttr()
  978. n256backAttr[i] = c.backgroundAttr()
  979. }
  980. }
  981. // EnableColorsStdout enable colors if possible.
  982. func EnableColorsStdout(enabled *bool) func() {
  983. var mode uint32
  984. h := os.Stdout.Fd()
  985. if r, _, _ := procGetConsoleMode.Call(h, uintptr(unsafe.Pointer(&mode))); r != 0 {
  986. if r, _, _ = procSetConsoleMode.Call(h, uintptr(mode|cENABLE_VIRTUAL_TERMINAL_PROCESSING)); r != 0 {
  987. if enabled != nil {
  988. *enabled = true
  989. }
  990. return func() {
  991. procSetConsoleMode.Call(h, uintptr(mode))
  992. }
  993. }
  994. }
  995. if enabled != nil {
  996. *enabled = true
  997. }
  998. return func() {}
  999. }