colorable_windows.go 20 KB

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