Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
12 changes: 11 additions & 1 deletion src/browser/Terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import { MouseZoneManager } from 'browser/MouseZoneManager';
import { AccessibilityManager } from './AccessibilityManager';
import { ITheme, IMarker, IDisposable, ISelectionPosition, ILinkProvider } from 'xterm';
import { DomRenderer } from 'browser/renderer/dom/DomRenderer';
import { IKeyboardEvent, KeyboardResultType, CoreMouseEventType, CoreMouseButton, CoreMouseAction, ITerminalOptions } from 'common/Types';
import { IKeyboardEvent, KeyboardResultType, CoreMouseEventType, CoreMouseButton, CoreMouseAction, ITerminalOptions, IColorRGB } from 'common/Types';
import { evaluateKeyboardEvent } from 'common/input/Keyboard';
import { EventEmitter, IEvent, forwardEvent } from 'common/EventEmitter';
import { DEFAULT_ATTR_DATA } from 'common/buffer/BufferLine';
Expand All @@ -53,6 +53,7 @@ import { Linkifier2 } from 'browser/Linkifier2';
import { CoreBrowserService } from 'browser/services/CoreBrowserService';
import { CoreTerminal } from 'common/CoreTerminal';
import { ITerminalOptions as IInitializedTerminalOptions } from 'common/services/Services';
import { rgba } from 'browser/Color';

// Let it work inside Node.js for automated testing purposes.
const document: Document = (typeof window !== 'undefined') ? window.document : null as any;
Expand Down Expand Up @@ -150,13 +151,22 @@ export class Terminal extends CoreTerminal implements ITerminal {
this.register(this._inputHandler.onRequestWindowsOptionsReport(type => this._reportWindowsOptions(type)));
this.register(forwardEvent(this._inputHandler.onCursorMove, this._onCursorMove));
this.register(forwardEvent(this._inputHandler.onTitleChange, this._onTitleChange));
this.register(this._inputHandler.onAnsiColorChange((index, color) => this._changeAnsiColor(index, color)));
this.register(forwardEvent(this._inputHandler.onA11yChar, this._onA11yCharEmitter));
this.register(forwardEvent(this._inputHandler.onA11yTab, this._onA11yTabEmitter));

// Setup listeners
this.register(this._bufferService.onResize(e => this._afterResize(e.cols, e.rows)));
}

private _changeAnsiColor(colorIndex: number, colorRGB: IColorRGB): void {
const color = rgba.toColor(colorRGB[0], colorRGB[1], colorRGB[2]);

this._colorManager!.colors.ansi[colorIndex] = color;
this._renderService?.setColors(this._colorManager!.colors);
this.viewport?.onThemeChange(this._colorManager!.colors);
}
Comment on lines +162 to +173
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An additional method on Terminal is an unlucky choice, as we try to get rid of helpers on terminal in favor of more direct service usage. Can we get this moved to the color manager directly? Not sure if it is properly service'ified yet, if not we can keep it here and move it later on...

I dont like the ! much, as it might lead to runtime errors in case the manager is indeed not there for whatever reason. Lets better hide the access behind an initial condition test, something like if (!this._colorManager) { return; }...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, if (!this._colorManager) { return; } is a good idea here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jerch The function now exists early if _colorManager is undefined.


public dispose(): void {
if (this._isDisposed) {
return;
Expand Down
32 changes: 31 additions & 1 deletion src/common/InputHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @license MIT
*/

import { IInputHandler, IAttributeData, IDisposable, IWindowOptions } from 'common/Types';
import { IInputHandler, IAttributeData, IDisposable, IWindowOptions, IColorRGB } from 'common/Types';
import { C0, C1 } from 'common/data/EscapeSequences';
import { CHARSETS, DEFAULT_CHARSET } from 'common/data/Charsets';
import { EscapeSequenceParser } from 'common/parser/EscapeSequenceParser';
Expand Down Expand Up @@ -250,6 +250,8 @@ export class InputHandler extends Disposable implements IInputHandler {
public get onScroll(): IEvent<number> { return this._onScroll.event; }
private _onTitleChange = new EventEmitter<string>();
public get onTitleChange(): IEvent<string> { return this._onTitleChange.event; }
private _onAnsiColorChange = new EventEmitter<number, IColorRGB>();
public get onAnsiColorChange(): IEvent<number, IColorRGB> { return this._onAnsiColorChange.event; }

constructor(
private readonly _bufferService: IBufferService,
Expand Down Expand Up @@ -372,6 +374,7 @@ export class InputHandler extends Disposable implements IInputHandler {
this._parser.setOscHandler(2, new OscHandler((data: string) => this.setTitle(data)));
// 3 - set property X in the form "prop=value"
// 4 - Change Color Number
this._parser.setOscHandler(4, new OscHandler((data: string) => this.setAnsiColor(data)));
// 5 - Change Special Color Number
// 6 - Enable/disable Special Color Number c
// 7 - current directory? (not in xterm spec, see https://gitlab.com/gnachman/iterm2/issues/3939)
Expand Down Expand Up @@ -2711,6 +2714,33 @@ export class InputHandler extends Disposable implements IInputHandler {
this._iconName = data;
}

/**
* OSC 4; <num> ; <text> ST (set ANSI color <num> to <text>)
*
* The expected content of data is: <number>;rgb:<rr>/<gg>/<bb> where rr, gg, bb are hex numbers.
*/
public setAnsiColor(data: string): void {
// example data: 5;rgb:aa/bb/cc
const regex = /(\d+);rgb:([0-9a-fA-F]{2})\/([0-9a-fA-F]{2})\/([0-9a-fA-F]{2})/;
const match = data.match(regex);

if (match) {
const colorIndex = parseInt(match[1]);
const color: IColorRGB = [
parseInt(match[2], 16),
parseInt(match[3], 16),
parseInt(match[4], 16)
];

//TODO: remove debug
console.log(`Setting ANSI color ${colorIndex} to RGB value ${color}`);
this._onAnsiColorChange.fire(colorIndex, color);
}
else {
this._logService.warn(`Expected format <num>;rgb:<rr>/<gg>/<bb> but got data: ${data}`);
}
}

/**
* ESC E
* C1.NEL
Expand Down
1 change: 1 addition & 0 deletions src/common/Types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ export interface IInputHandler {
/** CSI ' ~ */ deleteColumns(params: IParams): void;
/** OSC 0
OSC 2 */ setTitle(data: string): void;
/** OSC 4 */ setAnsiColor(data: string): void;
/** ESC E */ nextLine(): void;
/** ESC = */ keypadApplicationMode(): void;
/** ESC > */ keypadNumericMode(): void;
Expand Down