Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add word wrapping for lines which are longer than the terminal width #553

Merged
merged 3 commits into from
Sep 22, 2023

Conversation

pdevine
Copy link
Contributor

@pdevine pdevine commented Sep 19, 2023

This change makes it so the REPL will properly wrap a line on a word boundary. The way it works is that it walks through each character of each token returned by the server, and then keeps a buffer of the last word. If the maximum boundary length is exceeded, it will backtrack using ANSI escape codes to the length of the current word buffer, erase to the end of the line, give a line feed, and then add the word fragment to the new line. This requires that the terminal allow ANSI graphics which should be OK for any modern terminal. If you run this headless, it will default to not wrapping any lines.

Right now I've set the width to 5 characters less than the terminal width, but we can potentially make this a setting in the future.

This fixes issue #150

Copy link
Contributor

@BruceMacD BruceMacD left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, my only suggestions is comments that explain what is going on

if wrapTerm {
for _, ch := range response.Response {
if currentLineLength+1 > termWidth-5 {
// backtrack the length of the last word and clear to the end of the line
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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
// \x1b[%dD: This ANSI escape code moves the cursor backward by the specified number of characters (in this case, len(wordBuffer)).
// \x1b[K: This ANSI escape code clears the line from the cursor to the end.

@pdevine pdevine merged commit c928ceb into main Sep 22, 2023
@pdevine pdevine deleted the wordwrap branch September 22, 2023 20:36
@mxyng mxyng mentioned this pull request Sep 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants