ansi.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // +build windows
  2. package winterm
  3. import (
  4. "fmt"
  5. "os"
  6. "strconv"
  7. "strings"
  8. "syscall"
  9. "github.com/Azure/go-ansiterm"
  10. )
  11. // Windows keyboard constants
  12. // See https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx.
  13. const (
  14. VK_PRIOR = 0x21 // PAGE UP key
  15. VK_NEXT = 0x22 // PAGE DOWN key
  16. VK_END = 0x23 // END key
  17. VK_HOME = 0x24 // HOME key
  18. VK_LEFT = 0x25 // LEFT ARROW key
  19. VK_UP = 0x26 // UP ARROW key
  20. VK_RIGHT = 0x27 // RIGHT ARROW key
  21. VK_DOWN = 0x28 // DOWN ARROW key
  22. VK_SELECT = 0x29 // SELECT key
  23. VK_PRINT = 0x2A // PRINT key
  24. VK_EXECUTE = 0x2B // EXECUTE key
  25. VK_SNAPSHOT = 0x2C // PRINT SCREEN key
  26. VK_INSERT = 0x2D // INS key
  27. VK_DELETE = 0x2E // DEL key
  28. VK_HELP = 0x2F // HELP key
  29. VK_F1 = 0x70 // F1 key
  30. VK_F2 = 0x71 // F2 key
  31. VK_F3 = 0x72 // F3 key
  32. VK_F4 = 0x73 // F4 key
  33. VK_F5 = 0x74 // F5 key
  34. VK_F6 = 0x75 // F6 key
  35. VK_F7 = 0x76 // F7 key
  36. VK_F8 = 0x77 // F8 key
  37. VK_F9 = 0x78 // F9 key
  38. VK_F10 = 0x79 // F10 key
  39. VK_F11 = 0x7A // F11 key
  40. VK_F12 = 0x7B // F12 key
  41. RIGHT_ALT_PRESSED = 0x0001
  42. LEFT_ALT_PRESSED = 0x0002
  43. RIGHT_CTRL_PRESSED = 0x0004
  44. LEFT_CTRL_PRESSED = 0x0008
  45. SHIFT_PRESSED = 0x0010
  46. NUMLOCK_ON = 0x0020
  47. SCROLLLOCK_ON = 0x0040
  48. CAPSLOCK_ON = 0x0080
  49. ENHANCED_KEY = 0x0100
  50. )
  51. type ansiCommand struct {
  52. CommandBytes []byte
  53. Command string
  54. Parameters []string
  55. IsSpecial bool
  56. }
  57. func newAnsiCommand(command []byte) *ansiCommand {
  58. if isCharacterSelectionCmdChar(command[1]) {
  59. // Is Character Set Selection commands
  60. return &ansiCommand{
  61. CommandBytes: command,
  62. Command: string(command),
  63. IsSpecial: true,
  64. }
  65. }
  66. // last char is command character
  67. lastCharIndex := len(command) - 1
  68. ac := &ansiCommand{
  69. CommandBytes: command,
  70. Command: string(command[lastCharIndex]),
  71. IsSpecial: false,
  72. }
  73. // more than a single escape
  74. if lastCharIndex != 0 {
  75. start := 1
  76. // skip if double char escape sequence
  77. if command[0] == ansiterm.ANSI_ESCAPE_PRIMARY && command[1] == ansiterm.ANSI_ESCAPE_SECONDARY {
  78. start++
  79. }
  80. // convert this to GetNextParam method
  81. ac.Parameters = strings.Split(string(command[start:lastCharIndex]), ansiterm.ANSI_PARAMETER_SEP)
  82. }
  83. return ac
  84. }
  85. func (ac *ansiCommand) paramAsSHORT(index int, defaultValue int16) int16 {
  86. if index < 0 || index >= len(ac.Parameters) {
  87. return defaultValue
  88. }
  89. param, err := strconv.ParseInt(ac.Parameters[index], 10, 16)
  90. if err != nil {
  91. return defaultValue
  92. }
  93. return int16(param)
  94. }
  95. func (ac *ansiCommand) String() string {
  96. return fmt.Sprintf("0x%v \"%v\" (\"%v\")",
  97. bytesToHex(ac.CommandBytes),
  98. ac.Command,
  99. strings.Join(ac.Parameters, "\",\""))
  100. }
  101. // isAnsiCommandChar returns true if the passed byte falls within the range of ANSI commands.
  102. // See http://manpages.ubuntu.com/manpages/intrepid/man4/console_codes.4.html.
  103. func isAnsiCommandChar(b byte) bool {
  104. switch {
  105. case ansiterm.ANSI_COMMAND_FIRST <= b && b <= ansiterm.ANSI_COMMAND_LAST && b != ansiterm.ANSI_ESCAPE_SECONDARY:
  106. return true
  107. case b == ansiterm.ANSI_CMD_G1 || b == ansiterm.ANSI_CMD_OSC || b == ansiterm.ANSI_CMD_DECPAM || b == ansiterm.ANSI_CMD_DECPNM:
  108. // non-CSI escape sequence terminator
  109. return true
  110. case b == ansiterm.ANSI_CMD_STR_TERM || b == ansiterm.ANSI_BEL:
  111. // String escape sequence terminator
  112. return true
  113. }
  114. return false
  115. }
  116. func isXtermOscSequence(command []byte, current byte) bool {
  117. return (len(command) >= 2 && command[0] == ansiterm.ANSI_ESCAPE_PRIMARY && command[1] == ansiterm.ANSI_CMD_OSC && current != ansiterm.ANSI_BEL)
  118. }
  119. func isCharacterSelectionCmdChar(b byte) bool {
  120. return (b == ansiterm.ANSI_CMD_G0 || b == ansiterm.ANSI_CMD_G1 || b == ansiterm.ANSI_CMD_G2 || b == ansiterm.ANSI_CMD_G3)
  121. }
  122. // bytesToHex converts a slice of bytes to a human-readable string.
  123. func bytesToHex(b []byte) string {
  124. hex := make([]string, len(b))
  125. for i, ch := range b {
  126. hex[i] = fmt.Sprintf("%X", ch)
  127. }
  128. return strings.Join(hex, "")
  129. }
  130. // ensureInRange adjusts the passed value, if necessary, to ensure it is within
  131. // the passed min / max range.
  132. func ensureInRange(n int16, min int16, max int16) int16 {
  133. if n < min {
  134. return min
  135. } else if n > max {
  136. return max
  137. } else {
  138. return n
  139. }
  140. }
  141. func GetStdFile(nFile int) (*os.File, uintptr) {
  142. var file *os.File
  143. switch nFile {
  144. case syscall.STD_INPUT_HANDLE:
  145. file = os.Stdin
  146. case syscall.STD_OUTPUT_HANDLE:
  147. file = os.Stdout
  148. case syscall.STD_ERROR_HANDLE:
  149. file = os.Stderr
  150. default:
  151. panic(fmt.Errorf("Invalid standard handle identifier: %v", nFile))
  152. }
  153. fd, err := syscall.GetStdHandle(nFile)
  154. if err != nil {
  155. panic(fmt.Errorf("Invalid standard handle identifier: %v -- %v", nFile, err))
  156. }
  157. return file, uintptr(fd)
  158. }