add bracketed paste mode (#922)

This commit is contained in:
Patrick Devine 2023-10-26 15:57:00 -07:00 committed by GitHub
parent 9bc5864a03
commit a79f030e75
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 18 deletions

View file

@ -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 {

View file

@ -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

View file

@ -72,6 +72,14 @@ const (
const (
CharBracketedPaste = 50
CharBracketedPasteStart = 0
CharBracketedPasteEnd = 1
CharBracketedPasteStart = "00~"
CharBracketedPasteEnd = "01~"
)
type PasteMode int
const (
PastModeOff = iota
PasteModeStart
PasteModeEnd
)