diff --git a/demo/index.html b/demo/index.html index fff77d09..72804f46 100644 --- a/demo/index.html +++ b/demo/index.html @@ -16,6 +16,7 @@

+

diff --git a/demo/main.js b/demo/main.js index 70544e38..79116efb 100644 --- a/demo/main.js +++ b/demo/main.js @@ -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); } }); diff --git a/src/addons/search/Interfaces.ts b/src/addons/search/Interfaces.ts index 926e3f36..e0ed1173 100644 --- a/src/addons/search/Interfaces.ts +++ b/src/addons/search/Interfaces.ts @@ -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; } diff --git a/src/addons/search/SearchHelper.ts b/src/addons/search/SearchHelper.ts index 9b5c06f3..0a22a57e 100644 --- a/src/addons/search/SearchHelper.ts +++ b/src/addons/search/SearchHelper.ts @@ -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++) { diff --git a/src/addons/search/search.ts b/src/addons/search/search.ts index a1dd766f..4e97dd90 100644 --- a/src/addons/search/search.ts +++ b/src/addons/search/search.ts @@ -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 = 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 = 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 { - (terminalConstructor.prototype).findNext = function(term: string): boolean { - return findNext(this, term); + (terminalConstructor.prototype).findNext = function(term: string, regex: boolean): boolean { + return findNext(this, term, regex); }; - (terminalConstructor.prototype).findPrevious = function(term: string): boolean { - return findPrevious(this, term); + (terminalConstructor.prototype).findPrevious = function(term: string, regex: boolean): boolean { + return findPrevious(this, term, regex); }; }