Compare commits

...

2 Commits

Author SHA1 Message Date
Patrick Devine
b8af12ceaf feed the linter 2024-09-17 18:19:31 -07:00
Patrick Devine
6f041ddfa4 allow ctl-j to add a new line + fix multiline bracketed paste 2024-09-17 18:12:55 -07:00
3 changed files with 22 additions and 5 deletions

View File

@ -30,6 +30,11 @@ const (
MultilineSystem
)
const (
scannerPrompt = ">>> "
scannerAltPrompt = "... "
)
func generateInteractive(cmd *cobra.Command, opts runOptions) error {
usage := func() {
fmt.Fprintln(os.Stderr, "Available Commands:")
@ -111,8 +116,8 @@ func generateInteractive(cmd *cobra.Command, opts runOptions) error {
}
scanner, err := readline.New(readline.Prompt{
Prompt: ">>> ",
AltPrompt: "... ",
Prompt: scannerPrompt,
AltPrompt: scannerAltPrompt,
Placeholder: "Send a message (/? for help)",
AltPlaceholder: `Use """ to end multi-line input`,
})
@ -144,6 +149,11 @@ func generateInteractive(cmd *cobra.Command, opts runOptions) error {
scanner.Prompt.UseAlt = false
sb.Reset()
continue
case errors.Is(err, readline.ErrNewLineDetected):
sb.WriteString(line)
fmt.Fprintln(&sb)
scanner.Prompt.Prompt = scannerAltPrompt
continue
case err != nil:
return err
@ -169,7 +179,7 @@ func generateInteractive(cmd *cobra.Command, opts runOptions) error {
multiline = MultilineNone
scanner.Prompt.UseAlt = false
case strings.HasPrefix(line, `"""`):
case strings.HasPrefix(line, `"""`) && !scanner.Pasting:
line := strings.TrimPrefix(line, `"""`)
line, ok := strings.CutSuffix(line, `"""`)
sb.WriteString(line)
@ -433,7 +443,7 @@ func generateInteractive(cmd *cobra.Command, opts runOptions) error {
sb.WriteString(line)
}
if sb.Len() > 0 && multiline == MultilineNone {
if sb.Len() > 0 && strings.TrimSpace(sb.String()) != "" && multiline == MultilineNone {
newMessage := api.Message{Role: "user", Content: sb.String()}
if opts.MultiModal {
@ -464,6 +474,7 @@ func generateInteractive(cmd *cobra.Command, opts runOptions) error {
}
sb.Reset()
scanner.Prompt.Prompt = scannerPrompt
}
}
}

View File

@ -4,7 +4,10 @@ import (
"errors"
)
var ErrInterrupt = errors.New("Interrupt")
var (
ErrInterrupt = errors.New("Interrupt")
ErrNewLineDetected = errors.New("new line detected")
)
type InterruptError struct {
Line []rune

View File

@ -225,6 +225,9 @@ func (i *Instance) Readline() (string, error) {
buf.MoveToEnd()
fmt.Println()
if r == CharCtrlJ {
return output, ErrNewLineDetected
}
return output, nil
default:
if metaDel {