package emulator import ( "strings" "github.com/c-bata/go-prompt" ) // Slice of common key bindings used for both connect and session prompt var commonKeyBindings = []prompt.KeyBind{ { // Ctrl+Left key Back one word Key: prompt.ControlLeft, Fn: func(b *prompt.Buffer) { b.CursorLeft(len([]rune(b.Document().GetWordBeforeCursorWithSpace()))) }, }, { // Ctrl+Right Key Forward one word Key: prompt.ControlRight, Fn: func(b *prompt.Buffer) { b.CursorRight(len([]rune(b.Document().GetWordAfterCursorWithSpace()))) }, }, } // Slice of key bindings exclusively used by the session prompt var sessionKeyBindings = []prompt.KeyBind{ { // Ctrl+S enter session prompt options Key: prompt.ControlS, Fn: func(b *prompt.Buffer) { // User should only be able to press Ctrl+S once if strings.Contains(b.Document().CurrentLine(), sendOptionsSplitter) { return } prompt.GoLineEnd(b) // splitter should be padded with spaces w := b.Document().GetWordBeforeCursorWithSpace() if len(w) == 0 || w[len(w)-1] != ' ' { b.InsertText(" ", false, true) } b.InsertText(sendOptionsSplitter+" ", false, true) }, }, // The following bindings override default key bindings to ensure the // sessionOptionSplitter cannot be broken/modified by a user. // This is required as the sessionOptionSplitter is currently multiple // characters wide, however it should be treated as a single character when // manipulating the command line. // Note they are all additive key bindings, they do not replace the original // mapping for the particular key { Key: prompt.Left, Fn: func(b *prompt.Buffer) { l := len(sendOptionsSplitter) if b.Document().GetWordBeforeCursor() == sendOptionsSplitter { b.CursorLeft(l + 1) } }, }, { // Emacs left Key: prompt.ControlB, Fn: func(b *prompt.Buffer) { l := len(sendOptionsSplitter) if b.Document().GetWordBeforeCursor() == sendOptionsSplitter { b.CursorLeft(l + 1) } }, }, { Key: prompt.Right, Fn: func(b *prompt.Buffer) { l := len(sendOptionsSplitter) if b.Document().GetWordAfterCursor() == sendOptionsSplitter { b.CursorRight(l + 1) } }, }, { // Same as Right Key: prompt.ControlF, Fn: func(b *prompt.Buffer) { l := len(sendOptionsSplitter) if b.Document().GetWordAfterCursor() == sendOptionsSplitter { b.CursorRight(l + 1) } }, }, { Key: prompt.Backspace, Fn: func(b *prompt.Buffer) { l := len(sendOptionsSplitter) if b.Document().GetWordBeforeCursor() == sendOptionsSplitter { b.DeleteBeforeCursor(l + 1) } }, }, { // Same as backspace Key: prompt.ControlH, Fn: func(b *prompt.Buffer) { l := len(sendOptionsSplitter) if b.Document().GetWordBeforeCursor() == sendOptionsSplitter { b.DeleteBeforeCursor(l + 1) } }, }, { // Same as Delete Key: prompt.ControlD, Fn: func(b *prompt.Buffer) { l := len(sendOptionsSplitter) if b.Document().GetWordAfterCursor() == sendOptionsSplitter { b.Delete(l + 1) } }, }, { Key: prompt.Delete, Fn: func(b *prompt.Buffer) { l := len(sendOptionsSplitter) if b.Document().GetWordAfterCursor() == sendOptionsSplitter { b.Delete(l + 1) } }, }, }