Fixes #1660: Search as you type
This commit is contained in:
+18
-20
@@ -14,6 +14,7 @@ import * as fullscreen from '../lib/addons/fullscreen/fullscreen';
|
|||||||
import * as search from '../lib/addons/search/search';
|
import * as search from '../lib/addons/search/search';
|
||||||
import * as webLinks from '../lib/addons/webLinks/webLinks';
|
import * as webLinks from '../lib/addons/webLinks/webLinks';
|
||||||
import * as winptyCompat from '../lib/addons/winptyCompat/winptyCompat';
|
import * as winptyCompat from '../lib/addons/winptyCompat/winptyCompat';
|
||||||
|
import { ISearchOptions } from '../lib/addons/search/Interfaces';
|
||||||
|
|
||||||
// Pulling in the module's types relies on the <reference> above, it's looks a
|
// Pulling in the module's types relies on the <reference> above, it's looks a
|
||||||
// little weird here as we're importing "this" module
|
// little weird here as we're importing "this" module
|
||||||
@@ -50,6 +51,14 @@ function setPadding(): void {
|
|||||||
term.fit();
|
term.fit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getSearchOptions(): ISearchOptions {
|
||||||
|
return {
|
||||||
|
regex: (document.getElementById('regex') as HTMLInputElement).checked,
|
||||||
|
wholeWord: (document.getElementById('whole-word') as HTMLInputElement).checked,
|
||||||
|
caseSensitive: (document.getElementById('case-sensitive') as HTMLInputElement).checked,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
createTerminal();
|
createTerminal();
|
||||||
|
|
||||||
const disposeRecreateButtonHandler = () => {
|
const disposeRecreateButtonHandler = () => {
|
||||||
@@ -97,27 +106,16 @@ function createTerminal(): void {
|
|||||||
|
|
||||||
addDomListener(paddingElement, 'change', setPadding);
|
addDomListener(paddingElement, 'change', setPadding);
|
||||||
|
|
||||||
addDomListener(actionElements.findNext, 'keypress', (e) => {
|
addDomListener(actionElements.findNext, 'keyup', (e) => {
|
||||||
if (e.key === 'Enter') {
|
const searchOptions = getSearchOptions();
|
||||||
e.preventDefault();
|
searchOptions.incremental = e.key !== `Enter`;
|
||||||
const searchOptions = {
|
term.findNext(actionElements.findNext.value, searchOptions);
|
||||||
regex: (document.getElementById('regex') as HTMLInputElement).checked,
|
|
||||||
wholeWord: (document.getElementById('whole-word') as HTMLInputElement).checked,
|
|
||||||
caseSensitive: (document.getElementById('case-sensitive') as HTMLInputElement).checked
|
|
||||||
};
|
|
||||||
term.findNext(actionElements.findNext.value, searchOptions);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
addDomListener(actionElements.findPrevious, 'keypress', (e) => {
|
|
||||||
if (e.key === 'Enter') {
|
addDomListener(actionElements.findPrevious, 'keyup', (e) => {
|
||||||
e.preventDefault();
|
const searchOptions = getSearchOptions();
|
||||||
const searchOptions = {
|
searchOptions.incremental = e.key !== `Enter`;
|
||||||
regex: (document.getElementById('regex') as HTMLInputElement).checked,
|
term.findPrevious(actionElements.findPrevious.value, searchOptions);
|
||||||
wholeWord: (document.getElementById('whole-word') as HTMLInputElement).checked,
|
|
||||||
caseSensitive: (document.getElementById('case-sensitive') as HTMLInputElement).checked
|
|
||||||
};
|
|
||||||
term.findPrevious(actionElements.findPrevious.value, searchOptions);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// fit is called within a setTimeout, cols and rows need this.
|
// fit is called within a setTimeout, cols and rows need this.
|
||||||
|
|||||||
@@ -25,6 +25,8 @@ export interface ISearchOptions {
|
|||||||
regex?: boolean;
|
regex?: boolean;
|
||||||
wholeWord?: boolean;
|
wholeWord?: boolean;
|
||||||
caseSensitive?: boolean;
|
caseSensitive?: boolean;
|
||||||
|
/** Assume caller implements 'search as you type' where findNext gets called when search input changes */
|
||||||
|
incremental?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ISearchResult {
|
export interface ISearchResult {
|
||||||
|
|||||||
@@ -24,29 +24,34 @@ export class SearchHelper implements ISearchHelper {
|
|||||||
* @return Whether a result was found.
|
* @return Whether a result was found.
|
||||||
*/
|
*/
|
||||||
public findNext(term: string, searchOptions?: ISearchOptions): boolean {
|
public findNext(term: string, searchOptions?: ISearchOptions): boolean {
|
||||||
|
const selectionManager = this._terminal._core.selectionManager;
|
||||||
|
const {incremental} = searchOptions;
|
||||||
|
let result: ISearchResult;
|
||||||
|
|
||||||
if (!term || term.length === 0) {
|
if (!term || term.length === 0) {
|
||||||
|
selectionManager.clearSelection();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
let result: ISearchResult;
|
|
||||||
|
|
||||||
let startRow = this._terminal._core.buffer.ydisp;
|
let startRow = this._terminal._core.buffer.ydisp;
|
||||||
if (this._terminal._core.selectionManager.selectionEnd) {
|
|
||||||
|
if (selectionManager.selectionEnd) {
|
||||||
// Start from the selection end if there is a selection
|
// Start from the selection end if there is a selection
|
||||||
|
// For incremental search, use existing row
|
||||||
if (this._terminal.getSelection().length !== 0) {
|
if (this._terminal.getSelection().length !== 0) {
|
||||||
startRow = this._terminal._core.selectionManager.selectionEnd[1];
|
startRow = incremental ? selectionManager.selectionStart[1] : selectionManager.selectionEnd[1];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Search from ydisp + 1 to end
|
// Search from startRow to end
|
||||||
for (let y = startRow + 1; y < this._terminal._core.buffer.ybase + this._terminal.rows; y++) {
|
for (let y = incremental ? startRow: startRow + 1; y < this._terminal._core.buffer.ybase + this._terminal.rows; y++) {
|
||||||
result = this._findInLine(term, y, searchOptions);
|
result = this._findInLine(term, y, searchOptions);
|
||||||
if (result) {
|
if (result) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Search from the top to the current ydisp
|
// Search from the top to the startRow
|
||||||
if (!result) {
|
if (!result) {
|
||||||
for (let y = 0; y < startRow; y++) {
|
for (let y = 0; y < startRow; y++) {
|
||||||
result = this._findInLine(term, y, searchOptions);
|
result = this._findInLine(term, y, searchOptions);
|
||||||
@@ -68,29 +73,33 @@ export class SearchHelper implements ISearchHelper {
|
|||||||
* @return Whether a result was found.
|
* @return Whether a result was found.
|
||||||
*/
|
*/
|
||||||
public findPrevious(term: string, searchOptions?: ISearchOptions): boolean {
|
public findPrevious(term: string, searchOptions?: ISearchOptions): boolean {
|
||||||
|
const selectionManager = this._terminal._core.selectionManager;
|
||||||
|
const {incremental} = searchOptions;
|
||||||
|
let result: ISearchResult;
|
||||||
|
|
||||||
if (!term || term.length === 0) {
|
if (!term || term.length === 0) {
|
||||||
|
selectionManager.clearSelection();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
let result: ISearchResult;
|
|
||||||
|
|
||||||
let startRow = this._terminal._core.buffer.ydisp;
|
let startRow = this._terminal._core.buffer.ydisp;
|
||||||
if (this._terminal._core.selectionManager.selectionStart) {
|
|
||||||
// Start from the selection end if there is a selection
|
if (selectionManager.selectionStart) {
|
||||||
|
// Start from the selection start if there is a selection
|
||||||
if (this._terminal.getSelection().length !== 0) {
|
if (this._terminal.getSelection().length !== 0) {
|
||||||
startRow = this._terminal._core.selectionManager.selectionStart[1];
|
startRow = selectionManager.selectionStart[1];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Search from ydisp + 1 to end
|
// Search from startRow to top
|
||||||
for (let y = startRow - 1; y >= 0; y--) {
|
for (let y = incremental ? startRow : startRow - 1; y >= 0; y--) {
|
||||||
result = this._findInLine(term, y, searchOptions);
|
result = this._findInLine(term, y, searchOptions);
|
||||||
if (result) {
|
if (result) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Search from the top to the current ydisp
|
// Search from the bottom to startRow
|
||||||
if (!result) {
|
if (!result) {
|
||||||
for (let y = this._terminal._core.buffer.ybase + this._terminal.rows - 1; y > startRow; y--) {
|
for (let y = this._terminal._core.buffer.ybase + this._terminal.rows - 1; y > startRow; y--) {
|
||||||
result = this._findInLine(term, y, searchOptions);
|
result = this._findInLine(term, y, searchOptions);
|
||||||
|
|||||||
Reference in New Issue
Block a user