make linewrap still work when the terminal width has changed (#1350)

This commit is contained in:
Patrick Devine 2023-12-04 14:14:56 -08:00 committed by GitHub
parent 1f126afb2d
commit 2113c9d31a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -521,9 +521,17 @@ func generate(cmd *cobra.Command, opts generateOptions) error {
latest = response latest = response
if opts.WordWrap { termWidth, _, _ = term.GetSize(int(os.Stdout.Fd()))
if opts.WordWrap && termWidth >= 10 {
for _, ch := range response.Response { for _, ch := range response.Response {
if currentLineLength+1 > termWidth-5 { if currentLineLength+1 > termWidth-5 {
if len(wordBuffer) > termWidth-10 {
fmt.Printf("%s%c", wordBuffer, ch)
wordBuffer = ""
currentLineLength = 0
continue
}
// backtrack the length of the last word and clear to the end of the line // backtrack the length of the last word and clear to the end of the line
fmt.Printf("\x1b[%dD\x1b[K\n", len(wordBuffer)) fmt.Printf("\x1b[%dD\x1b[K\n", len(wordBuffer))
fmt.Printf("%s%c", wordBuffer, ch) fmt.Printf("%s%c", wordBuffer, ch)
@ -543,7 +551,10 @@ func generate(cmd *cobra.Command, opts generateOptions) error {
} }
} }
} else { } else {
fmt.Print(response.Response) fmt.Printf("%s%s", wordBuffer, response.Response)
if len(wordBuffer) > 0 {
wordBuffer = ""
}
} }
return nil return nil