mirror of
https://github.com/wavetermdev/xterm.js.git
synced 2026-08-05 13:43:48 -07:00
Merge pull request #1666 from alexr00/alexr00/searchRegex
Added support for searching by regular expression
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
<p>
|
||||
<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>
|
||||
</p>
|
||||
</div>
|
||||
<div>
|
||||
|
||||
+12
-2
@@ -83,13 +83,23 @@ function createTerminal() {
|
||||
addDomListener(actionElements.findNext, 'keypress', function (e) {
|
||||
if (e.key === "Enter") {
|
||||
e.preventDefault();
|
||||
term.findNext(actionElements.findNext.value);
|
||||
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);
|
||||
let searchOptions = {
|
||||
regex: document.getElementById('regex').checked,
|
||||
wholeWord: false,
|
||||
caseSensitive: false
|
||||
};
|
||||
term.findPrevious(actionElements.findPrevious.value, searchOptions);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -17,6 +17,18 @@ export interface ISearchAddonTerminal extends Terminal {
|
||||
}
|
||||
|
||||
export interface ISearchHelper {
|
||||
findNext(term: string): boolean;
|
||||
findPrevious(term: string): boolean;
|
||||
findNext(term: string, searchOptions: ISearchOptions): boolean;
|
||||
findPrevious(term: string, searchOptions: ISearchOptions): boolean;
|
||||
}
|
||||
|
||||
export interface ISearchOptions {
|
||||
regex?: boolean;
|
||||
wholeWord?: boolean;
|
||||
caseSensitive?: boolean;
|
||||
}
|
||||
|
||||
export interface ISearchResult {
|
||||
term: string;
|
||||
col: number;
|
||||
row: number;
|
||||
}
|
||||
|
||||
@@ -3,13 +3,7 @@
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
import { ISearchHelper, ISearchAddonTerminal } 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.
|
||||
@@ -19,16 +13,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 searchOptions Search options.
|
||||
* @return Whether a result was found.
|
||||
*/
|
||||
public findNext(term: string): boolean {
|
||||
public findNext(term: string, searchOptions?: ISearchOptions): boolean {
|
||||
if (!term || term.length === 0) {
|
||||
return false;
|
||||
}
|
||||
@@ -43,7 +37,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, searchOptions);
|
||||
if (result) {
|
||||
break;
|
||||
}
|
||||
@@ -52,7 +46,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, searchOptions);
|
||||
if (result) {
|
||||
break;
|
||||
}
|
||||
@@ -67,9 +61,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 searchOptions Search options.
|
||||
* @return Whether a result was found.
|
||||
*/
|
||||
public findPrevious(term: string): boolean {
|
||||
public findPrevious(term: string, searchOptions?: ISearchOptions): boolean {
|
||||
if (!term || term.length === 0) {
|
||||
return false;
|
||||
}
|
||||
@@ -84,7 +79,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, searchOptions);
|
||||
if (result) {
|
||||
break;
|
||||
}
|
||||
@@ -93,7 +88,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, searchOptions);
|
||||
if (result) {
|
||||
break;
|
||||
}
|
||||
@@ -106,14 +101,25 @@ 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 searchOptions Search options.
|
||||
* @return The search result if it was found.
|
||||
*/
|
||||
private _findInLine(term: string, y: number): 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 = lowerStringLine.indexOf(lowerTerm);
|
||||
let searchIndex = -1;
|
||||
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);
|
||||
}
|
||||
if (searchIndex >= 0) {
|
||||
const line = this._terminal._core.buffer.lines.get(y);
|
||||
for (let i = 0; i < searchIndex; i++) {
|
||||
|
||||
@@ -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,11 +45,40 @@ 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'});
|
||||
});
|
||||
it('should respect search regex', function(): void {
|
||||
search.apply(<any>MockTerminal);
|
||||
const term = new MockTerminal({cols: 10, rows: 4});
|
||||
term.core.write('abcdefghijklmnopqrstuvwxyz\r\n~/dev ');
|
||||
/*
|
||||
abcdefghij
|
||||
klmnopqrst
|
||||
uvwxyz
|
||||
~/dev
|
||||
*/
|
||||
term.pushWriteData();
|
||||
const searchOptions = {
|
||||
regex: true,
|
||||
wholeWord: false,
|
||||
caseSensitive: false
|
||||
};
|
||||
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);
|
||||
expect(tilda0).eql({col: 0, row: 3, term: '~'});
|
||||
expect(tilda1).eql({col: 0, row: 3, term: '~'});
|
||||
expect(tilda2).eql({col: 0, row: 3, term: '~'});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -5,42 +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 searchOptions Search options
|
||||
* @return Whether a result was found.
|
||||
*/
|
||||
export function findNext(terminal: Terminal, term: string): boolean {
|
||||
export function findNext(terminal: Terminal, term: string, searchOptions: ISearchOptions = {}): boolean {
|
||||
const addonTerminal = <ISearchAddonTerminal>terminal;
|
||||
if (!addonTerminal.__searchHelper) {
|
||||
addonTerminal.__searchHelper = new SearchHelper(addonTerminal);
|
||||
}
|
||||
return addonTerminal.__searchHelper.findNext(term);
|
||||
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 searchOptions Search options
|
||||
* @return Whether a result was found.
|
||||
*/
|
||||
export function findPrevious(terminal: Terminal, term: string): boolean {
|
||||
export function findPrevious(terminal: Terminal, term: string, searchOptions: ISearchOptions): boolean {
|
||||
const addonTerminal = <ISearchAddonTerminal>terminal;
|
||||
if (!addonTerminal.__searchHelper) {
|
||||
addonTerminal.__searchHelper = new SearchHelper(addonTerminal);
|
||||
}
|
||||
return addonTerminal.__searchHelper.findPrevious(term);
|
||||
return addonTerminal.__searchHelper.findPrevious(term, searchOptions);
|
||||
}
|
||||
|
||||
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, searchOptions: ISearchOptions): boolean {
|
||||
return findNext(this, term, searchOptions);
|
||||
};
|
||||
|
||||
(<any>terminalConstructor.prototype).findPrevious = function(term: string): boolean {
|
||||
return findPrevious(this, term);
|
||||
(<any>terminalConstructor.prototype).findPrevious = function(term: string, searchOptions: ISearchOptions): boolean {
|
||||
return findPrevious(this, term, searchOptions);
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user