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

Preserve multiline prompt on 'clear buffer' gnachman/iterm2#1330 #249

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions iTerm2XCTests/VT100GridTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -1805,7 +1805,7 @@ - (void)testResetWithLineBufferLeavingBehindZero {
[lineBuffer setMaxLines:1];
int dropped = [grid resetWithLineBuffer:lineBuffer
unlimitedScrollback:NO
preserveCursorLine:NO];
preservePromptLines:0];
XCTAssert(dropped == 2);
XCTAssert([[grid compactLineDump] isEqualToString:
@"....\n"
Expand All @@ -1826,7 +1826,7 @@ - (void)testResetWithLineBufferLeavingBehindZero {
[lineBuffer setMaxLines:1];
dropped = [grid resetWithLineBuffer:lineBuffer
unlimitedScrollback:YES
preserveCursorLine:NO];
preservePromptLines:0];
XCTAssert(dropped == 0);
XCTAssert([[grid compactLineDump] isEqualToString:
@"....\n"
Expand All @@ -1841,7 +1841,7 @@ - (void)testResetWithLineBufferLeavingBehindZero {
[lineBuffer setMaxLines:1];
dropped = [grid resetWithLineBuffer:lineBuffer
unlimitedScrollback:YES
preserveCursorLine:NO];
preservePromptLines:0];
XCTAssert(dropped == 0);
XCTAssert([[grid compactLineDump] isEqualToString:
@"..\n"
Expand All @@ -1861,7 +1861,7 @@ - (void)testResetWithLineBufferLeavingBehindCursorLine {
[lineBuffer setMaxLines:1];
int dropped = [grid resetWithLineBuffer:lineBuffer
unlimitedScrollback:NO
preserveCursorLine:YES];
preservePromptLines:1];
XCTAssert(dropped == 2);
XCTAssert([[grid compactLineDump] isEqualToString:
@"....\n"
Expand All @@ -1887,7 +1887,7 @@ - (void)testResetWithLineBufferLeavingBehindCursorLine {
[lineBuffer setMaxLines:1];
dropped = [grid resetWithLineBuffer:lineBuffer
unlimitedScrollback:NO
preserveCursorLine:YES];
preservePromptLines:1];
XCTAssert(dropped == 1);
XCTAssert([[grid compactLineDump] isEqualToString:
@"efgh\n"
Expand All @@ -1913,7 +1913,7 @@ - (void)testResetWithLineBufferLeavingBehindCursorLine {
[lineBuffer setMaxLines:1];
dropped = [grid resetWithLineBuffer:lineBuffer
unlimitedScrollback:NO
preserveCursorLine:YES];
preservePromptLines:1];
XCTAssert(dropped == 0);
XCTAssert([[grid compactLineDump] isEqualToString:
@"abcd\n"
Expand All @@ -1935,7 +1935,7 @@ - (void)testResetWithLineBufferLeavingBehindCursorLine {
[lineBuffer setMaxLines:1];
dropped = [grid resetWithLineBuffer:lineBuffer
unlimitedScrollback:YES
preserveCursorLine:YES];
preservePromptLines:1];
XCTAssert(dropped == 0);
XCTAssert([[grid compactLineDump] isEqualToString:
@"....\n"
Expand All @@ -1950,7 +1950,7 @@ - (void)testResetWithLineBufferLeavingBehindCursorLine {
[lineBuffer setMaxLines:1];
dropped = [grid resetWithLineBuffer:lineBuffer
unlimitedScrollback:YES
preserveCursorLine:YES];
preservePromptLines:1];
XCTAssert(dropped == 0);
XCTAssert([[grid compactLineDump] isEqualToString:
@"..\n"
Expand Down
2 changes: 1 addition & 1 deletion sources/VT100Grid.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
// for |leave|.
- (int)resetWithLineBuffer:(LineBuffer *)lineBuffer
unlimitedScrollback:(BOOL)unlimitedScrollback
preserveCursorLine:(BOOL)preserveCursorLine;
preservePromptLines:(int)preservePromptLines;

// Move the grid contents up, leaving only the whole wrapped line the cursor is on at the top.
- (void)moveWrappedCursorLineToTopOfGrid;
Expand Down
8 changes: 4 additions & 4 deletions sources/VT100Grid.m
Original file line number Diff line number Diff line change
Expand Up @@ -388,12 +388,12 @@ - (int)scrollUpIntoLineBuffer:(LineBuffer *)lineBuffer

- (int)resetWithLineBuffer:(LineBuffer *)lineBuffer
unlimitedScrollback:(BOOL)unlimitedScrollback
preserveCursorLine:(BOOL)preserveCursorLine {
preservePromptLines:(int)preservePromptLines {
self.scrollRegionRows = VT100GridRangeMake(0, size_.height);
self.scrollRegionCols = VT100GridRangeMake(0, size_.width);
int numLinesToScroll;
if (preserveCursorLine) {
numLinesToScroll = cursor_.y;
if (preservePromptLines > 0 && preservePromptLines < size_.height) {
numLinesToScroll = MAX(0, cursor_.y - preservePromptLines + 1);
} else {
numLinesToScroll = [self lineNumberOfLastNonEmptyLine] + 1;
}
Expand All @@ -405,7 +405,7 @@ - (int)resetWithLineBuffer:(LineBuffer *)lineBuffer
}
self.cursor = VT100GridCoordMake(0, 0);

[self setCharsFrom:VT100GridCoordMake(0, preserveCursorLine ? 1 : 0)
[self setCharsFrom:VT100GridCoordMake(0, preservePromptLines)
Copy link
Owner

Choose a reason for hiding this comment

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

This will probably explode if preservePromptLines >= size_.height, so constrain it to not exceed size_.height - 1.

Copy link
Author

Choose a reason for hiding this comment

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

Agreed, fix this in line 395 above - add check for preservePromptLines < size_.height.

to:VT100GridCoordMake(size_.width - 1, size_.height - 1)
toChar:[self defaultChar]];

Expand Down
21 changes: 19 additions & 2 deletions sources/VT100Screen.m
Original file line number Diff line number Diff line change
Expand Up @@ -848,13 +848,30 @@ - (void)clearBuffer {
// This clears the screen, leaving the cursor's line at the top and preserves the cursor's x
// coordinate. Scroll regions and the saved cursor position are reset.
- (void)clearAndResetScreenPreservingCursorLine {
int numberOfLinesInPrompt;
if (_shellIntegrationInstalled) {
numberOfLinesInPrompt = currentGrid_.cursorY + [self numberOfScrollbackLines] + [self totalScrollbackOverflow] - _lastCommandOutputRange.end.y + 1;
DLog(@"numberOfLinesInPrompt calculated (based on shell integration): %d", numberOfLinesInPrompt);
} else {
numberOfLinesInPrompt = [iTermAdvancedSettingsModel numberOfLinesInPrompt];
DLog(@"numberOfLinesInPrompt taken from advanced settings: %d", numberOfLinesInPrompt);
}

if (numberOfLinesInPrompt <= 0) {
DLog(@"iTerm advanced settings variable 'numberOfLinesInPrompt' is equal or less than than zero: %d", numberOfLinesInPrompt);
numberOfLinesInPrompt = 1;
} else if (numberOfLinesInPrompt > 3) {
DLog(@"iTerm advanced settings variable 'numberOfLinesInPrompt' is greater than 3: %d", numberOfLinesInPrompt);
}

[delegate_ screenTriggerableChangeDidOccur];
// This clears the screen.
int x = currentGrid_.cursorX;
[self incrementOverflowBy:[currentGrid_ resetWithLineBuffer:linebuffer_
unlimitedScrollback:unlimitedScrollback_
preserveCursorLine:YES]];
preservePromptLines:numberOfLinesInPrompt]];
currentGrid_.cursorX = x;
currentGrid_.cursorY = numberOfLinesInPrompt - 1;
}

- (void)clearScrollbackBuffer
Expand Down Expand Up @@ -2586,7 +2603,7 @@ - (void)terminalResetPreservingPrompt:(BOOL)preservePrompt {
} else {
[self incrementOverflowBy:[currentGrid_ resetWithLineBuffer:linebuffer_
unlimitedScrollback:unlimitedScrollback_
preserveCursorLine:NO]];
preservePromptLines:0]];
}

[self setInitialTabStops];
Expand Down
1 change: 1 addition & 0 deletions sources/iTermAdvancedSettingsModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
+ (BOOL)useOpenDirectory;
+ (BOOL)disallowCopyEmptyString;
+ (BOOL)profilesWindowJoinsActiveSpace;
+ (int)numberOfLinesInPrompt;

+ (NSString *)badgeFont;
+ (BOOL)badgeFontIsBold;
Expand Down
1 change: 1 addition & 0 deletions sources/iTermAdvancedSettingsModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ + (NSString *)name { \
DEFINE_INT(triggerRadius, 3, @"Terminal: Number of screen lines to match against trigger regular expressions.\nTrigger regular expressions are matched against the last logical line of text when a newline is received. A search is performed to find the start of the line. Since very long lines would cause performance problems, the search (and consequently the regular expression match, highlighting, and so on) is limited to this many screen lines.");
DEFINE_BOOL(requireCmdForDraggingText, NO, @"Terminal: To drag images or selected text, you must hold ⌘. This prevents accidental drags.");
DEFINE_BOOL(focusReportingEnabled, YES, @"Terminal: Apps may turn on Focus Reporting.\nFocus reporting causes iTerm2 to send an escape sequence when a session gains or loses focus. It can cause problems when an ssh session dies unexpectedly because it gets left on, so some users prefer to disable it.");
DEFINE_INT(numberOfLinesInPrompt, 1, @"Terminal: Number of lines in prompt.\n\"Clear Buffer\" command clears all output except prompt lines. If prompt consist of more than one line, all lines except last will be cleared. You could set this variable to prompt lines count to avoid this behavior. This param is ignored if Shell Integration is installed.");

#pragma mark Hotkey
DEFINE_FLOAT(hotkeyTermAnimationDuration, 0.25, @"Hotkey: Duration in seconds of the hotkey window animation.\nWarning: reducing this value may cause problems if you have multiple displays.");
Expand Down