Skip to content

Commit 8a124d0

Browse files
authored
Merge pull request #308 from Tyriar/294_scroll
Implement scrollPages, scrollToTop, scrollToBottom and add scroll tests
2 parents 9389f89 + 0ad02a4 commit 8a124d0

2 files changed

Lines changed: 117 additions & 0 deletions

File tree

src/xterm.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,6 +1307,28 @@ Terminal.prototype.scrollDisp = function(disp, suppressScrollEvent) {
13071307
this.refresh(0, this.rows - 1);
13081308
};
13091309

1310+
/**
1311+
* Scroll the display of the terminal by a number of pages.
1312+
* @param {number} pageCount The number of pages to scroll (negative scrolls up).
1313+
*/
1314+
Terminal.prototype.scrollPages = function(pageCount) {
1315+
this.scrollDisp(pageCount * (this.rows - 1));
1316+
}
1317+
1318+
/**
1319+
* Scrolls the display of the terminal to the top.
1320+
*/
1321+
Terminal.prototype.scrollToTop = function() {
1322+
this.scrollDisp(-this.ydisp);
1323+
}
1324+
1325+
/**
1326+
* Scrolls the display of the terminal to the bottom.
1327+
*/
1328+
Terminal.prototype.scrollToBottom = function() {
1329+
this.scrollDisp(this.ybase - this.ydisp);
1330+
}
1331+
13101332
/**
13111333
* Writes text to the terminal.
13121334
* @param {string} text The text to write to the terminal.

test/test.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,101 @@ describe('xterm.js', function() {
8888
});
8989
});
9090

91+
describe('scroll', function() {
92+
describe('scrollDisp', function() {
93+
var startYDisp;
94+
beforeEach(function() {
95+
for (var i = 0; i < xterm.rows * 2; i++) {
96+
xterm.writeln('test');
97+
}
98+
startYDisp = xterm.rows + 1;
99+
});
100+
it('should scroll a single line', function() {
101+
assert.equal(xterm.ydisp, startYDisp);
102+
xterm.scrollDisp(-1);
103+
assert.equal(xterm.ydisp, startYDisp - 1);
104+
xterm.scrollDisp(1);
105+
assert.equal(xterm.ydisp, startYDisp);
106+
});
107+
it('should scroll multiple lines', function() {
108+
assert.equal(xterm.ydisp, startYDisp);
109+
xterm.scrollDisp(-5);
110+
assert.equal(xterm.ydisp, startYDisp - 5);
111+
xterm.scrollDisp(5);
112+
assert.equal(xterm.ydisp, startYDisp);
113+
});
114+
it('should not scroll beyond the bounds of the buffer', function() {
115+
assert.equal(xterm.ydisp, startYDisp);
116+
xterm.scrollDisp(1);
117+
assert.equal(xterm.ydisp, startYDisp);
118+
for (var i = 0; i < startYDisp; i++) {
119+
xterm.scrollDisp(-1);
120+
}
121+
assert.equal(xterm.ydisp, 0);
122+
xterm.scrollDisp(-1);
123+
assert.equal(xterm.ydisp, 0);
124+
});
125+
});
126+
127+
describe('scrollPages', function() {
128+
var startYDisp;
129+
beforeEach(function() {
130+
for (var i = 0; i < xterm.rows * 3; i++) {
131+
xterm.writeln('test');
132+
}
133+
startYDisp = (xterm.rows * 2) + 1;
134+
});
135+
it('should scroll a single page', function() {
136+
assert.equal(xterm.ydisp, startYDisp);
137+
xterm.scrollPages(-1);
138+
assert.equal(xterm.ydisp, startYDisp - (xterm.rows - 1));
139+
xterm.scrollPages(1);
140+
assert.equal(xterm.ydisp, startYDisp);
141+
});
142+
it('should scroll a multiple pages', function() {
143+
assert.equal(xterm.ydisp, startYDisp);
144+
xterm.scrollPages(-2);
145+
assert.equal(xterm.ydisp, startYDisp - (xterm.rows - 1) * 2);
146+
xterm.scrollPages(2);
147+
assert.equal(xterm.ydisp, startYDisp);
148+
});
149+
});
150+
151+
describe('scrollToTop', function() {
152+
beforeEach(function() {
153+
for (var i = 0; i < xterm.rows * 3; i++) {
154+
xterm.writeln('test');
155+
}
156+
});
157+
it('should scroll to the top', function() {
158+
assert.notEqual(xterm.ydisp, 0);
159+
xterm.scrollToTop();
160+
assert.equal(xterm.ydisp, 0);
161+
});
162+
});
163+
164+
describe('scrollToBottom', function() {
165+
var startYDisp;
166+
beforeEach(function() {
167+
for (var i = 0; i < xterm.rows * 3; i++) {
168+
xterm.writeln('test');
169+
}
170+
startYDisp = (xterm.rows * 2) + 1;
171+
});
172+
it('should scroll to the bottom', function() {
173+
xterm.scrollDisp(-1);
174+
xterm.scrollToBottom();
175+
assert.equal(xterm.ydisp, startYDisp);
176+
xterm.scrollPages(-1);
177+
xterm.scrollToBottom();
178+
assert.equal(xterm.ydisp, startYDisp);
179+
xterm.scrollToTop();
180+
xterm.scrollToBottom();
181+
assert.equal(xterm.ydisp, startYDisp);
182+
});
183+
});
184+
});
185+
91186
describe('evaluateKeyEscapeSequence', function() {
92187
it('should return the correct escape sequence for unmodified keys', function() {
93188
// Backspace

0 commit comments

Comments
 (0)