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

Allow configuring search overview ruler and border color #3708

Merged
merged 2 commits into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 31 additions & 15 deletions addons/xterm-addon-search/src/SearchAddon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ export interface ISearchOptions {
}

interface ISearchDecorationOptions {
matchColor: string;
selectedColor: string;
matchBackground?: string;
matchBorder?: string;
matchOverviewRuler: string;
activeMatchBackground?: string;
activeMatchBorder?: string;
activeMatchColorOverviewRuler: string;
}

export interface ISearchPosition {
Expand Down Expand Up @@ -601,11 +605,16 @@ export class SearchAddon implements ITerminalAddon {
return false;
}
terminal.select(result.col, result.row, result.size);
if (decorations?.selectedColor) {
if (decorations?.activeMatchColorOverviewRuler) {
const marker = terminal.registerMarker(-terminal.buffer.active.baseY - terminal.buffer.active.cursorY + result.row);
if (marker) {
this._selectedDecoration = terminal.registerDecoration({ marker, overviewRulerOptions: { color: decorations.selectedColor } });
this._selectedDecoration?.onRender((e) => this._applyStyles(e, decorations.selectedColor, result));
this._selectedDecoration = terminal.registerDecoration({
marker,
overviewRulerOptions: {
color: decorations.activeMatchColorOverviewRuler
}
});
this._selectedDecoration?.onRender((e) => this._applyStyles(e, decorations.activeMatchBackground, decorations.activeMatchBorder, result));
this._selectedDecoration?.onDispose(() => marker.dispose());
}
}
Expand All @@ -622,20 +631,25 @@ export class SearchAddon implements ITerminalAddon {
/**
* Applies styles to the decoration when it is rendered
* @param element the decoration's element
* @param color the color to apply
* @param backgroundColor the background color to apply
* @param borderColor the border color to apply
* @param result the search result associated with the decoration
* @returns
*/
private _applyStyles(element: HTMLElement, color: string, result: ISearchResult): void {
private _applyStyles(element: HTMLElement, backgroundColor: string | undefined, borderColor: string | undefined, result: ISearchResult): void {
if (element.clientWidth <= 0) {
return;
}
if (!element.classList.contains('xterm-find-result-decoration')) {
element.classList.add('xterm-find-result-decoration');
element.style.left = `${element.clientWidth * result.col}px`;
element.style.width = `${element.clientWidth * result.term.length}px`;
element.style.backgroundColor = color;
element.style.opacity = '0.6';
if (backgroundColor) {
element.style.backgroundColor = backgroundColor;
}
if (borderColor) {
element.style.outline = `1px solid ${borderColor}`;
}
}
}

Expand All @@ -648,14 +662,16 @@ export class SearchAddon implements ITerminalAddon {
private _createResultDecoration(result: ISearchResult, decorations: ISearchDecorationOptions): IDecoration | undefined {
const terminal = this._terminal!;
const marker = terminal.registerMarker(-terminal.buffer.active.baseY - terminal.buffer.active.cursorY + result.row);
if (!marker || !decorations?.matchColor) {
if (!marker || !decorations?.matchOverviewRuler) {
return undefined;
}
const findResultDecoration = terminal.registerDecoration(
{ marker,
overviewRulerOptions: this._resultDecorations.get(marker.line) && !this._dataChanged ? undefined : { color: decorations.matchColor, position: 'center' }
});
findResultDecoration?.onRender((e) => this._applyStyles(e, decorations.matchColor, result));
const findResultDecoration = terminal.registerDecoration({
marker,
overviewRulerOptions: this._resultDecorations.get(marker.line) && !this._dataChanged ? undefined : {
color: decorations.matchOverviewRuler, position: 'center'
}
});
findResultDecoration?.onRender((e) => this._applyStyles(e, decorations.matchBackground, decorations.matchBorder, result));
findResultDecoration?.onDispose(() => marker.dispose());
return findResultDecoration;
}
Expand Down
28 changes: 24 additions & 4 deletions addons/xterm-addon-search/typings/xterm-addon-search.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,34 @@ declare module 'xterm-addon-search' {
*/
interface ISearchDecorationOptions {
/**
* The color of a match.
* The background color of a match.
*/
matchColor: string;
matchBackground?: string;

/**
* The color for the currently selected match.
* The border color of a match
*/
selectedColor: string;
matchBorder?: string;

/**
* The overview ruler color of a match.
*/
matchOverviewRuler: string;

/**
* The background color for the currently active match.
*/
activeMatchBackground?: string;

/**
* The border color of the currently active match.
*/
activeMatchBorder?: string;

/**
* The overview ruler color of the currently active match.
*/
activeMatchColorOverviewRuler: string;
}

/**
Expand Down
9 changes: 8 additions & 1 deletion demo/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,14 @@ function getSearchOptions(e: KeyboardEvent): ISearchOptions {
wholeWord: (document.getElementById('whole-word') as HTMLInputElement).checked,
caseSensitive: (document.getElementById('case-sensitive') as HTMLInputElement).checked,
incremental: e.key !== `Enter`,
decorations: (document.getElementById('highlight-all-matches') as HTMLInputElement).checked ? { matchColor: '#555753', selectedColor: '#ef2929' } : undefined
decorations: (document.getElementById('highlight-all-matches') as HTMLInputElement).checked ? {
matchBackground: '#55575380',
matchBorder: '#555753',
matchOverviewRuler: '#555753',
activeMatchBackground: '#ef292980',
activeMatchBorder: '#ef2929',
activeMatchColorOverviewRuler: '#ef2929'
} : undefined
};
}

Expand Down