Added support for searching by regular expression

This commit is contained in:
Alex Ross
2018-09-07 09:47:15 -07:00
parent 3a310f1597
commit 88205511e6
5 changed files with 35 additions and 20 deletions
+1
View File
@@ -16,6 +16,7 @@
<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>
+2 -2
View File
@@ -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);
}
});
+2 -2
View File
@@ -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;
}
+20 -8
View File
@@ -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 {
if (!term || term.length === 0) {
return false;
}
@@ -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;
}
@@ -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;
}
@@ -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;
}
@@ -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;
}
@@ -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;
}
@@ -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++) {
+10 -8
View File
@@ -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);
};
}