Skip to content

Commit

Permalink
Added optional case sensitive searching
Browse files Browse the repository at this point in the history
  • Loading branch information
alexr00 committed Sep 6, 2018
1 parent 3a310f1 commit 2a6aa25
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 20 deletions.
1 change: 1 addition & 0 deletions demo/index.html
Expand Up @@ -16,6 +16,7 @@ <h3>Actions</h3>
<p>
<label>Find next <input id="find-next"/></label>
<label>Find previous <input id="find-previous"/></label>
<label>Case sensitive<input type="checkbox" id="case-sensitive"/></label>
</p>
</div>
<div>
Expand Down
4 changes: 2 additions & 2 deletions demo/main.js
Expand Up @@ -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('case-sensitive').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('case-sensitive').checked);
}
});

Expand Down
21 changes: 11 additions & 10 deletions src/addons/search/SearchHelper.ts
Expand Up @@ -28,7 +28,7 @@ export class SearchHelper implements ISearchHelper {
* @param term Tne search term.
* @return Whether a result was found.
*/
public findNext(term: string): boolean {
public findNext(term: string, caseSensitive: boolean = false): boolean {
if (!term || term.length === 0) {
return false;
}
Expand All @@ -43,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);
result = this._findInLine(term, y, caseSensitive);
if (result) {
break;
}
Expand All @@ -52,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);
result = this._findInLine(term, y, caseSensitive);
if (result) {
break;
}
Expand All @@ -69,7 +69,7 @@ export class SearchHelper implements ISearchHelper {
* @param term Tne search term.
* @return Whether a result was found.
*/
public findPrevious(term: string): boolean {
public findPrevious(term: string, caseSensitive: boolean = false): boolean {
if (!term || term.length === 0) {
return false;
}
Expand All @@ -84,7 +84,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, caseSensitive);
if (result) {
break;
}
Expand All @@ -93,7 +93,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, caseSensitive);
if (result) {
break;
}
Expand All @@ -110,10 +110,11 @@ export class SearchHelper implements ISearchHelper {
* @param y The line to search.
* @return The search result if it was found.
*/
private _findInLine(term: string, y: number): ISearchResult {
const lowerStringLine = this._terminal._core.buffer.translateBufferLineToString(y, true).toLowerCase();
const lowerTerm = term.toLowerCase();
let searchIndex = lowerStringLine.indexOf(lowerTerm);
private _findInLine(term: string, y: number, caseSensitive: boolean): ISearchResult {
const stringLIne = this._terminal._core.buffer.translateBufferLineToString(y, true);
const searchStringLine = caseSensitive ? stringLIne : stringLIne.toLowerCase();
const searchTerm = caseSensitive ? term : term.toLowerCase();
let searchIndex = searchStringLine.indexOf(searchTerm);
if (searchIndex >= 0) {
const line = this._terminal._core.buffer.lines.get(y);
for (let i = 0; i < searchIndex; i++) {
Expand Down
12 changes: 12 additions & 0 deletions src/addons/search/search.test.ts
Expand Up @@ -46,4 +46,16 @@ describe('search addon', function(): void {
expect(hello1).eql(undefined);
expect(hello2).eql({col: 11, row: 2, term: 'Hello'});
});
it('should respect case sensitive', function(): void {
search.apply(<any>MockTerminal);
const term = new MockTerminal({cols: 20, rows: 4});
term.core.write('Hello World\r\n123....hello\r\nmoreTestHello');
term.pushWriteData();
const hello0 = (term.searchHelper as any)._findInLine('Hello', 0, true);
const hello1 = (term.searchHelper as any)._findInLine('Hello', 1, true);
const hello2 = (term.searchHelper as any)._findInLine('Hello', 2, true);
expect(hello0).eql({col: 0, row: 0, term: 'Hello'});
expect(hello1).eql(undefined);
expect(hello2).eql({col: 8, row: 2, term: 'Hello'});
});
});
16 changes: 8 additions & 8 deletions src/addons/search/search.ts
Expand Up @@ -13,12 +13,12 @@ import { ISearchAddonTerminal } from './Interfaces';
* @param term Tne search term.
* @return Whether a result was found.
*/
export function findNext(terminal: Terminal, term: string): boolean {
export function findNext(terminal: Terminal, term: string, caseSensitive: boolean): boolean {
const addonTerminal = <ISearchAddonTerminal>terminal;
if (!addonTerminal.__searchHelper) {
addonTerminal.__searchHelper = new SearchHelper(addonTerminal);
}
return addonTerminal.__searchHelper.findNext(term);
return addonTerminal.__searchHelper.findNext(term, caseSensitive);
}

/**
Expand All @@ -27,20 +27,20 @@ export function findNext(terminal: Terminal, term: string): boolean {
* @param term Tne search term.
* @return Whether a result was found.
*/
export function findPrevious(terminal: Terminal, term: string): boolean {
export function findPrevious(terminal: Terminal, term: string, caseSensitive: boolean): boolean {
const addonTerminal = <ISearchAddonTerminal>terminal;
if (!addonTerminal.__searchHelper) {
addonTerminal.__searchHelper = new SearchHelper(addonTerminal);
}
return addonTerminal.__searchHelper.findPrevious(term);
return addonTerminal.__searchHelper.findPrevious(term, caseSensitive);
}

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, caseSensitive: boolean): boolean {
return findNext(this, term, caseSensitive);
};

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

0 comments on commit 2a6aa25

Please sign in to comment.