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 wordWrap = false
} }
fmt.Print(readline.StartBracketedPaste)
defer fmt.Printf(readline.EndBracketedPaste)
var multiLineBuffer string var multiLineBuffer string
for { for {

View file

@ -65,8 +65,7 @@ func (i *Instance) Readline() (string, error) {
var esc bool var esc bool
var escex bool var escex bool
var metaDel bool var metaDel bool
var bracketedPaste bool var pasteMode PasteMode
var ignoreEnter bool
var currentLineBuf []rune var currentLineBuf []rune
@ -111,7 +110,16 @@ func (i *Instance) Readline() (string, error) {
case KeyRight: case KeyRight:
buf.MoveRight() buf.MoveRight()
case CharBracketedPaste: 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: case KeyDel:
if buf.Size() > 0 { if buf.Size() > 0 {
buf.Delete() buf.Delete()
@ -141,10 +149,6 @@ func (i *Instance) Readline() (string, error) {
} }
switch r { switch r {
case CharBracketedPasteStart:
if bracketedPaste {
ignoreEnter = true
}
case CharEsc: case CharEsc:
esc = true esc = true
case CharInterrupt: case CharInterrupt:
@ -179,16 +183,19 @@ func (i *Instance) Readline() (string, error) {
case CharCtrlW: case CharCtrlW:
buf.DeleteWord() buf.DeleteWord()
case CharEnter: case CharEnter:
if !ignoreEnter { output := buf.String()
output := buf.String() if output != "" {
if output != "" { i.History.Add([]rune(output))
i.History.Add([]rune(output))
}
buf.MoveToEnd()
fmt.Println()
return output, nil
} }
fallthrough buf.MoveToEnd()
fmt.Println()
switch pasteMode {
case PasteModeStart:
output = `"""` + output
case PasteModeEnd:
output = output + `"""`
}
return output, nil
default: default:
if metaDel { if metaDel {
metaDel = false metaDel = false

View file

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