...

Source file src/edge-infra.dev/pkg/sds/emergencyaccess/emulator/key_bindings.go

Documentation: edge-infra.dev/pkg/sds/emergencyaccess/emulator

     1  package emulator
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/c-bata/go-prompt"
     7  )
     8  
     9  // Slice of common key bindings used for both connect and session prompt
    10  var commonKeyBindings = []prompt.KeyBind{
    11  	{
    12  		// Ctrl+Left key Back one word
    13  		Key: prompt.ControlLeft,
    14  		Fn: func(b *prompt.Buffer) {
    15  			b.CursorLeft(len([]rune(b.Document().GetWordBeforeCursorWithSpace())))
    16  		},
    17  	},
    18  	{
    19  		// Ctrl+Right Key Forward one word
    20  		Key: prompt.ControlRight,
    21  		Fn: func(b *prompt.Buffer) {
    22  			b.CursorRight(len([]rune(b.Document().GetWordAfterCursorWithSpace())))
    23  		},
    24  	},
    25  }
    26  
    27  // Slice of key bindings exclusively used by the session prompt
    28  var sessionKeyBindings = []prompt.KeyBind{
    29  	{
    30  		// Ctrl+S enter session prompt options
    31  		Key: prompt.ControlS,
    32  		Fn: func(b *prompt.Buffer) {
    33  			// User should only be able to press Ctrl+S once
    34  			if strings.Contains(b.Document().CurrentLine(), sendOptionsSplitter) {
    35  				return
    36  			}
    37  
    38  			prompt.GoLineEnd(b)
    39  			// splitter should be padded with spaces
    40  			w := b.Document().GetWordBeforeCursorWithSpace()
    41  			if len(w) == 0 || w[len(w)-1] != ' ' {
    42  				b.InsertText(" ", false, true)
    43  			}
    44  			b.InsertText(sendOptionsSplitter+" ", false, true)
    45  		},
    46  	},
    47  
    48  	// The following bindings override default key bindings to ensure the
    49  	// sessionOptionSplitter cannot be broken/modified by a user.
    50  	// This is required as the sessionOptionSplitter is currently multiple
    51  	// characters wide, however it should be treated as a single character when
    52  	// manipulating the command line.
    53  	// Note they are all additive key bindings, they do not replace the original
    54  	// mapping for the particular key
    55  
    56  	{
    57  		Key: prompt.Left,
    58  		Fn: func(b *prompt.Buffer) {
    59  			l := len(sendOptionsSplitter)
    60  			if b.Document().GetWordBeforeCursor() == sendOptionsSplitter {
    61  				b.CursorLeft(l + 1)
    62  			}
    63  		},
    64  	},
    65  	{
    66  		// Emacs left
    67  		Key: prompt.ControlB,
    68  		Fn: func(b *prompt.Buffer) {
    69  			l := len(sendOptionsSplitter)
    70  			if b.Document().GetWordBeforeCursor() == sendOptionsSplitter {
    71  				b.CursorLeft(l + 1)
    72  			}
    73  		},
    74  	},
    75  
    76  	{
    77  		Key: prompt.Right,
    78  		Fn: func(b *prompt.Buffer) {
    79  			l := len(sendOptionsSplitter)
    80  			if b.Document().GetWordAfterCursor() == sendOptionsSplitter {
    81  				b.CursorRight(l + 1)
    82  			}
    83  		},
    84  	},
    85  	{
    86  		// Same as Right
    87  		Key: prompt.ControlF,
    88  		Fn: func(b *prompt.Buffer) {
    89  			l := len(sendOptionsSplitter)
    90  			if b.Document().GetWordAfterCursor() == sendOptionsSplitter {
    91  				b.CursorRight(l + 1)
    92  			}
    93  		},
    94  	},
    95  
    96  	{
    97  		Key: prompt.Backspace,
    98  		Fn: func(b *prompt.Buffer) {
    99  			l := len(sendOptionsSplitter)
   100  			if b.Document().GetWordBeforeCursor() == sendOptionsSplitter {
   101  				b.DeleteBeforeCursor(l + 1)
   102  			}
   103  		},
   104  	},
   105  	{
   106  		// Same as backspace
   107  		Key: prompt.ControlH,
   108  		Fn: func(b *prompt.Buffer) {
   109  			l := len(sendOptionsSplitter)
   110  			if b.Document().GetWordBeforeCursor() == sendOptionsSplitter {
   111  				b.DeleteBeforeCursor(l + 1)
   112  			}
   113  		},
   114  	},
   115  
   116  	{
   117  		// Same as Delete
   118  		Key: prompt.ControlD,
   119  		Fn: func(b *prompt.Buffer) {
   120  			l := len(sendOptionsSplitter)
   121  			if b.Document().GetWordAfterCursor() == sendOptionsSplitter {
   122  				b.Delete(l + 1)
   123  			}
   124  		},
   125  	},
   126  	{
   127  		Key: prompt.Delete,
   128  		Fn: func(b *prompt.Buffer) {
   129  			l := len(sendOptionsSplitter)
   130  			if b.Document().GetWordAfterCursor() == sendOptionsSplitter {
   131  				b.Delete(l + 1)
   132  			}
   133  		},
   134  	},
   135  }
   136  

View as plain text