Skip to content

Commit

Permalink
Add Terminal.reflowEnabled #104
Browse files Browse the repository at this point in the history
  • Loading branch information
xtyxtyx committed Oct 30, 2022
1 parent df6cc30 commit 7e01a69
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/src/core/buffer/buffer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -433,14 +433,16 @@ class Buffer {
_cursorX = _cursorX.clamp(0, newWidth - 1);
_cursorY = _cursorY.clamp(0, newHeight - 1);

if (!isAltBuffer && newWidth != oldWidth) {
final reflowResult = reflow(lines, oldWidth, newWidth);
if (terminal.reflowEnabled) {
if (!isAltBuffer && newWidth != oldWidth) {
final reflowResult = reflow(lines, oldWidth, newWidth);

while (reflowResult.length < newHeight) {
reflowResult.add(_newEmptyLine(newWidth));
}
while (reflowResult.length < newHeight) {
reflowResult.add(_newEmptyLine(newWidth));
}

lines.replaceWith(reflowResult);
lines.replaceWith(reflowResult);
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions lib/src/core/state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ abstract class TerminalState {

CursorStyle get cursor;

bool get reflowEnabled;

/* Modes */

bool get insertMode;
Expand Down
6 changes: 6 additions & 0 deletions lib/src/terminal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class Terminal with Observable implements TerminalState, EscapeHandler {
this.platform = TerminalTargetPlatform.unknown,
this.inputHandler = defaultInputHandler,
this.mouseHandler = defaultMouseHandler,
this.reflowEnabled = true,
});

TerminalMouseHandler? mouseHandler;
Expand Down Expand Up @@ -166,6 +167,11 @@ class Terminal with Observable implements TerminalState, EscapeHandler {

CircularList<BufferLine> get lines => _buffer.lines;

/// Whether the terminal performs reflow when the viewport size changes or
/// simply truncates lines. true by default.
@override
bool reflowEnabled;

void write(String data) {
_parser.write(data);
notifyListeners();
Expand Down
36 changes: 36 additions & 0 deletions test/src/terminal_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,42 @@ void main() {
expect(output, ['\x1B[M +,']);
});
});

group('Terminal.reflowEnabled', () {
test('prevents reflow when set to false', () {
final terminal = Terminal(reflowEnabled: false);

terminal.write('Hello World');
terminal.resize(5, 5);

expect(terminal.buffer.lines[0].toString(), 'Hello');
expect(terminal.buffer.lines[1].toString(), isEmpty);
});

test('preserves hidden cells when reflow is disabled', () {
final terminal = Terminal(reflowEnabled: false);

terminal.write('Hello World');
terminal.resize(5, 5);
terminal.resize(20, 5);

expect(terminal.buffer.lines[0].toString(), 'Hello World');
expect(terminal.buffer.lines[1].toString(), isEmpty);
});

test('can be set at runtime', () {
final terminal = Terminal(reflowEnabled: true);

terminal.resize(5, 5);
terminal.write('Hello World');
terminal.reflowEnabled = false;
terminal.resize(20, 5);

expect(terminal.buffer.lines[0].toString(), 'Hello');
expect(terminal.buffer.lines[1].toString(), ' Worl');
expect(terminal.buffer.lines[2].toString(), 'd');
});
});
}

class _TestInputHandler implements TerminalInputHandler {
Expand Down

0 comments on commit 7e01a69

Please sign in to comment.