colorable_windows.go 24 KB

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