...

Source file src/github.com/c-bata/go-prompt/input.go

Documentation: github.com/c-bata/go-prompt

     1  package prompt
     2  
     3  import "bytes"
     4  
     5  // WinSize represents the width and height of terminal.
     6  type WinSize struct {
     7  	Row uint16
     8  	Col uint16
     9  }
    10  
    11  // ConsoleParser is an interface to abstract input layer.
    12  type ConsoleParser interface {
    13  	// Setup should be called before starting input
    14  	Setup() error
    15  	// TearDown should be called after stopping input
    16  	TearDown() error
    17  	// GetWinSize returns WinSize object to represent width and height of terminal.
    18  	GetWinSize() *WinSize
    19  	// Read returns byte array.
    20  	Read() ([]byte, error)
    21  }
    22  
    23  // GetKey returns Key correspond to input byte codes.
    24  func GetKey(b []byte) Key {
    25  	for _, k := range ASCIISequences {
    26  		if bytes.Equal(k.ASCIICode, b) {
    27  			return k.Key
    28  		}
    29  	}
    30  	return NotDefined
    31  }
    32  
    33  // ASCIISequences holds mappings of the key and byte array.
    34  var ASCIISequences = []*ASCIICode{
    35  	{Key: Escape, ASCIICode: []byte{0x1b}},
    36  
    37  	{Key: ControlSpace, ASCIICode: []byte{0x00}},
    38  	{Key: ControlA, ASCIICode: []byte{0x1}},
    39  	{Key: ControlB, ASCIICode: []byte{0x2}},
    40  	{Key: ControlC, ASCIICode: []byte{0x3}},
    41  	{Key: ControlD, ASCIICode: []byte{0x4}},
    42  	{Key: ControlE, ASCIICode: []byte{0x5}},
    43  	{Key: ControlF, ASCIICode: []byte{0x6}},
    44  	{Key: ControlG, ASCIICode: []byte{0x7}},
    45  	{Key: ControlH, ASCIICode: []byte{0x8}},
    46  	//{Key: ControlI, ASCIICode: []byte{0x9}},
    47  	//{Key: ControlJ, ASCIICode: []byte{0xa}},
    48  	{Key: ControlK, ASCIICode: []byte{0xb}},
    49  	{Key: ControlL, ASCIICode: []byte{0xc}},
    50  	{Key: ControlM, ASCIICode: []byte{0xd}},
    51  	{Key: ControlN, ASCIICode: []byte{0xe}},
    52  	{Key: ControlO, ASCIICode: []byte{0xf}},
    53  	{Key: ControlP, ASCIICode: []byte{0x10}},
    54  	{Key: ControlQ, ASCIICode: []byte{0x11}},
    55  	{Key: ControlR, ASCIICode: []byte{0x12}},
    56  	{Key: ControlS, ASCIICode: []byte{0x13}},
    57  	{Key: ControlT, ASCIICode: []byte{0x14}},
    58  	{Key: ControlU, ASCIICode: []byte{0x15}},
    59  	{Key: ControlV, ASCIICode: []byte{0x16}},
    60  	{Key: ControlW, ASCIICode: []byte{0x17}},
    61  	{Key: ControlX, ASCIICode: []byte{0x18}},
    62  	{Key: ControlY, ASCIICode: []byte{0x19}},
    63  	{Key: ControlZ, ASCIICode: []byte{0x1a}},
    64  
    65  	{Key: ControlBackslash, ASCIICode: []byte{0x1c}},
    66  	{Key: ControlSquareClose, ASCIICode: []byte{0x1d}},
    67  	{Key: ControlCircumflex, ASCIICode: []byte{0x1e}},
    68  	{Key: ControlUnderscore, ASCIICode: []byte{0x1f}},
    69  	{Key: Backspace, ASCIICode: []byte{0x7f}},
    70  
    71  	{Key: Up, ASCIICode: []byte{0x1b, 0x5b, 0x41}},
    72  	{Key: Down, ASCIICode: []byte{0x1b, 0x5b, 0x42}},
    73  	{Key: Right, ASCIICode: []byte{0x1b, 0x5b, 0x43}},
    74  	{Key: Left, ASCIICode: []byte{0x1b, 0x5b, 0x44}},
    75  	{Key: Home, ASCIICode: []byte{0x1b, 0x5b, 0x48}},
    76  	{Key: Home, ASCIICode: []byte{0x1b, 0x30, 0x48}},
    77  	{Key: End, ASCIICode: []byte{0x1b, 0x5b, 0x46}},
    78  	{Key: End, ASCIICode: []byte{0x1b, 0x30, 0x46}},
    79  
    80  	{Key: Enter, ASCIICode: []byte{0xa}},
    81  	{Key: Delete, ASCIICode: []byte{0x1b, 0x5b, 0x33, 0x7e}},
    82  	{Key: ShiftDelete, ASCIICode: []byte{0x1b, 0x5b, 0x33, 0x3b, 0x32, 0x7e}},
    83  	{Key: ControlDelete, ASCIICode: []byte{0x1b, 0x5b, 0x33, 0x3b, 0x35, 0x7e}},
    84  	{Key: Home, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x7e}},
    85  	{Key: End, ASCIICode: []byte{0x1b, 0x5b, 0x34, 0x7e}},
    86  	{Key: PageUp, ASCIICode: []byte{0x1b, 0x5b, 0x35, 0x7e}},
    87  	{Key: PageDown, ASCIICode: []byte{0x1b, 0x5b, 0x36, 0x7e}},
    88  	{Key: Home, ASCIICode: []byte{0x1b, 0x5b, 0x37, 0x7e}},
    89  	{Key: End, ASCIICode: []byte{0x1b, 0x5b, 0x38, 0x7e}},
    90  	{Key: Tab, ASCIICode: []byte{0x9}},
    91  	{Key: BackTab, ASCIICode: []byte{0x1b, 0x5b, 0x5a}},
    92  	{Key: Insert, ASCIICode: []byte{0x1b, 0x5b, 0x32, 0x7e}},
    93  
    94  	{Key: F1, ASCIICode: []byte{0x1b, 0x4f, 0x50}},
    95  	{Key: F2, ASCIICode: []byte{0x1b, 0x4f, 0x51}},
    96  	{Key: F3, ASCIICode: []byte{0x1b, 0x4f, 0x52}},
    97  	{Key: F4, ASCIICode: []byte{0x1b, 0x4f, 0x53}},
    98  
    99  	{Key: F1, ASCIICode: []byte{0x1b, 0x4f, 0x50, 0x41}}, // Linux console
   100  	{Key: F2, ASCIICode: []byte{0x1b, 0x5b, 0x5b, 0x42}}, // Linux console
   101  	{Key: F3, ASCIICode: []byte{0x1b, 0x5b, 0x5b, 0x43}}, // Linux console
   102  	{Key: F4, ASCIICode: []byte{0x1b, 0x5b, 0x5b, 0x44}}, // Linux console
   103  	{Key: F5, ASCIICode: []byte{0x1b, 0x5b, 0x5b, 0x45}}, // Linux console
   104  
   105  	{Key: F1, ASCIICode: []byte{0x1b, 0x5b, 0x11, 0x7e}}, // rxvt-unicode
   106  	{Key: F2, ASCIICode: []byte{0x1b, 0x5b, 0x12, 0x7e}}, // rxvt-unicode
   107  	{Key: F3, ASCIICode: []byte{0x1b, 0x5b, 0x13, 0x7e}}, // rxvt-unicode
   108  	{Key: F4, ASCIICode: []byte{0x1b, 0x5b, 0x14, 0x7e}}, // rxvt-unicode
   109  
   110  	{Key: F5, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x35, 0x7e}},
   111  	{Key: F6, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x37, 0x7e}},
   112  	{Key: F7, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x38, 0x7e}},
   113  	{Key: F8, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x39, 0x7e}},
   114  	{Key: F9, ASCIICode: []byte{0x1b, 0x5b, 0x32, 0x30, 0x7e}},
   115  	{Key: F10, ASCIICode: []byte{0x1b, 0x5b, 0x32, 0x31, 0x7e}},
   116  	{Key: F11, ASCIICode: []byte{0x1b, 0x5b, 0x32, 0x32, 0x7e}},
   117  	{Key: F12, ASCIICode: []byte{0x1b, 0x5b, 0x32, 0x34, 0x7e, 0x8}},
   118  	{Key: F13, ASCIICode: []byte{0x1b, 0x5b, 0x25, 0x7e}},
   119  	{Key: F14, ASCIICode: []byte{0x1b, 0x5b, 0x26, 0x7e}},
   120  	{Key: F15, ASCIICode: []byte{0x1b, 0x5b, 0x28, 0x7e}},
   121  	{Key: F16, ASCIICode: []byte{0x1b, 0x5b, 0x29, 0x7e}},
   122  	{Key: F17, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x7e}},
   123  	{Key: F18, ASCIICode: []byte{0x1b, 0x5b, 0x32, 0x7e}},
   124  	{Key: F19, ASCIICode: []byte{0x1b, 0x5b, 0x33, 0x7e}},
   125  	{Key: F20, ASCIICode: []byte{0x1b, 0x5b, 0x34, 0x7e}},
   126  
   127  	// Xterm
   128  	{Key: F13, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x3b, 0x32, 0x50}},
   129  	{Key: F14, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x3b, 0x32, 0x51}},
   130  	// &ASCIICode{Key: F15, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x3b, 0x32, 0x52}},  // Conflicts with CPR response
   131  	{Key: F16, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x3b, 0x32, 0x52}},
   132  	{Key: F17, ASCIICode: []byte{0x1b, 0x5b, 0x15, 0x3b, 0x32, 0x7e}},
   133  	{Key: F18, ASCIICode: []byte{0x1b, 0x5b, 0x17, 0x3b, 0x32, 0x7e}},
   134  	{Key: F19, ASCIICode: []byte{0x1b, 0x5b, 0x18, 0x3b, 0x32, 0x7e}},
   135  	{Key: F20, ASCIICode: []byte{0x1b, 0x5b, 0x19, 0x3b, 0x32, 0x7e}},
   136  	{Key: F21, ASCIICode: []byte{0x1b, 0x5b, 0x20, 0x3b, 0x32, 0x7e}},
   137  	{Key: F22, ASCIICode: []byte{0x1b, 0x5b, 0x21, 0x3b, 0x32, 0x7e}},
   138  	{Key: F23, ASCIICode: []byte{0x1b, 0x5b, 0x23, 0x3b, 0x32, 0x7e}},
   139  	{Key: F24, ASCIICode: []byte{0x1b, 0x5b, 0x24, 0x3b, 0x32, 0x7e}},
   140  
   141  	{Key: ControlUp, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x3b, 0x35, 0x41}},
   142  	{Key: ControlDown, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x3b, 0x35, 0x42}},
   143  	{Key: ControlRight, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x3b, 0x35, 0x43}},
   144  	{Key: ControlLeft, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x3b, 0x35, 0x44}},
   145  
   146  	{Key: ShiftUp, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x3b, 0x32, 0x41}},
   147  	{Key: ShiftDown, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x3b, 0x32, 0x42}},
   148  	{Key: ShiftRight, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x3b, 0x32, 0x43}},
   149  	{Key: ShiftLeft, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x3b, 0x32, 0x44}},
   150  
   151  	// Tmux sends following keystrokes when control+arrow is pressed, but for
   152  	// Emacs ansi-term sends the same sequences for normal arrow keys. Consider
   153  	// it a normal arrow press, because that's more important.
   154  	{Key: Up, ASCIICode: []byte{0x1b, 0x4f, 0x41}},
   155  	{Key: Down, ASCIICode: []byte{0x1b, 0x4f, 0x42}},
   156  	{Key: Right, ASCIICode: []byte{0x1b, 0x4f, 0x43}},
   157  	{Key: Left, ASCIICode: []byte{0x1b, 0x4f, 0x44}},
   158  
   159  	{Key: ControlUp, ASCIICode: []byte{0x1b, 0x5b, 0x35, 0x41}},
   160  	{Key: ControlDown, ASCIICode: []byte{0x1b, 0x5b, 0x35, 0x42}},
   161  	{Key: ControlRight, ASCIICode: []byte{0x1b, 0x5b, 0x35, 0x43}},
   162  	{Key: ControlLeft, ASCIICode: []byte{0x1b, 0x5b, 0x35, 0x44}},
   163  
   164  	{Key: ControlRight, ASCIICode: []byte{0x1b, 0x5b, 0x4f, 0x63}}, // rxvt
   165  	{Key: ControlLeft, ASCIICode: []byte{0x1b, 0x5b, 0x4f, 0x64}},  // rxvt
   166  
   167  	{Key: Ignore, ASCIICode: []byte{0x1b, 0x5b, 0x45}}, // Xterm
   168  	{Key: Ignore, ASCIICode: []byte{0x1b, 0x5b, 0x46}}, // Linux console
   169  }
   170  

View as plain text