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> <p>
<label>Find next <input id="find-next"/></label> <label>Find next <input id="find-next"/></label>
<label>Find previous <input id="find-previous"/></label> <label>Find previous <input id="find-previous"/></label>
<label>Use regex<input type="checkbox" id="regex"/></label>
</p> </p>
</div> </div>
<div> <div>
+2 -2
View File
@@ -83,13 +83,13 @@ function createTerminal() {
addDomListener(actionElements.findNext, 'keypress', function (e) { addDomListener(actionElements.findNext, 'keypress', function (e) {
if (e.key === "Enter") { if (e.key === "Enter") {
e.preventDefault(); e.preventDefault();
term.findNext(actionElements.findNext.value); term.findNext(actionElements.findNext.value, document.getElementById('regex').checked);
} }
}); });
addDomListener(actionElements.findPrevious, 'keypress', function (e) { addDomListener(actionElements.findPrevious, 'keypress', function (e) {
if (e.key === "Enter") { if (e.key === "Enter") {
e.preventDefault(); 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 { export interface ISearchHelper {
findNext(term: string): boolean; findNext(term: string, regex: boolean): boolean;
findPrevious(term: string): 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 * Find the next instance of the term, then scroll to and select it. If it
* doesn't exist, do nothing. * doesn't exist, do nothing.
* @param term Tne search term. * @param term Tne search term.
* @param regex Should use regular expressions
* @return Whether a result was found. * @return Whether a result was found.
*/ */
public findNext(term: string): boolean { public findNext(term: string, regex: boolean = false): boolean {
if (!term || term.length === 0) { if (!term || term.length === 0) {
return false; return false;
} }
@@ -43,7 +44,7 @@ export class SearchHelper implements ISearchHelper {
// Search from ydisp + 1 to end // Search from ydisp + 1 to end
for (let y = startRow + 1; y < this._terminal._core.buffer.ybase + this._terminal.rows; y++) { 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) { if (result) {
break; break;
} }
@@ -52,7 +53,7 @@ export class SearchHelper implements ISearchHelper {
// Search from the top to the current ydisp // Search from the top to the current ydisp
if (!result) { if (!result) {
for (let y = 0; y < startRow; y++) { for (let y = 0; y < startRow; y++) {
result = this._findInLine(term, y); result = this._findInLine(term, y, regex);
if (result) { if (result) {
break; 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 * Find the previous instance of the term, then scroll to and select it. If it
* doesn't exist, do nothing. * doesn't exist, do nothing.
* @param term Tne search term. * @param term Tne search term.
* @param regex Should use regular expressions
* @return Whether a result was found. * @return Whether a result was found.
*/ */
public findPrevious(term: string): boolean { public findPrevious(term: string, regex: boolean = false): boolean {
if (!term || term.length === 0) { if (!term || term.length === 0) {
return false; return false;
} }
@@ -84,7 +86,7 @@ export class SearchHelper implements ISearchHelper {
// Search from ydisp + 1 to end // Search from ydisp + 1 to end
for (let y = startRow - 1; y >= 0; y--) { for (let y = startRow - 1; y >= 0; y--) {
result = this._findInLine(term, y); result = this._findInLine(term, y, regex);
if (result) { if (result) {
break; break;
} }
@@ -93,7 +95,7 @@ export class SearchHelper implements ISearchHelper {
// Search from the top to the current ydisp // Search from the top to the current ydisp
if (!result) { if (!result) {
for (let y = this._terminal._core.buffer.ybase + this._terminal.rows - 1; y > startRow; y--) { 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) { if (result) {
break; break;
} }
@@ -108,12 +110,22 @@ export class SearchHelper implements ISearchHelper {
* Searches a line for a search term. * Searches a line for a search term.
* @param term Tne search term. * @param term Tne search term.
* @param y The line to search. * @param y The line to search.
* @param regex Should use regular expressions
* @return The search result if it was found. * @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 lowerStringLine = this._terminal._core.buffer.translateBufferLineToString(y, true).toLowerCase();
const lowerTerm = term.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) { if (searchIndex >= 0) {
const line = this._terminal._core.buffer.lines.get(y); const line = this._terminal._core.buffer.lines.get(y);
for (let i = 0; i < searchIndex; i++) { 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 * Find the next instance of the term, then scroll to and select it. If it
* doesn't exist, do nothing. * doesn't exist, do nothing.
* @param term Tne search term. * @param term Tne search term.
* @param regex Should use regular expressions
* @return Whether a result was found. * @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; const addonTerminal = <ISearchAddonTerminal>terminal;
if (!addonTerminal.__searchHelper) { if (!addonTerminal.__searchHelper) {
addonTerminal.__searchHelper = new SearchHelper(addonTerminal); 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 * Find the previous instance of the term, then scroll to and select it. If it
* doesn't exist, do nothing. * doesn't exist, do nothing.
* @param term Tne search term. * @param term Tne search term.
* @param regex Should use regular expressions
* @return Whether a result was found. * @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; const addonTerminal = <ISearchAddonTerminal>terminal;
if (!addonTerminal.__searchHelper) { if (!addonTerminal.__searchHelper) {
addonTerminal.__searchHelper = new SearchHelper(addonTerminal); addonTerminal.__searchHelper = new SearchHelper(addonTerminal);
} }
return addonTerminal.__searchHelper.findPrevious(term); return addonTerminal.__searchHelper.findPrevious(term, regex);
} }
export function apply(terminalConstructor: typeof Terminal): void { export function apply(terminalConstructor: typeof Terminal): void {
(<any>terminalConstructor.prototype).findNext = function(term: string): boolean { (<any>terminalConstructor.prototype).findNext = function(term: string, regex: boolean): boolean {
return findNext(this, term); return findNext(this, term, regex);
}; };
(<any>terminalConstructor.prototype).findPrevious = function(term: string): boolean { (<any>terminalConstructor.prototype).findPrevious = function(term: string, regex: boolean): boolean {
return findPrevious(this, term); return findPrevious(this, term, regex);
}; };
} }