colorable_windows.go 24 KB

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