add bracketed paste mode (#922)
This commit is contained in:
parent
9bc5864a03
commit
a79f030e75
3 changed files with 36 additions and 18 deletions
|
@ -553,6 +553,9 @@ func generateInteractive(cmd *cobra.Command, model string) error {
|
|||
wordWrap = false
|
||||
}
|
||||
|
||||
fmt.Print(readline.StartBracketedPaste)
|
||||
defer fmt.Printf(readline.EndBracketedPaste)
|
||||
|
||||
var multiLineBuffer string
|
||||
|
||||
for {
|
||||
|
|
|
@ -65,8 +65,7 @@ func (i *Instance) Readline() (string, error) {
|
|||
var esc bool
|
||||
var escex bool
|
||||
var metaDel bool
|
||||
var bracketedPaste bool
|
||||
var ignoreEnter bool
|
||||
var pasteMode PasteMode
|
||||
|
||||
var currentLineBuf []rune
|
||||
|
||||
|
@ -111,7 +110,16 @@ func (i *Instance) Readline() (string, error) {
|
|||
case KeyRight:
|
||||
buf.MoveRight()
|
||||
case CharBracketedPaste:
|
||||
bracketedPaste = true
|
||||
var code string
|
||||
for cnt := 0; cnt < 3; cnt++ {
|
||||
r = i.Terminal.ReadRune()
|
||||
code += string(r)
|
||||
}
|
||||
if code == CharBracketedPasteStart {
|
||||
pasteMode = PasteModeStart
|
||||
} else if code == CharBracketedPasteEnd {
|
||||
pasteMode = PasteModeEnd
|
||||
}
|
||||
case KeyDel:
|
||||
if buf.Size() > 0 {
|
||||
buf.Delete()
|
||||
|
@ -141,10 +149,6 @@ func (i *Instance) Readline() (string, error) {
|
|||
}
|
||||
|
||||
switch r {
|
||||
case CharBracketedPasteStart:
|
||||
if bracketedPaste {
|
||||
ignoreEnter = true
|
||||
}
|
||||
case CharEsc:
|
||||
esc = true
|
||||
case CharInterrupt:
|
||||
|
@ -179,16 +183,19 @@ func (i *Instance) Readline() (string, error) {
|
|||
case CharCtrlW:
|
||||
buf.DeleteWord()
|
||||
case CharEnter:
|
||||
if !ignoreEnter {
|
||||
output := buf.String()
|
||||
if output != "" {
|
||||
i.History.Add([]rune(output))
|
||||
}
|
||||
buf.MoveToEnd()
|
||||
fmt.Println()
|
||||
return output, nil
|
||||
switch pasteMode {
|
||||
case PasteModeStart:
|
||||
output = `"""` + output
|
||||
case PasteModeEnd:
|
||||
output = output + `"""`
|
||||
}
|
||||
fallthrough
|
||||
return output, nil
|
||||
default:
|
||||
if metaDel {
|
||||
metaDel = false
|
||||
|
|
|
@ -72,6 +72,14 @@ const (
|
|||
|
||||
const (
|
||||
CharBracketedPaste = 50
|
||||
CharBracketedPasteStart = 0
|
||||
CharBracketedPasteEnd = 1
|
||||
CharBracketedPasteStart = "00~"
|
||||
CharBracketedPasteEnd = "01~"
|
||||
)
|
||||
|
||||
type PasteMode int
|
||||
|
||||
const (
|
||||
PastModeOff = iota
|
||||
PasteModeStart
|
||||
PasteModeEnd
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue