BufferStringIterator

This commit is contained in:
Jörg Breitbart
2018-09-09 22:17:49 +02:00
parent a8fe4107a3
commit a494e80a9f
3 changed files with 74 additions and 36 deletions
+38 -3
View File
@@ -4,7 +4,7 @@
*/
import { CircularList } from './common/CircularList';
import { CharData, ITerminal, IBuffer, IBufferLine } from './Types';
import { CharData, ITerminal, IBuffer, IBufferLine, BufferIndex, IBufferStringIterator } from './Types';
import { EventEmitter } from './common/EventEmitter';
import { IMarker } from 'xterm';
import { BufferLine } from './BufferLine';
@@ -194,12 +194,12 @@ export class Buffer implements IBuffer {
this.scrollBottom = newRows - 1;
}
public stringIndexToBufferIndex(lineIndex: number, stringIndex: number): number[] {
public stringIndexToBufferIndex(lineIndex: number, stringIndex: number): BufferIndex {
if (!stringIndex) {
return [lineIndex, 0];
}
while (stringIndex) {
let line = this.lines.get(lineIndex);
const line = this.lines.get(lineIndex);
if (!line) {
[-1, -1];
}
@@ -360,6 +360,10 @@ export class Buffer implements IBuffer {
// TODO: This could probably be optimized by relying on sort order and trimming the array using .length
this.markers.splice(this.markers.indexOf(marker), 1);
}
public contents(trimRight: boolean, startIndex?: number, endIndex?: number): IBufferStringIterator {
return new BufferStringIterator(this, trimRight, startIndex, endIndex);
}
}
export class Marker extends EventEmitter implements IMarker {
@@ -386,3 +390,34 @@ export class Marker extends EventEmitter implements IMarker {
super.dispose();
}
}
export class BufferStringIterator implements IBufferStringIterator {
private _start: number;
private _end: number;
private _current: number;
constructor (private _buffer: IBuffer, private _trimRight: boolean, startIndex?: number, endIndex?: number) {
this._start = startIndex || 0;
this._end = endIndex || this._buffer.lines.length;
this._current = this._start;
}
public hasNext(): boolean {
return this._current < this._end;
}
public next(withRanges: boolean = false): string | [{first: number, last: number}, string] {
const range = this._buffer.getWrappedRangeForLine(this._current);
let result = '';
for (let i = range.first; i <= range.last; ++i) {
result += this._buffer.translateBufferLineToString(i, (this._trimRight) ? i === range.last : false);
}
this._current = range.last;
this._current++;
return (withRanges) ? [range, result] : result;
}
toArray(): string[] {
const result: string[] = [];
while (this.hasNext()) {
result.push(this.next() as string);
}
return result;
}
}