diff --git a/src/addons/search/Interfaces.ts b/src/addons/search/Interfaces.ts index 4fa8b77e..af06c5d1 100644 --- a/src/addons/search/Interfaces.ts +++ b/src/addons/search/Interfaces.ts @@ -22,7 +22,13 @@ export interface ISearchHelper { } export interface ISearchOptions { - regex: boolean; - wholeWord: boolean; - caseSensitive: boolean; + regex?: boolean; + wholeWord?: boolean; + caseSensitive?: boolean; +} + +export interface ISearchResult { + term: string; + col: number; + row: number; } diff --git a/src/addons/search/SearchHelper.ts b/src/addons/search/SearchHelper.ts index be1a1475..63fd047b 100644 --- a/src/addons/search/SearchHelper.ts +++ b/src/addons/search/SearchHelper.ts @@ -3,13 +3,7 @@ * @license MIT */ -import { ISearchHelper, ISearchAddonTerminal, ISearchOptions } from './Interfaces'; - -interface ISearchResult { - term: string; - col: number; - row: number; -} +import { ISearchHelper, ISearchAddonTerminal, ISearchOptions, ISearchResult } from './Interfaces'; /** * A class that knows how to search the terminal and how to display the results. @@ -28,7 +22,7 @@ export class SearchHelper implements ISearchHelper { * @param searchOptions Search options. * @return Whether a result was found. */ - public findNext(term: string, searchOptions: ISearchOptions): boolean { + public findNext(term: string, searchOptions?: ISearchOptions): boolean { if (!term || term.length === 0) { return false; } @@ -70,7 +64,7 @@ export class SearchHelper implements ISearchHelper { * @param searchOptions Search options. * @return Whether a result was found. */ - public findPrevious(term: string, searchOptions: ISearchOptions): boolean { + public findPrevious(term: string, searchOptions?: ISearchOptions): boolean { if (!term || term.length === 0) { return false; } @@ -112,7 +106,7 @@ export class SearchHelper implements ISearchHelper { * @param searchOptions Search options. * @return The search result if it was found. */ - private _findInLine(term: string, y: number, searchOptions: ISearchOptions = {regex: false, wholeWord: false, caseSensitive: false}): ISearchResult { + protected _findInLine(term: string, y: number, searchOptions: ISearchOptions = {}): ISearchResult { const lowerStringLine = this._terminal._core.buffer.translateBufferLineToString(y, true).toLowerCase(); const lowerTerm = term.toLowerCase(); let searchIndex = -1; diff --git a/src/addons/search/search.test.ts b/src/addons/search/search.test.ts index 9616ae3c..8fcde950 100644 --- a/src/addons/search/search.test.ts +++ b/src/addons/search/search.test.ts @@ -6,17 +6,17 @@ import { assert, expect } from 'chai'; import * as search from './search'; import { SearchHelper } from './SearchHelper'; -import { ISearchHelper } from './Interfaces'; +import { ISearchOptions, ISearchResult } from './Interfaces'; class MockTerminalPlain {} class MockTerminal { private _core: any; - public searchHelper: ISearchHelper; + public searchHelper: TestSearchHelper; constructor(options: any) { this._core = new (require('../../../lib/Terminal').Terminal)(options); - this.searchHelper = new SearchHelper(this as any); + this.searchHelper = new TestSearchHelper(this as any); } get core(): any { return this._core; @@ -26,6 +26,12 @@ class MockTerminal { } } +class TestSearchHelper extends SearchHelper { + public findInLine(term: string, y: number, searchOptions?: ISearchOptions): ISearchResult { + return this._findInLine(term, y, searchOptions); + } +} + describe('search addon', function(): void { describe('apply', () => { it('should register findNext and findPrevious', () => { @@ -39,9 +45,9 @@ describe('search addon', function(): void { const term = new MockTerminal({cols: 20, rows: 3}); term.core.write('Hello World\r\ntest\n123....hello'); term.pushWriteData(); - const hello0 = (term.searchHelper as any)._findInLine('Hello', 0); - const hello1 = (term.searchHelper as any)._findInLine('Hello', 1); - const hello2 = (term.searchHelper as any)._findInLine('Hello', 2); + const hello0 = term.searchHelper.findInLine('Hello', 0); + const hello1 = term.searchHelper.findInLine('Hello', 1); + const hello2 = term.searchHelper.findInLine('Hello', 2); expect(hello0).eql({col: 0, row: 0, term: 'Hello'}); expect(hello1).eql(undefined); expect(hello2).eql({col: 11, row: 2, term: 'Hello'}); @@ -62,12 +68,12 @@ describe('search addon', function(): void { wholeWord: false, caseSensitive: false }; - const hello0 = (term.searchHelper as any)._findInLine('dee*', 0, searchOptions); - const hello1 = (term.searchHelper as any)._findInLine('jkk*', 0, searchOptions); - const hello2 = (term.searchHelper as any)._findInLine('mnn*', 1, searchOptions); - const tilda0 = (term.searchHelper as any)._findInLine('^~', 3, searchOptions); - const tilda1 = (term.searchHelper as any)._findInLine('^[~]', 3, searchOptions); - const tilda2 = (term.searchHelper as any)._findInLine('^\\~', 3, searchOptions); + const hello0 = term.searchHelper.findInLine('dee*', 0, searchOptions); + term.searchHelper.findInLine('jkk*', 0, searchOptions); + term.searchHelper.findInLine('mnn*', 1, searchOptions); + const tilda0 = term.searchHelper.findInLine('^~', 3, searchOptions); + const tilda1 = term.searchHelper.findInLine('^[~]', 3, searchOptions); + const tilda2 = term.searchHelper.findInLine('^\\~', 3, searchOptions); expect(hello0).eql({col: 3, row: 0, term: 'de'}); // TODO: uncomment this test when line wrap search is checked in expect(hello1).eql({col: 9, row: 0, term: 'jk'}); // TODO: uncomment this test when line wrap search is checked in expect(hello2).eql(undefined); diff --git a/src/addons/search/search.ts b/src/addons/search/search.ts index 93ac8843..9be0b33b 100644 --- a/src/addons/search/search.ts +++ b/src/addons/search/search.ts @@ -14,7 +14,7 @@ import { ISearchAddonTerminal, ISearchOptions } from './Interfaces'; * @param searchOptions Search options * @return Whether a result was found. */ -export function findNext(terminal: Terminal, term: string, searchOptions: ISearchOptions = {regex: false, wholeWord: false, caseSensitive: false}): boolean { +export function findNext(terminal: Terminal, term: string, searchOptions: ISearchOptions = {}): boolean { const addonTerminal = terminal; if (!addonTerminal.__searchHelper) { addonTerminal.__searchHelper = new SearchHelper(addonTerminal); @@ -29,7 +29,7 @@ export function findNext(terminal: Terminal, term: string, searchOptions: ISearc * @param searchOptions Search options * @return Whether a result was found. */ -export function findPrevious(terminal: Terminal, term: string, searchOptions: ISearchOptions = {regex: false, wholeWord: false, caseSensitive: false}): boolean { +export function findPrevious(terminal: Terminal, term: string, searchOptions: ISearchOptions): boolean { const addonTerminal = terminal; if (!addonTerminal.__searchHelper) { addonTerminal.__searchHelper = new SearchHelper(addonTerminal);