Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public final class TerminalBuffer {
private int mActiveTranscriptRows = 0;
/** The index in the circular buffer where the visible screen starts. */
private int mScreenFirstRow = 0;
/** Monotonic document row coordinate corresponding to external row 0 (`mScreenFirstRow`). */
private long mBaseAbsoluteRow = 0L;


/**
Expand Down Expand Up @@ -265,7 +267,15 @@ public void clearLineWrap(int row) {
* @param newRows The number of rows the screen should have.
* @param cursor An int[2] containing the (column, row) cursor location.
*/
public long getBaseAbsoluteRow() {
return mBaseAbsoluteRow;
}

public void resize(int newColumns, int newRows, int newTotalRows, int[] cursor, long currentStyle, boolean altScreen) {
resize(newColumns, newRows, newTotalRows, cursor, currentStyle, altScreen, null);
}

public void resize(int newColumns, int newRows, int newTotalRows, int[] cursor, long currentStyle, boolean altScreen, int[] anchorExternalRow) {
// newRows > mTotalRows should not normally happen since mTotalRows is TRANSCRIPT_ROWS (10000):
if (newColumns == mColumns && newRows <= mTotalRows) {
// Fast resize where just the rows changed.
Expand Down Expand Up @@ -293,6 +303,12 @@ public void resize(int newColumns, int newRows, int newTotalRows, int[] cursor,
mScreenFirstRow = (mScreenFirstRow < 0) ? (mScreenFirstRow + mTotalRows) : (mScreenFirstRow % mTotalRows);
mTotalRows = newTotalRows;
mActiveTranscriptRows = altScreen ? 0 : Math.max(0, mActiveTranscriptRows + shiftDownOfTopRow);
if (!altScreen) {
mBaseAbsoluteRow += shiftDownOfTopRow;
}
if (anchorExternalRow != null) {
anchorExternalRow[0] = altScreen ? 0 : Math.max(-mActiveTranscriptRows, Math.min(newRows - 1, anchorExternalRow[0] - shiftDownOfTopRow));
}
cursor[1] -= shiftDownOfTopRow;
mScreenRows = newRows;
} else {
Expand All @@ -306,9 +322,11 @@ public void resize(int newColumns, int newRows, int newTotalRows, int[] cursor,
final int oldScreenFirstRow = mScreenFirstRow;
final int oldScreenRows = mScreenRows;
final int oldTotalRows = mTotalRows;
final int oldColumns = mColumns;
mTotalRows = newTotalRows;
mScreenRows = newRows;
mActiveTranscriptRows = mScreenFirstRow = 0;
mBaseAbsoluteRow = 0L;
mColumns = newColumns;

int newCursorRow = -1;
Expand All @@ -317,6 +335,10 @@ public void resize(int newColumns, int newRows, int newTotalRows, int[] cursor,
int oldCursorColumn = cursor[0];
boolean newCursorPlaced = false;

int oldAnchorRow = (anchorExternalRow != null) ? anchorExternalRow[0] : 0;
int newAnchorRow = 0;
boolean anchorPlaced = (anchorExternalRow == null);

int currentOutputExternalRow = 0;
int currentOutputExternalColumn = 0;

Expand All @@ -331,14 +353,17 @@ public void resize(int newColumns, int newRows, int newTotalRows, int[] cursor,

TerminalRow oldLine = oldLines[internalOldRow];
boolean cursorAtThisRow = externalOldRow == oldCursorRow;
boolean anchorAtThisRow = !anchorPlaced && externalOldRow >= oldAnchorRow;
// The cursor may only be on a non-null line, which we should not skip:
if (oldLine == null || (!(!newCursorPlaced && cursorAtThisRow)) && oldLine.isBlank()) {
if (!anchorAtThisRow && (oldLine == null || (!(!newCursorPlaced && cursorAtThisRow)) && oldLine.isBlank())) {
skippedBlankLines++;
continue;
} else if (skippedBlankLines > 0) {
// After skipping some blank lines we encounter a non-blank line. Insert the skipped blank lines.
for (int i = 0; i < skippedBlankLines; i++) {
if (currentOutputExternalRow == mScreenRows - 1) {
if (newCursorPlaced) newCursorRow--;
if (anchorPlaced) newAnchorRow--;
scrollDownOneLine(0, mScreenRows, currentStyle);
} else {
currentOutputExternalRow++;
Expand All @@ -348,6 +373,14 @@ public void resize(int newColumns, int newRows, int newTotalRows, int[] cursor,
skippedBlankLines = 0;
}

if (anchorAtThisRow) {
newAnchorRow = currentOutputExternalRow;
anchorPlaced = true;
}
if (oldLine == null) {
oldLine = new TerminalRow(oldColumns, currentStyle);
}

int lastNonSpaceIndex = 0;
boolean justToCursor = false;
if (cursorAtThisRow || oldLine.mLineWrap) {
Expand Down Expand Up @@ -376,6 +409,7 @@ public void resize(int newColumns, int newRows, int newTotalRows, int[] cursor,
setLineWrap(currentOutputExternalRow);
if (currentOutputExternalRow == mScreenRows - 1) {
if (newCursorPlaced) newCursorRow--;
if (anchorPlaced) newAnchorRow--;
scrollDownOneLine(0, mScreenRows, currentStyle);
} else {
currentOutputExternalRow++;
Expand All @@ -402,6 +436,7 @@ public void resize(int newColumns, int newRows, int newTotalRows, int[] cursor,
if (externalOldRow != (oldScreenRows - 1) && !oldLine.mLineWrap) {
if (currentOutputExternalRow == mScreenRows - 1) {
if (newCursorPlaced) newCursorRow--;
if (anchorPlaced) newAnchorRow--;
scrollDownOneLine(0, mScreenRows, currentStyle);
} else {
currentOutputExternalRow++;
Expand All @@ -412,6 +447,11 @@ public void resize(int newColumns, int newRows, int newTotalRows, int[] cursor,

cursor[0] = newCursorColumn;
cursor[1] = newCursorRow;
if (anchorExternalRow != null) {
anchorExternalRow[0] = anchorPlaced
? Math.max(-mActiveTranscriptRows, Math.min(mScreenRows - 1, newAnchorRow))
: 0;
}
}

// Handle cursor scrolling off screen:
Expand Down Expand Up @@ -458,6 +498,7 @@ public void scrollDownOneLine(int topMargin, int bottomMargin, long style) {

// Update the screen location in the ring buffer:
mScreenFirstRow = (mScreenFirstRow + 1) % mTotalRows;
mBaseAbsoluteRow++;
// Note that the history has grown if not already full:
if (mActiveTranscriptRows < mTotalRows - mScreenRows) mActiveTranscriptRows++;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,12 +427,18 @@ public final class TerminalEmulator {
*/
private int mScrollCounter = 0;

/** The cached value for this emulator for `TerminalView.mTopRow` as sessions/emulators may be switched.*/
private int mTopRow;
/** True when viewport is pinned to bottom (`0`) and follows new output automatically. */
private boolean mFollowOutput = true;

/** Continuous document row coordinate (`mMainBuffer.getBaseAbsoluteRow() + viewportOffset`) when off-bottom. */
private double mAnchoredDocRow = 0.0;

/** If automatic scrolling of terminal is disabled */
private boolean mAutoScrollDisabled;

/** Timestamp of last main-buffer line scroll, used for streaming bottom catch-snap. */
private long mLastScrollOutputNs = 0L;

private byte mUtf8ToFollow, mUtf8Index;
private final byte[] mUtf8InputBuffer = new byte[4];
private int mLastEmittedCodePoint = -1;
Expand Down Expand Up @@ -604,7 +610,20 @@ public void resize(int columns, int rows, int cellWidthPixels, int cellHeightPix
private void resizeScreen() {
final int[] cursor = {mCursorCol, mCursorRow};
int newTotalRows = (mScreen == mAltBuffer) ? mRows : mMainBuffer.mTotalRows;
mScreen.resize(mColumns, mRows, newTotalRows, cursor, getStyle(), isAlternateBufferActive());
if (mScreen == mMainBuffer && (!mFollowOutput || mAutoScrollDisabled)) {
double currentOffset = mAnchoredDocRow - mMainBuffer.getBaseAbsoluteRow();
int oldAnchorRow = (int) Math.floor(currentOffset);
double subRowFraction = currentOffset - oldAnchorRow;
int[] anchor = {Math.max(-mMainBuffer.getActiveTranscriptRows(), Math.min(0, oldAnchorRow))};
mMainBuffer.resize(mColumns, mRows, newTotalRows, cursor, getStyle(), false, anchor);
double newOffset = Math.min(0.0, anchor[0] + subRowFraction);
mAnchoredDocRow = mMainBuffer.getBaseAbsoluteRow() + newOffset;
if (newOffset >= -0.15 && !mAutoScrollDisabled) {
mFollowOutput = true;
}
} else {
mScreen.resize(mColumns, mRows, newTotalRows, cursor, getStyle(), isAlternateBufferActive());
}
mCursorCol = cursor[0];
mCursorRow = cursor[1];
}
Expand Down Expand Up @@ -2044,6 +2063,8 @@ private void doEsc(int b) {
case 'c': // RIS - Reset to Initial State (http://vt100.net/docs/vt510-rm/RIS).
reset();
mMainBuffer.clearTranscript();
mScrollCounter = 0;
pinViewportToBottom();
blockClear(0, 0, mColumns, mRows);
setCursorPosition(0, 0);
break;
Expand Down Expand Up @@ -2204,6 +2225,14 @@ private void doCsi(int b) {
break;
case 3: // Delete all lines saved in the scrollback buffer (xterm etc)
mMainBuffer.clearTranscript();
mScrollCounter = 0;
if (mScreen == mMainBuffer) {
if (!mAutoScrollDisabled) {
pinViewportToBottom();
} else {
mAnchoredDocRow = mMainBuffer.getBaseAbsoluteRow();
}
}
break;
default:
unknownSequence(b);
Expand Down Expand Up @@ -3071,6 +3100,9 @@ private void scrollDownOneLine() {
// .. and blank bottom row between margins:
mScreen.blockSet(mLeftMargin, mBottomMargin - 1, mRightMargin - mLeftMargin, 1, ' ', currentStyle);
} else {
if (mScreen == mMainBuffer) {
mLastScrollOutputNs = System.nanoTime();
}
mScreen.scrollDownOneLine(mTopMargin, mBottomMargin, currentStyle);
}
}
Expand Down Expand Up @@ -3463,22 +3495,70 @@ public void clearScrollCounter() {
mScrollCounter = 0;
}

public boolean isFollowingOutput() {
return mFollowOutput && !mAutoScrollDisabled;
}

public boolean hasRecentOutputScroll(long windowNs) {
return mLastScrollOutputNs != 0L && (System.nanoTime() - mLastScrollOutputNs) <= windowNs;
}

public boolean isAutoScrollDisabled() {
return mAutoScrollDisabled;
}

public void toggleAutoScrollDisabled() {
mAutoScrollDisabled = !mAutoScrollDisabled;
if (mAutoScrollDisabled) {
if (mFollowOutput) {
mAnchoredDocRow = mMainBuffer.getBaseAbsoluteRow();
mFollowOutput = false;
}
} else {
pinViewportToBottom();
}
}

public void pinViewportToBottom() {
mFollowOutput = true;
mAnchoredDocRow = mMainBuffer.getBaseAbsoluteRow();
}

public void freezeViewportAtOffset(double externalRowOffset) {
mFollowOutput = false;
int rowsInHistory = mMainBuffer.getActiveTranscriptRows();
double clamped = Math.max(-rowsInHistory, Math.min(0.0, externalRowOffset));
mAnchoredDocRow = mMainBuffer.getBaseAbsoluteRow() + clamped;
}

public void setViewportOffset(double externalRowOffset) {
if (externalRowOffset >= -0.15 && !mAutoScrollDisabled) {
pinViewportToBottom();
} else {
freezeViewportAtOffset(externalRowOffset);
}
}

public double getViewportOffset() {
if (isAlternateBufferActive()) return 0.0;
if (mFollowOutput && !mAutoScrollDisabled) return 0.0;
int rowsInHistory = mMainBuffer.getActiveTranscriptRows();
double offset = mAnchoredDocRow - mMainBuffer.getBaseAbsoluteRow();
if (offset < -rowsInHistory) {
offset = -rowsInHistory;
mAnchoredDocRow = mMainBuffer.getBaseAbsoluteRow() + offset;
} else if (offset > 0.0) {
return 0.0;
}
return offset;
}

public int getTopRow() {
return mTopRow;
return (int) Math.floor(getViewportOffset());
}

public void setTopRow(int topRow) {
mTopRow = topRow;
setViewportOffset(topRow);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package com.termux.terminal;

public class ScrollPreservationTest extends TerminalTestCase {

public void testFollowsOutputOnlyWhenAtBottom() {
withTerminalSized(3, 3).enterString("111222333444555");
assertEquals(2, mTerminal.getScreen().getActiveTranscriptRows());
assertTrue(mTerminal.isFollowingOutput());
assertEquals(0.0, mTerminal.getViewportOffset(), 1e-6);

// New output arrives while at bottom -> stays at 0:
enterString("666777");
assertEquals(4, mTerminal.getScreen().getActiveTranscriptRows());
assertEquals(0.0, mTerminal.getViewportOffset(), 1e-6);

// User scrolls 1.4 rows up into history (sub-row pixel offset preserved):
mTerminal.setViewportOffset(-1.4);
assertFalse(mTerminal.isFollowingOutput());
assertEquals(-1.4, mTerminal.getViewportOffset(), 1e-6);

// 2 more rows of output arrive -> viewport shifts by -2.0 so visible content stays stationary:
enterString("888999");
assertEquals(6, mTerminal.getScreen().getActiveTranscriptRows());
assertEquals(-3.4, mTerminal.getViewportOffset(), 1e-6);

// Minor sub-row jitter within 0.15 row of bottom snaps back to follow output:
mTerminal.setViewportOffset(-0.10);
assertTrue(mTerminal.isFollowingOutput());
assertEquals(0.0, mTerminal.getViewportOffset(), 1e-6);
enterString("AAABBB");
assertEquals(0.0, mTerminal.getViewportOffset(), 1e-6);
}

public void testResizeRowCountPreservesExactSubRowPosition() {
withTerminalSized(3, 4).enterString("000111222333444555666777888999");
assertEquals(6, mTerminal.getScreen().getActiveTranscriptRows());

mTerminal.setViewportOffset(-2.3);
assertEquals(-2.3, mTerminal.getViewportOffset(), 1e-6);

// Height shrinks from 4 rows to 2 rows (+2 lines pushed into transcript = 8):
resize(3, 2);
assertEquals(8, mTerminal.getScreen().getActiveTranscriptRows());
assertEquals(-4.3, mTerminal.getViewportOffset(), 1e-6);
assertEquals(3.7, mTerminal.getScreen().getBaseAbsoluteRow() + mTerminal.getViewportOffset(), 1e-6);

// Height expands back from 2 rows to 4 rows:
resize(3, 4);
assertEquals(6, mTerminal.getScreen().getActiveTranscriptRows());
assertEquals(-2.3, mTerminal.getViewportOffset(), 1e-6);
}

public void testColumnReflowPreservesAnchoredLine() {
withTerminalSized(6, 2).enterString("AAAAAA\r\nBBBBBB\r\nCCCCCC\r\nDDDDDD");
assertEquals(2, mTerminal.getScreen().getActiveTranscriptRows());

mTerminal.setViewportOffset(-1.0);
assertEquals("BBBBBB", mTerminal.getScreen().getSelectedText(0, mTerminal.getTopRow(), 6, mTerminal.getTopRow()).trim());

resize(3, 2);
assertEquals(6, mTerminal.getScreen().getActiveTranscriptRows());
assertEquals("BBB", mTerminal.getScreen().getSelectedText(0, mTerminal.getTopRow(), 3, mTerminal.getTopRow()).trim());
}

public void testCsi3JClearResetsViewportWithoutStrandingAtTopOnLongSessionResize() {
withTerminalSized(6, 4);
// Overflow default ring buffer capacity or multi-screen history, then scroll up:
for (int i = 0; i < 300; i++) {
enterString("L" + i + "\r\n");
}
mTerminal.setViewportOffset(-5.0);
assertFalse(mTerminal.isFollowingOutput());

// Window shrinks to narrower floating/split width and TUI wipes/replays via CSI 3 J:
resize(3, 3);
enterString("\033[H\033[2J\033[3J");
for (int i = 0; i < 300; i++) {
enterString("L" + i + "\r\n");
}
assertTrue(mTerminal.isFollowingOutput());
assertEquals(0.0, mTerminal.getViewportOffset(), 1e-6);
}

public void testAutoScrollDisabledAtBottomSurvivesOutputAndCsi3J() {
withTerminalSized(3, 3).enterString("111222333444555666777888999");
assertEquals(0.0, mTerminal.getViewportOffset(), 1e-6);

mTerminal.toggleAutoScrollDisabled();
assertTrue(mTerminal.isAutoScrollDisabled());

enterString("\033[H\033[2J\033[3J111222333444");
assertEquals(1, mTerminal.getScreen().getActiveTranscriptRows());
assertEquals(-1.0, mTerminal.getViewportOffset(), 1e-6);

mTerminal.toggleAutoScrollDisabled();
assertFalse(mTerminal.isAutoScrollDisabled());
assertEquals(0.0, mTerminal.getViewportOffset(), 1e-6);
}

}
Loading