-
Notifications
You must be signed in to change notification settings - Fork 4
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
Fix escape sequence problem #9
Conversation
Reviewer's Guide by SourceryThis pull request fixes an issue where pressing CTRL-LEFT corrupted the buffer during tab completion. It introduces a new function, Sequence diagram for handling escape sequences in linenoiseEditFeedsequenceDiagram
participant linenoiseEditFeed
participant readCode
participant readEscapeSequence
participant completeLine
participant linenoiseEditMoveLeft
linenoiseEditFeed->>readCode: Reads input code
alt c == ESC
linenoiseEditFeed->>readEscapeSequence: Reads escape sequence
readEscapeSequence-->>linenoiseEditFeed: Returns esc_type
end
alt esc_type == ESC_LEFT
linenoiseEditFeed->>linenoiseEditMoveLeft: Moves cursor left
else esc_type == ESC_RIGHT
linenoiseEditFeed->>linenoiseEditMoveRight: Moves cursor right
else esc_type == ESC_HOME
linenoiseEditFeed->>linenoiseEditMoveHome: Moves cursor to home
else esc_type == ESC_END
linenoiseEditFeed->>linenoiseEditMoveEnd: Moves cursor to end
else esc_type == ESC_UP
linenoiseEditFeed->>linenoiseEditHistoryNext: Retrieves previous history entry
else esc_type == ESC_DOWN
linenoiseEditFeed->>linenoiseEditHistoryNext: Retrieves next history entry
else esc_type == ESC_DELETE
linenoiseEditFeed->>linenoiseEditDelete: Deletes character at cursor
else c == TAB and completionCallback != NULL
linenoiseEditFeed->>completeLine: Handles tab completion
completeLine-->>linenoiseEditFeed: Returns character
end
Sequence diagram for handling tab completion in completeLinesequenceDiagram
participant completeLine
participant linenoiseCompletions
participant linenoiseBeep
participant refreshLine
completeLine->>linenoiseCompletions: Generates completions
alt TAB key pressed
completeLine->>completeLine: Updates completion index
alt completion_idx == lc.len
completeLine->>linenoiseBeep: Beeps to indicate end of completions
end
else ESC key pressed
completeLine->>refreshLine: Re-shows original buffer
else other key pressed
completeLine->>completeLine: Updates buffer with completion
end
completeLine->>refreshLine: Shows completion or original buffer
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @yhirose - I've reviewed your changes - here's some feedback:
Overall Comments:
- Consider using a non-blocking read instead of
poll
with a timeout inreadEscapeSequence
. - The
readEscapeSequence
function could be improved by adding a loop to read all available bytes, ensuring that the entire escape sequence is processed.
Here's what I looked at during the review
- 🟢 General issues: all looks good
- 🟢 Security: all looks good
- 🟢 Testing: all looks good
- 🟡 Complexity: 1 issue found
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
@@ -1540,58 +1603,30 @@ const char *linenoiseEditFeed(struct linenoiseState *l) { | |||
linenoiseEditHistoryNext(l, LINENOISE_HISTORY_NEXT); | |||
break; | |||
case ESC: /* escape sequence */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (complexity): Consider extracting the escape handling logic into its own function to reduce nesting and improve code readability in both the completion and feed paths, isolating ESC_TYPE handling and simplifying the primary control flow in linenoiseEditFeed and completeLine functions..
Consider extracting the escape handling into its own function to flatten the nesting in both the completion and feed paths without reverting the abstractions. This isolates the ESC_TYPE handling and makes the primary control flow easier to follow. For example, you might refactor the ESC switch logic in linenoiseEditFeed as follows:
static void handleEscape(linenoiseState *l, ESC_TYPE esc_type) {
switch (esc_type) {
case ESC_DELETE:
linenoiseEditDelete(l);
break;
case ESC_UP:
linenoiseEditHistoryNext(l, LINENOISE_HISTORY_PREV);
break;
case ESC_DOWN:
linenoiseEditHistoryNext(l, LINENOISE_HISTORY_NEXT);
break;
case ESC_RIGHT:
linenoiseEditMoveRight(l);
break;
case ESC_LEFT:
linenoiseEditMoveLeft(l);
break;
case ESC_HOME:
linenoiseEditMoveHome(l);
break;
case ESC_END:
linenoiseEditMoveEnd(l);
break;
default:
break;
}
}
Then, in linenoiseEditFeed reduce nesting by calling:
if (c == ESC) {
auto esc_type = readEscapeSequence(l);
handleEscape(l, esc_type);
break;
}
Similarly, in your completeLine function, consider isolating the escape-specific branch into a helper if it gets too nested. This approach keeps all functionality intact while simplifying the main control flow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a good idea to suggest smaller functions like handleEscape . Would like to see this in a follow on PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have created an issue for your comment: #13
This is to fix the problem reported in the following issue and pull request.
antirez/linenoise#129
antirez/linenoise#230
You can reproduce this problem with the following steps:
mkdir build && cd build && meson .. && ninja && ./linenoise
)This PR is based on antirez/linenoise#230. It almost works, but it actually creates another problem described in the first comment in antirez/linenoise#129. I applied the suggestion in the comment to check the readability of the input fd.
Summary by Sourcery
Fixes an issue where escape sequences could corrupt the input buffer when using tab completion and pressing CTRL-LEFT. This is achieved by reading escape sequences using a poll to check the readability of the input file descriptor.
Bug Fixes: