colorable_windows.go 24 KB

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