Skip to content

Commit

Permalink
Responded to feedback and fixed the returned found term
Browse files Browse the repository at this point in the history
  • Loading branch information
alexr00 committed Sep 7, 2018
1 parent 03e54cc commit e546584
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 36 deletions.
14 changes: 12 additions & 2 deletions demo/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,23 @@ function createTerminal() {
addDomListener(actionElements.findNext, 'keypress', function (e) {
if (e.key === "Enter") {
e.preventDefault();
term.findNext(actionElements.findNext.value, document.getElementById('regex').checked);
let searchOptions = {
regex: document.getElementById('regex').checked,
wholeWord: false,
caseSensitive: false
};
term.findNext(actionElements.findNext.value, searchOptions);
}
});
addDomListener(actionElements.findPrevious, 'keypress', function (e) {
if (e.key === "Enter") {
e.preventDefault();
term.findPrevious(actionElements.findPrevious.value, document.getElementById('regex').checked);
let searchOptions = {
regex: document.getElementById('regex').checked,
wholeWord: false,
caseSensitive: false
};
term.findPrevious(actionElements.findPrevious.value, searchOptions);
}
});

Expand Down
10 changes: 8 additions & 2 deletions src/addons/search/Interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ export interface ISearchAddonTerminal extends Terminal {
}

export interface ISearchHelper {
findNext(term: string, regex: boolean): boolean;
findPrevious(term: string, regex: boolean): boolean;
findNext(term: string, searchOptions: ISearchOptions): boolean;
findPrevious(term: string, searchOptions: ISearchOptions): boolean;
}

export interface ISearchOptions {
regex: boolean;
wholeWord: boolean;
caseSensitive: boolean;
}
28 changes: 14 additions & 14 deletions src/addons/search/SearchHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @license MIT
*/

import { ISearchHelper, ISearchAddonTerminal } from './Interfaces';
import { ISearchHelper, ISearchAddonTerminal, ISearchOptions } from './Interfaces';

interface ISearchResult {
term: string;
Expand All @@ -19,17 +19,16 @@ export class SearchHelper implements ISearchHelper {
// TODO: Search for multiple instances on 1 line
// TODO: Don't use the actual selection, instead use a "find selection" so multiple instances can be highlighted
// TODO: Highlight other instances in the viewport
// TODO: Support regex, case sensitivity, etc.
}

/**
* 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
* @param searchOptions Search options.
* @return Whether a result was found.
*/
public findNext(term: string, regex: boolean = false): boolean {
public findNext(term: string, searchOptions: ISearchOptions): boolean {
if (!term || term.length === 0) {
return false;
}
Expand All @@ -44,7 +43,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, regex);
result = this._findInLine(term, y, searchOptions);
if (result) {
break;
}
Expand All @@ -53,7 +52,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, regex);
result = this._findInLine(term, y, searchOptions);
if (result) {
break;
}
Expand All @@ -68,10 +67,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
* @param searchOptions Search options.
* @return Whether a result was found.
*/
public findPrevious(term: string, regex: boolean = false): boolean {
public findPrevious(term: string, searchOptions: ISearchOptions): boolean {
if (!term || term.length === 0) {
return false;
}
Expand All @@ -86,7 +85,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, regex);
result = this._findInLine(term, y, searchOptions);
if (result) {
break;
}
Expand All @@ -95,7 +94,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, regex);
result = this._findInLine(term, y, searchOptions);
if (result) {
break;
}
Expand All @@ -108,20 +107,21 @@ export class SearchHelper implements ISearchHelper {

/**
* Searches a line for a search term.
* @param term Tne search term.
* @param term The search term.
* @param y The line to search.
* @param regex Should use regular expressions
* @param searchOptions Search options.
* @return The search result if it was found.
*/
private _findInLine(term: string, y: number, regex: boolean): ISearchResult {
private _findInLine(term: string, y: number, searchOptions: ISearchOptions = {regex: false, wholeWord: false, caseSensitive: false}): ISearchResult {
const lowerStringLine = this._terminal._core.buffer.translateBufferLineToString(y, true).toLowerCase();
const lowerTerm = term.toLowerCase();
let searchIndex = -1;
if (regex) {
if (searchOptions.regex) {
const searchRegex = RegExp(lowerTerm, 'g');
const foundTerm = searchRegex.exec(lowerStringLine);
if (foundTerm) {
searchIndex = searchRegex.lastIndex - foundTerm[0].length;
term = foundTerm[0];
}
} else {
searchIndex = lowerStringLine.indexOf(lowerTerm);
Expand Down
26 changes: 19 additions & 7 deletions src/addons/search/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,31 @@ describe('search addon', function(): void {
});
it('should respect search regex', function(): void {
search.apply(<any>MockTerminal);
const term = new MockTerminal({cols: 10, rows: 3});
term.core.write('abcdefghijklmnopqrstuvwxyz');
const term = new MockTerminal({cols: 10, rows: 4});
term.core.write('abcdefghijklmnopqrstuvwxyz\r\n~/dev ');
/*
abcdefghij
klmnopqrst
uvwxyz
~/dev
*/
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*'});
const searchOptions = {
regex: true,
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);
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);
expect(tilda0).eql({col: 0, row: 3, term: '~'});
expect(tilda1).eql({col: 0, row: 3, term: '~'});
expect(tilda2).eql({col: 0, row: 3, term: '~'});
});
});
22 changes: 11 additions & 11 deletions src/addons/search/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,44 @@

import { SearchHelper } from './SearchHelper';
import { Terminal } from 'xterm';
import { ISearchAddonTerminal } from './Interfaces';
import { ISearchAddonTerminal, ISearchOptions } 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
* @param searchOptions Search options
* @return Whether a result was found.
*/
export function findNext(terminal: Terminal, term: string, regex: boolean): boolean {
export function findNext(terminal: Terminal, term: string, searchOptions: ISearchOptions = {regex: false, wholeWord: false, caseSensitive: false}): boolean {
const addonTerminal = <ISearchAddonTerminal>terminal;
if (!addonTerminal.__searchHelper) {
addonTerminal.__searchHelper = new SearchHelper(addonTerminal);
}
return addonTerminal.__searchHelper.findNext(term, regex);
return addonTerminal.__searchHelper.findNext(term, searchOptions);
}

/**
* 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
* @param searchOptions Search options
* @return Whether a result was found.
*/
export function findPrevious(terminal: Terminal, term: string, regex: boolean): boolean {
export function findPrevious(terminal: Terminal, term: string, searchOptions: ISearchOptions = {regex: false, wholeWord: false, caseSensitive: false}): boolean {
const addonTerminal = <ISearchAddonTerminal>terminal;
if (!addonTerminal.__searchHelper) {
addonTerminal.__searchHelper = new SearchHelper(addonTerminal);
}
return addonTerminal.__searchHelper.findPrevious(term, regex);
return addonTerminal.__searchHelper.findPrevious(term, searchOptions);
}

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

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

0 comments on commit e546584

Please sign in to comment.