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

Added support for searching by regular expression #1666

Merged
merged 4 commits into from Sep 8, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions demo/index.html
Expand Up @@ -16,6 +16,7 @@ <h3>Actions</h3>
<p>
<label>Find next <input id="find-next"/></label>
<label>Find previous <input id="find-previous"/></label>
<label>Use regex<input type="checkbox" id="regex"/></label>
</p>
</div>
<div>
Expand Down
4 changes: 2 additions & 2 deletions demo/main.js
Expand Up @@ -83,13 +83,13 @@ function createTerminal() {
addDomListener(actionElements.findNext, 'keypress', function (e) {
if (e.key === "Enter") {
e.preventDefault();
term.findNext(actionElements.findNext.value);
term.findNext(actionElements.findNext.value, document.getElementById('regex').checked);
}
});
addDomListener(actionElements.findPrevious, 'keypress', function (e) {
if (e.key === "Enter") {
e.preventDefault();
term.findPrevious(actionElements.findPrevious.value);
term.findPrevious(actionElements.findPrevious.value, document.getElementById('regex').checked);
}
});

Expand Down
4 changes: 2 additions & 2 deletions src/addons/search/Interfaces.ts
Expand Up @@ -17,6 +17,6 @@ export interface ISearchAddonTerminal extends Terminal {
}

export interface ISearchHelper {
findNext(term: string): boolean;
findPrevious(term: string): boolean;
findNext(term: string, regex: boolean): boolean;
findPrevious(term: string, regex: boolean): boolean;
}
28 changes: 20 additions & 8 deletions src/addons/search/SearchHelper.ts
Expand Up @@ -26,9 +26,10 @@ export class SearchHelper implements ISearchHelper {
* Find the next instance of the term, then scroll to and select it. If it
* doesn't exist, do nothing.
* @param term Tne search term.
* @param regex Should use regular expressions
* @return Whether a result was found.
*/
public findNext(term: string): boolean {
public findNext(term: string, regex: boolean = false): boolean {
alexr00 marked this conversation as resolved.
Show resolved Hide resolved
if (!term || term.length === 0) {
return false;
}
Expand All @@ -43,7 +44,7 @@ export class SearchHelper implements ISearchHelper {

// Search from ydisp + 1 to end
for (let y = startRow + 1; y < this._terminal._core.buffer.ybase + this._terminal.rows; y++) {
result = this._findInLine(term, y);
result = this._findInLine(term, y, regex);
if (result) {
break;
}
Expand All @@ -52,7 +53,7 @@ export class SearchHelper implements ISearchHelper {
// Search from the top to the current ydisp
if (!result) {
for (let y = 0; y < startRow; y++) {
result = this._findInLine(term, y);
result = this._findInLine(term, y, regex);
if (result) {
break;
}
Expand All @@ -67,9 +68,10 @@ export class SearchHelper implements ISearchHelper {
* Find the previous instance of the term, then scroll to and select it. If it
* doesn't exist, do nothing.
* @param term Tne search term.
* @param regex Should use regular expressions
* @return Whether a result was found.
*/
public findPrevious(term: string): boolean {
public findPrevious(term: string, regex: boolean = false): boolean {
if (!term || term.length === 0) {
return false;
}
Expand All @@ -84,7 +86,7 @@ export class SearchHelper implements ISearchHelper {

// Search from ydisp + 1 to end
for (let y = startRow - 1; y >= 0; y--) {
result = this._findInLine(term, y);
result = this._findInLine(term, y, regex);
if (result) {
break;
}
Expand All @@ -93,7 +95,7 @@ export class SearchHelper implements ISearchHelper {
// Search from the top to the current ydisp
if (!result) {
for (let y = this._terminal._core.buffer.ybase + this._terminal.rows - 1; y > startRow; y--) {
result = this._findInLine(term, y);
result = this._findInLine(term, y, regex);
if (result) {
break;
}
Expand All @@ -108,12 +110,22 @@ export class SearchHelper implements ISearchHelper {
* Searches a line for a search term.
* @param term Tne search term.
* @param y The line to search.
* @param regex Should use regular expressions
* @return The search result if it was found.
*/
private _findInLine(term: string, y: number): ISearchResult {
private _findInLine(term: string, y: number, regex: boolean): ISearchResult {
const lowerStringLine = this._terminal._core.buffer.translateBufferLineToString(y, true).toLowerCase();
const lowerTerm = term.toLowerCase();
let searchIndex = lowerStringLine.indexOf(lowerTerm);
let searchIndex = -1;
if (regex) {
const searchRegex = RegExp(lowerTerm, 'g');
const foundTerm = searchRegex.exec(lowerStringLine);
if (foundTerm) {
searchIndex = searchRegex.lastIndex - foundTerm[0].length;
}
} else {
searchIndex = lowerStringLine.indexOf(lowerTerm);
}
if (searchIndex >= 0) {
const line = this._terminal._core.buffer.lines.get(y);
for (let i = 0; i < searchIndex; i++) {
Expand Down
17 changes: 17 additions & 0 deletions src/addons/search/search.test.ts
Expand Up @@ -46,4 +46,21 @@ describe('search addon', function(): void {
expect(hello1).eql(undefined);
expect(hello2).eql({col: 11, row: 2, term: 'Hello'});
});
it('should respect search regex', function(): void {
search.apply(<any>MockTerminal);
const term = new MockTerminal({cols: 10, rows: 3});
term.core.write('abcdefghijklmnopqrstuvwxyz');
/*
abcdefghij
klmnopqrst
uvwxyz
*/
term.pushWriteData();
const hello0 = (term.searchHelper as any)._findInLine('dee*', 0, true);
const hello1 = (term.searchHelper as any)._findInLine('jkk*', 0, true);
const hello2 = (term.searchHelper as any)._findInLine('mnn*', 1, true);
expect(hello0).eql({col: 3, row: 0, term: 'dee*'});
// TODO: uncomment this test when line wrap search is checked in expect(hello1).eql({col: 9, row: 0, term: 'jkk*'});
// TODO: uncomment this test when line wrap search is checked in expect(hello2).eql(undefined);
});
});
18 changes: 10 additions & 8 deletions src/addons/search/search.ts
Expand Up @@ -11,36 +11,38 @@ import { ISearchAddonTerminal } from './Interfaces';
* Find the next instance of the term, then scroll to and select it. If it
* doesn't exist, do nothing.
* @param term Tne search term.
* @param regex Should use regular expressions
* @return Whether a result was found.
*/
export function findNext(terminal: Terminal, term: string): boolean {
export function findNext(terminal: Terminal, term: string, regex: boolean): boolean {
const addonTerminal = <ISearchAddonTerminal>terminal;
if (!addonTerminal.__searchHelper) {
addonTerminal.__searchHelper = new SearchHelper(addonTerminal);
}
return addonTerminal.__searchHelper.findNext(term);
return addonTerminal.__searchHelper.findNext(term, regex);
}

/**
* Find the previous instance of the term, then scroll to and select it. If it
* doesn't exist, do nothing.
* @param term Tne search term.
* @param regex Should use regular expressions
* @return Whether a result was found.
*/
export function findPrevious(terminal: Terminal, term: string): boolean {
export function findPrevious(terminal: Terminal, term: string, regex: boolean): boolean {
const addonTerminal = <ISearchAddonTerminal>terminal;
if (!addonTerminal.__searchHelper) {
addonTerminal.__searchHelper = new SearchHelper(addonTerminal);
}
return addonTerminal.__searchHelper.findPrevious(term);
return addonTerminal.__searchHelper.findPrevious(term, regex);
}

export function apply(terminalConstructor: typeof Terminal): void {
(<any>terminalConstructor.prototype).findNext = function(term: string): boolean {
return findNext(this, term);
(<any>terminalConstructor.prototype).findNext = function(term: string, regex: boolean): boolean {
return findNext(this, term, regex);
};

(<any>terminalConstructor.prototype).findPrevious = function(term: string): boolean {
return findPrevious(this, term);
(<any>terminalConstructor.prototype).findPrevious = function(term: string, regex: boolean): boolean {
return findPrevious(this, term, regex);
};
}