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. commonLvbUnderscore = 0x8000
  27. cENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x4
  28. )
  29. const (
  30. genericRead = 0x80000000
  31. genericWrite = 0x40000000
  32. )
  33. const (
  34. consoleTextmodeBuffer = 0x1
  35. )
  36. type wchar uint16
  37. type short int16
  38. type dword uint32
  39. type word uint16
  40. type coord struct {
  41. x short
  42. y short
  43. }
  44. type smallRect struct {
  45. left short
  46. top short
  47. right short
  48. bottom short
  49. }
  50. type consoleScreenBufferInfo struct {
  51. size coord
  52. cursorPosition coord
  53. attributes word
  54. window smallRect
  55. maximumWindowSize coord
  56. }
  57. type consoleCursorInfo struct {
  58. size dword
  59. visible int32
  60. }
  61. var (
  62. kernel32 = syscall.NewLazyDLL("kernel32.dll")
  63. procGetConsoleScreenBufferInfo = kernel32.NewProc("GetConsoleScreenBufferInfo")
  64. procSetConsoleTextAttribute = kernel32.NewProc("SetConsoleTextAttribute")
  65. procSetConsoleCursorPosition = kernel32.NewProc("SetConsoleCursorPosition")
  66. procFillConsoleOutputCharacter = kernel32.NewProc("FillConsoleOutputCharacterW")
  67. procFillConsoleOutputAttribute = kernel32.NewProc("FillConsoleOutputAttribute")
  68. procGetConsoleCursorInfo = kernel32.NewProc("GetConsoleCursorInfo")
  69. procSetConsoleCursorInfo = kernel32.NewProc("SetConsoleCursorInfo")
  70. procSetConsoleTitle = kernel32.NewProc("SetConsoleTitleW")
  71. procGetConsoleMode = kernel32.NewProc("GetConsoleMode")
  72. procSetConsoleMode = kernel32.NewProc("SetConsoleMode")
  73. procCreateConsoleScreenBuffer = kernel32.NewProc("CreateConsoleScreenBuffer")
  74. )
  75. // Writer provides colorable Writer to the console
  76. type Writer struct {
  77. out io.Writer
  78. handle syscall.Handle
  79. althandle syscall.Handle
  80. oldattr word
  81. oldpos coord
  82. rest bytes.Buffer
  83. }
  84. // NewColorable returns new instance of Writer which handles escape sequence from File.
  85. func NewColorable(file *os.File) io.Writer {
  86. if file == nil {
  87. panic("nil passed instead of *os.File to NewColorable()")
  88. }
  89. if isatty.IsTerminal(file.Fd()) {
  90. var mode uint32
  91. if r, _, _ := procGetConsoleMode.Call(file.Fd(), uintptr(unsafe.Pointer(&mode))); r != 0 && mode&cENABLE_VIRTUAL_TERMINAL_PROCESSING != 0 {
  92. return file
  93. }
  94. var csbi consoleScreenBufferInfo
  95. handle := syscall.Handle(file.Fd())
  96. procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))
  97. return &Writer{out: file, handle: handle, oldattr: csbi.attributes, oldpos: coord{0, 0}}
  98. }
  99. return file
  100. }
  101. // NewColorableStdout returns new instance of Writer which handles escape sequence for stdout.
  102. func NewColorableStdout() io.Writer {
  103. return NewColorable(os.Stdout)
  104. }
  105. // NewColorableStderr returns new instance of Writer which handles escape sequence for stderr.
  106. func NewColorableStderr() io.Writer {
  107. return NewColorable(os.Stderr)
  108. }
  109. var color256 = map[int]int{
  110. 0: 0x000000,
  111. 1: 0x800000,
  112. 2: 0x008000,
  113. 3: 0x808000,
  114. 4: 0x000080,
  115. 5: 0x800080,
  116. 6: 0x008080,
  117. 7: 0xc0c0c0,
  118. 8: 0x808080,
  119. 9: 0xff0000,
  120. 10: 0x00ff00,
  121. 11: 0xffff00,
  122. 12: 0x0000ff,
  123. 13: 0xff00ff,
  124. 14: 0x00ffff,
  125. 15: 0xffffff,
  126. 16: 0x000000,
  127. 17: 0x00005f,
  128. 18: 0x000087,
  129. 19: 0x0000af,
  130. 20: 0x0000d7,
  131. 21: 0x0000ff,
  132. 22: 0x005f00,
  133. 23: 0x005f5f,
  134. 24: 0x005f87,
  135. 25: 0x005faf,
  136. 26: 0x005fd7,
  137. 27: 0x005fff,
  138. 28: 0x008700,
  139. 29: 0x00875f,
  140. 30: 0x008787,
  141. 31: 0x0087af,
  142. 32: 0x0087d7,
  143. 33: 0x0087ff,
  144. 34: 0x00af00,
  145. 35: 0x00af5f,
  146. 36: 0x00af87,
  147. 37: 0x00afaf,
  148. 38: 0x00afd7,
  149. 39: 0x00afff,
  150. 40: 0x00d700,
  151. 41: 0x00d75f,
  152. 42: 0x00d787,
  153. 43: 0x00d7af,
  154. 44: 0x00d7d7,
  155. 45: 0x00d7ff,
  156. 46: 0x00ff00,
  157. 47: 0x00ff5f,
  158. 48: 0x00ff87,
  159. 49: 0x00ffaf,
  160. 50: 0x00ffd7,
  161. 51: 0x00ffff,
  162. 52: 0x5f0000,
  163. 53: 0x5f005f,
  164. 54: 0x5f0087,
  165. 55: 0x5f00af,
  166. 56: 0x5f00d7,
  167. 57: 0x5f00ff,
  168. 58: 0x5f5f00,
  169. 59: 0x5f5f5f,
  170. 60: 0x5f5f87,
  171. 61: 0x5f5faf,
  172. 62: 0x5f5fd7,
  173. 63: 0x5f5fff,
  174. 64: 0x5f8700,
  175. 65: 0x5f875f,
  176. 66: 0x5f8787,
  177. 67: 0x5f87af,
  178. 68: 0x5f87d7,
  179. 69: 0x5f87ff,
  180. 70: 0x5faf00,
  181. 71: 0x5faf5f,
  182. 72: 0x5faf87,
  183. 73: 0x5fafaf,
  184. 74: 0x5fafd7,
  185. 75: 0x5fafff,
  186. 76: 0x5fd700,
  187. 77: 0x5fd75f,
  188. 78: 0x5fd787,
  189. 79: 0x5fd7af,
  190. 80: 0x5fd7d7,
  191. 81: 0x5fd7ff,
  192. 82: 0x5fff00,
  193. 83: 0x5fff5f,
  194. 84: 0x5fff87,
  195. 85: 0x5fffaf,
  196. 86: 0x5fffd7,
  197. 87: 0x5fffff,
  198. 88: 0x870000,
  199. 89: 0x87005f,
  200. 90: 0x870087,
  201. 91: 0x8700af,
  202. 92: 0x8700d7,
  203. 93: 0x8700ff,
  204. 94: 0x875f00,
  205. 95: 0x875f5f,
  206. 96: 0x875f87,
  207. 97: 0x875faf,
  208. 98: 0x875fd7,
  209. 99: 0x875fff,
  210. 100: 0x878700,
  211. 101: 0x87875f,
  212. 102: 0x878787,
  213. 103: 0x8787af,
  214. 104: 0x8787d7,
  215. 105: 0x8787ff,
  216. 106: 0x87af00,
  217. 107: 0x87af5f,
  218. 108: 0x87af87,
  219. 109: 0x87afaf,
  220. 110: 0x87afd7,
  221. 111: 0x87afff,
  222. 112: 0x87d700,
  223. 113: 0x87d75f,
  224. 114: 0x87d787,
  225. 115: 0x87d7af,
  226. 116: 0x87d7d7,
  227. 117: 0x87d7ff,
  228. 118: 0x87ff00,
  229. 119: 0x87ff5f,
  230. 120: 0x87ff87,
  231. 121: 0x87ffaf,
  232. 122: 0x87ffd7,
  233. 123: 0x87ffff,
  234. 124: 0xaf0000,
  235. 125: 0xaf005f,
  236. 126: 0xaf0087,
  237. 127: 0xaf00af,
  238. 128: 0xaf00d7,
  239. 129: 0xaf00ff,
  240. 130: 0xaf5f00,
  241. 131: 0xaf5f5f,
  242. 132: 0xaf5f87,
  243. 133: 0xaf5faf,
  244. 134: 0xaf5fd7,
  245. 135: 0xaf5fff,
  246. 136: 0xaf8700,
  247. 137: 0xaf875f,
  248. 138: 0xaf8787,
  249. 139: 0xaf87af,
  250. 140: 0xaf87d7,
  251. 141: 0xaf87ff,
  252. 142: 0xafaf00,
  253. 143: 0xafaf5f,
  254. 144: 0xafaf87,
  255. 145: 0xafafaf,
  256. 146: 0xafafd7,
  257. 147: 0xafafff,
  258. 148: 0xafd700,
  259. 149: 0xafd75f,
  260. 150: 0xafd787,
  261. 151: 0xafd7af,
  262. 152: 0xafd7d7,
  263. 153: 0xafd7ff,
  264. 154: 0xafff00,
  265. 155: 0xafff5f,
  266. 156: 0xafff87,
  267. 157: 0xafffaf,
  268. 158: 0xafffd7,
  269. 159: 0xafffff,
  270. 160: 0xd70000,
  271. 161: 0xd7005f,
  272. 162: 0xd70087,
  273. 163: 0xd700af,
  274. 164: 0xd700d7,
  275. 165: 0xd700ff,
  276. 166: 0xd75f00,
  277. 167: 0xd75f5f,
  278. 168: 0xd75f87,
  279. 169: 0xd75faf,
  280. 170: 0xd75fd7,
  281. 171: 0xd75fff,
  282. 172: 0xd78700,
  283. 173: 0xd7875f,
  284. 174: 0xd78787,
  285. 175: 0xd787af,
  286. 176: 0xd787d7,
  287. 177: 0xd787ff,
  288. 178: 0xd7af00,
  289. 179: 0xd7af5f,
  290. 180: 0xd7af87,
  291. 181: 0xd7afaf,
  292. 182: 0xd7afd7,
  293. 183: 0xd7afff,
  294. 184: 0xd7d700,
  295. 185: 0xd7d75f,
  296. 186: 0xd7d787,
  297. 187: 0xd7d7af,
  298. 188: 0xd7d7d7,
  299. 189: 0xd7d7ff,
  300. 190: 0xd7ff00,
  301. 191: 0xd7ff5f,
  302. 192: 0xd7ff87,
  303. 193: 0xd7ffaf,
  304. 194: 0xd7ffd7,
  305. 195: 0xd7ffff,
  306. 196: 0xff0000,
  307. 197: 0xff005f,
  308. 198: 0xff0087,
  309. 199: 0xff00af,
  310. 200: 0xff00d7,
  311. 201: 0xff00ff,
  312. 202: 0xff5f00,
  313. 203: 0xff5f5f,
  314. 204: 0xff5f87,
  315. 205: 0xff5faf,
  316. 206: 0xff5fd7,
  317. 207: 0xff5fff,
  318. 208: 0xff8700,
  319. 209: 0xff875f,
  320. 210: 0xff8787,
  321. 211: 0xff87af,
  322. 212: 0xff87d7,
  323. 213: 0xff87ff,
  324. 214: 0xffaf00,
  325. 215: 0xffaf5f,
  326. 216: 0xffaf87,
  327. 217: 0xffafaf,
  328. 218: 0xffafd7,
  329. 219: 0xffafff,
  330. 220: 0xffd700,
  331. 221: 0xffd75f,
  332. 222: 0xffd787,
  333. 223: 0xffd7af,
  334. 224: 0xffd7d7,
  335. 225: 0xffd7ff,
  336. 226: 0xffff00,
  337. 227: 0xffff5f,
  338. 228: 0xffff87,
  339. 229: 0xffffaf,
  340. 230: 0xffffd7,
  341. 231: 0xffffff,
  342. 232: 0x080808,
  343. 233: 0x121212,
  344. 234: 0x1c1c1c,
  345. 235: 0x262626,
  346. 236: 0x303030,
  347. 237: 0x3a3a3a,
  348. 238: 0x444444,
  349. 239: 0x4e4e4e,
  350. 240: 0x585858,
  351. 241: 0x626262,
  352. 242: 0x6c6c6c,
  353. 243: 0x767676,
  354. 244: 0x808080,
  355. 245: 0x8a8a8a,
  356. 246: 0x949494,
  357. 247: 0x9e9e9e,
  358. 248: 0xa8a8a8,
  359. 249: 0xb2b2b2,
  360. 250: 0xbcbcbc,
  361. 251: 0xc6c6c6,
  362. 252: 0xd0d0d0,
  363. 253: 0xdadada,
  364. 254: 0xe4e4e4,
  365. 255: 0xeeeeee,
  366. }
  367. // `\033]0;TITLESTR\007`
  368. func doTitleSequence(er *bytes.Reader) error {
  369. var c byte
  370. var err error
  371. c, err = er.ReadByte()
  372. if err != nil {
  373. return err
  374. }
  375. if c != '0' && c != '2' {
  376. return nil
  377. }
  378. c, err = er.ReadByte()
  379. if err != nil {
  380. return err
  381. }
  382. if c != ';' {
  383. return nil
  384. }
  385. title := make([]byte, 0, 80)
  386. for {
  387. c, err = er.ReadByte()
  388. if err != nil {
  389. return err
  390. }
  391. if c == 0x07 || c == '\n' {
  392. break
  393. }
  394. title = append(title, c)
  395. }
  396. if len(title) > 0 {
  397. title8, err := syscall.UTF16PtrFromString(string(title))
  398. if err == nil {
  399. procSetConsoleTitle.Call(uintptr(unsafe.Pointer(title8)))
  400. }
  401. }
  402. return nil
  403. }
  404. // returns Atoi(s) unless s == "" in which case it returns def
  405. func atoiWithDefault(s string, def int) (int, error) {
  406. if s == "" {
  407. return def, nil
  408. }
  409. return strconv.Atoi(s)
  410. }
  411. // Write writes data on console
  412. func (w *Writer) Write(data []byte) (n int, err error) {
  413. var csbi consoleScreenBufferInfo
  414. procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
  415. handle := w.handle
  416. var er *bytes.Reader
  417. if w.rest.Len() > 0 {
  418. var rest bytes.Buffer
  419. w.rest.WriteTo(&rest)
  420. w.rest.Reset()
  421. rest.Write(data)
  422. er = bytes.NewReader(rest.Bytes())
  423. } else {
  424. er = bytes.NewReader(data)
  425. }
  426. var bw [1]byte
  427. loop:
  428. for {
  429. c1, err := er.ReadByte()
  430. if err != nil {
  431. break loop
  432. }
  433. if c1 != 0x1b {
  434. bw[0] = c1
  435. w.out.Write(bw[:])
  436. continue
  437. }
  438. c2, err := er.ReadByte()
  439. if err != nil {
  440. break loop
  441. }
  442. switch c2 {
  443. case '>':
  444. continue
  445. case ']':
  446. w.rest.WriteByte(c1)
  447. w.rest.WriteByte(c2)
  448. er.WriteTo(&w.rest)
  449. if bytes.IndexByte(w.rest.Bytes(), 0x07) == -1 {
  450. break loop
  451. }
  452. er = bytes.NewReader(w.rest.Bytes()[2:])
  453. err := doTitleSequence(er)
  454. if err != nil {
  455. break loop
  456. }
  457. w.rest.Reset()
  458. continue
  459. // https://github.com/mattn/go-colorable/issues/27
  460. case '7':
  461. procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))
  462. w.oldpos = csbi.cursorPosition
  463. continue
  464. case '8':
  465. procSetConsoleCursorPosition.Call(uintptr(handle), *(*uintptr)(unsafe.Pointer(&w.oldpos)))
  466. continue
  467. case 0x5b:
  468. // execute part after switch
  469. default:
  470. continue
  471. }
  472. w.rest.WriteByte(c1)
  473. w.rest.WriteByte(c2)
  474. er.WriteTo(&w.rest)
  475. var buf bytes.Buffer
  476. var m byte
  477. for i, c := range w.rest.Bytes()[2:] {
  478. if ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '@' {
  479. m = c
  480. er = bytes.NewReader(w.rest.Bytes()[2+i+1:])
  481. w.rest.Reset()
  482. break
  483. }
  484. buf.Write([]byte(string(c)))
  485. }
  486. if m == 0 {
  487. break loop
  488. }
  489. switch m {
  490. case 'A':
  491. n, err = atoiWithDefault(buf.String(), 1)
  492. if err != nil {
  493. continue
  494. }
  495. procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))
  496. csbi.cursorPosition.y -= short(n)
  497. procSetConsoleCursorPosition.Call(uintptr(handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  498. case 'B':
  499. n, err = atoiWithDefault(buf.String(), 1)
  500. if err != nil {
  501. continue
  502. }
  503. procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))
  504. csbi.cursorPosition.y += short(n)
  505. procSetConsoleCursorPosition.Call(uintptr(handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  506. case 'C':
  507. n, err = atoiWithDefault(buf.String(), 1)
  508. if err != nil {
  509. continue
  510. }
  511. procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))
  512. csbi.cursorPosition.x += short(n)
  513. procSetConsoleCursorPosition.Call(uintptr(handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  514. case 'D':
  515. n, err = atoiWithDefault(buf.String(), 1)
  516. if err != nil {
  517. continue
  518. }
  519. procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))
  520. csbi.cursorPosition.x -= short(n)
  521. if csbi.cursorPosition.x < 0 {
  522. csbi.cursorPosition.x = 0
  523. }
  524. procSetConsoleCursorPosition.Call(uintptr(handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  525. case 'E':
  526. n, err = strconv.Atoi(buf.String())
  527. if err != nil {
  528. continue
  529. }
  530. procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))
  531. csbi.cursorPosition.x = 0
  532. csbi.cursorPosition.y += short(n)
  533. procSetConsoleCursorPosition.Call(uintptr(handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  534. case 'F':
  535. n, err = strconv.Atoi(buf.String())
  536. if err != nil {
  537. continue
  538. }
  539. procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))
  540. csbi.cursorPosition.x = 0
  541. csbi.cursorPosition.y -= short(n)
  542. procSetConsoleCursorPosition.Call(uintptr(handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  543. case 'G':
  544. n, err = strconv.Atoi(buf.String())
  545. if err != nil {
  546. continue
  547. }
  548. if n < 1 {
  549. n = 1
  550. }
  551. procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))
  552. csbi.cursorPosition.x = short(n - 1)
  553. procSetConsoleCursorPosition.Call(uintptr(handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  554. case 'H', 'f':
  555. procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))
  556. if buf.Len() > 0 {
  557. token := strings.Split(buf.String(), ";")
  558. switch len(token) {
  559. case 1:
  560. n1, err := strconv.Atoi(token[0])
  561. if err != nil {
  562. continue
  563. }
  564. csbi.cursorPosition.y = short(n1 - 1)
  565. case 2:
  566. n1, err := strconv.Atoi(token[0])
  567. if err != nil {
  568. continue
  569. }
  570. n2, err := strconv.Atoi(token[1])
  571. if err != nil {
  572. continue
  573. }
  574. csbi.cursorPosition.x = short(n2 - 1)
  575. csbi.cursorPosition.y = short(n1 - 1)
  576. }
  577. } else {
  578. csbi.cursorPosition.y = 0
  579. }
  580. procSetConsoleCursorPosition.Call(uintptr(handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
  581. case 'J':
  582. n := 0
  583. if buf.Len() > 0 {
  584. n, err = strconv.Atoi(buf.String())
  585. if err != nil {
  586. continue
  587. }
  588. }
  589. var count, written dword
  590. var cursor coord
  591. procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))
  592. switch n {
  593. case 0:
  594. cursor = coord{x: csbi.cursorPosition.x, y: csbi.cursorPosition.y}
  595. count = dword(csbi.size.x) - dword(csbi.cursorPosition.x) + dword(csbi.size.y-csbi.cursorPosition.y)*dword(csbi.size.x)
  596. case 1:
  597. cursor = coord{x: csbi.window.left, y: csbi.window.top}
  598. count = dword(csbi.size.x) - dword(csbi.cursorPosition.x) + dword(csbi.window.top-csbi.cursorPosition.y)*dword(csbi.size.x)
  599. case 2:
  600. cursor = coord{x: csbi.window.left, y: csbi.window.top}
  601. count = dword(csbi.size.x) - dword(csbi.cursorPosition.x) + dword(csbi.size.y-csbi.cursorPosition.y)*dword(csbi.size.x)
  602. }
  603. procFillConsoleOutputCharacter.Call(uintptr(handle), uintptr(' '), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  604. procFillConsoleOutputAttribute.Call(uintptr(handle), uintptr(csbi.attributes), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  605. case 'K':
  606. n := 0
  607. if buf.Len() > 0 {
  608. n, err = strconv.Atoi(buf.String())
  609. if err != nil {
  610. continue
  611. }
  612. }
  613. procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))
  614. var cursor coord
  615. var count, written dword
  616. switch n {
  617. case 0:
  618. cursor = coord{x: csbi.cursorPosition.x, y: csbi.cursorPosition.y}
  619. count = dword(csbi.size.x - csbi.cursorPosition.x)
  620. case 1:
  621. cursor = coord{x: csbi.window.left, y: csbi.cursorPosition.y}
  622. count = dword(csbi.size.x - csbi.cursorPosition.x)
  623. case 2:
  624. cursor = coord{x: csbi.window.left, y: csbi.cursorPosition.y}
  625. count = dword(csbi.size.x)
  626. }
  627. procFillConsoleOutputCharacter.Call(uintptr(handle), uintptr(' '), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  628. procFillConsoleOutputAttribute.Call(uintptr(handle), uintptr(csbi.attributes), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  629. case 'X':
  630. n := 0
  631. if buf.Len() > 0 {
  632. n, err = strconv.Atoi(buf.String())
  633. if err != nil {
  634. continue
  635. }
  636. }
  637. procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))
  638. var cursor coord
  639. var written dword
  640. cursor = coord{x: csbi.cursorPosition.x, y: csbi.cursorPosition.y}
  641. procFillConsoleOutputCharacter.Call(uintptr(handle), uintptr(' '), uintptr(n), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  642. procFillConsoleOutputAttribute.Call(uintptr(handle), uintptr(csbi.attributes), uintptr(n), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&written)))
  643. case 'm':
  644. procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))
  645. attr := csbi.attributes
  646. cs := buf.String()
  647. if cs == "" {
  648. procSetConsoleTextAttribute.Call(uintptr(handle), uintptr(w.oldattr))
  649. continue
  650. }
  651. token := strings.Split(cs, ";")
  652. for i := 0; i < len(token); i++ {
  653. ns := token[i]
  654. if n, err = strconv.Atoi(ns); err == nil {
  655. switch {
  656. case n == 0 || n == 100:
  657. attr = w.oldattr
  658. case n == 4:
  659. attr |= commonLvbUnderscore
  660. case (1 <= n && n <= 3) || n == 5:
  661. attr |= foregroundIntensity
  662. case n == 7 || n == 27:
  663. attr =
  664. (attr &^ (foregroundMask | backgroundMask)) |
  665. ((attr & foregroundMask) << 4) |
  666. ((attr & backgroundMask) >> 4)
  667. case n == 22:
  668. attr &^= foregroundIntensity
  669. case n == 24:
  670. attr &^= commonLvbUnderscore
  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. }