Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added optional case sensitive searching #1663

Merged
merged 5 commits into from Sep 12, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions demo/client.ts
Expand Up @@ -103,7 +103,7 @@ function createTerminal(): void {
const searchOptions = {
regex: (document.getElementById('regex') as HTMLInputElement).checked,
wholeWord: false,
caseSensitive: false
caseSensitive: (document.getElementById('case-sensitive') as HTMLInputElement).checked
};
term.findNext(actionElements.findNext.value, searchOptions);
}
Expand All @@ -114,7 +114,7 @@ function createTerminal(): void {
const searchOptions = {
regex: (document.getElementById('regex') as HTMLInputElement).checked,
wholeWord: false,
caseSensitive: false
caseSensitive: (document.getElementById('case-sensitive') as HTMLInputElement).checked
};
term.findPrevious(actionElements.findPrevious.value, searchOptions);
}
Expand Down
1 change: 1 addition & 0 deletions demo/index.html
Expand Up @@ -17,6 +17,7 @@ <h3>Actions</h3>
<label>Find next <input id="find-next"/></label>
<label>Find previous <input id="find-previous"/></label>
<label>Use regex<input type="checkbox" id="regex"/></label>
<label>Case sensitive<input type="checkbox" id="case-sensitive"/></label>
</p>
</div>
<div>
Expand Down
13 changes: 8 additions & 5 deletions src/addons/search/SearchHelper.ts
Expand Up @@ -113,18 +113,21 @@ export class SearchHelper implements ISearchHelper {
if (this._terminal._core.buffer.lines.get(y).isWrapped) {
return;
}
const lowerStringLine = this.translateBufferLineToStringWithWrap(y, true).toLowerCase();
const lowerTerm = term.toLowerCase();

const stringLine = this.translateBufferLineToStringWithWrap(y, true);
const searchStringLine = searchOptions.caseSensitive ? stringLine : stringLine.toLowerCase();
const searchTerm = searchOptions.caseSensitive ? term : term.toLowerCase();
let searchIndex = -1;

if (searchOptions.regex) {
const searchRegex = RegExp(lowerTerm, 'g');
const foundTerm = searchRegex.exec(lowerStringLine);
const searchRegex = RegExp(searchTerm, 'g');
const foundTerm = searchRegex.exec(searchStringLine);
if (foundTerm && foundTerm[0].length > 0) {
searchIndex = searchRegex.lastIndex - foundTerm[0].length;
term = foundTerm[0];
}
} else {
searchIndex = lowerStringLine.indexOf(lowerTerm);
searchIndex = searchStringLine.indexOf(searchTerm);
}

if (searchIndex >= 0) {
Expand Down
17 changes: 17 additions & 0 deletions src/addons/search/search.test.ts
Expand Up @@ -117,5 +117,22 @@ describe('search addon', () => {
const line = term.searchHelper.findInLine('^.*$', 0, { regex: true });
expect(line).eql(undefined);
});
it('should respect case sensitive', function(): void {
alexr00 marked this conversation as resolved.
Show resolved Hide resolved
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 searchOptions = {
regex: false,
wholeWord: false,
caseSensitive: true
};
const hello0 = (term.searchHelper as any)._findInLine('Hello', 0, searchOptions);
const hello1 = (term.searchHelper as any)._findInLine('Hello', 1, searchOptions);
const hello2 = (term.searchHelper as any)._findInLine('Hello', 2, searchOptions);
expect(hello0).eql({col: 0, row: 0, term: 'Hello'});
expect(hello1).eql(undefined);
expect(hello2).eql({col: 8, row: 2, term: 'Hello'});
});
});
});