Skip to content

Commit

Permalink
Merge pull request xtermjs#3905 from silamon/extendedcolors
Browse files Browse the repository at this point in the history
Support setting extended ansi colors (16-255)
  • Loading branch information
Tyriar authored Jul 18, 2022
2 parents 86cfad5 + c786c6a commit cbb13aa
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
21 changes: 20 additions & 1 deletion src/browser/ColorManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import jsdom = require('jsdom');
import { assert } from 'chai';
import { ColorManager } from 'browser/ColorManager';
import { ColorManager, DEFAULT_ANSI_COLORS } from 'browser/ColorManager';

describe('ColorManager', () => {
let cm: ColorManager;
Expand Down Expand Up @@ -309,5 +309,24 @@ describe('ColorManager', () => {
// FG reverts back to default
assert.equal(cm.colors.foreground.css, '#ffffff');
});

it('should set all extended ansi colors in reverse order', () => {
cm.setTheme({
extendedAnsi: DEFAULT_ANSI_COLORS.map(a => a.css).slice().reverse()
});

for (let ansiColor = 16; ansiColor <= 255; ansiColor++){
assert.equal(cm.colors.ansi[ansiColor].css, DEFAULT_ANSI_COLORS[255 + 16 - ansiColor].css);
}
});

it('should set one extended ansi colors and keep the other default', () => {
cm.setTheme({
extendedAnsi: [ '#ffffff' ]
});

assert.equal(cm.colors.ansi[16].css, '#ffffff');
assert.equal(cm.colors.ansi[17].css, DEFAULT_ANSI_COLORS[17].css);
});
});
});
6 changes: 6 additions & 0 deletions src/browser/ColorManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ export class ColorManager implements IColorManager {
this.colors.ansi[13] = this._parseColor(theme.brightMagenta, DEFAULT_ANSI_COLORS[13]);
this.colors.ansi[14] = this._parseColor(theme.brightCyan, DEFAULT_ANSI_COLORS[14]);
this.colors.ansi[15] = this._parseColor(theme.brightWhite, DEFAULT_ANSI_COLORS[15]);
if (theme.extendedAnsi) {
const colorCount = Math.max(theme.extendedAnsi.length + 16, 256);
for (let i = 16; i < colorCount; i++) {
this.colors.ansi[i] = this._parseColor(theme.extendedAnsi[i - 16], DEFAULT_ANSI_COLORS[i]);
}
}
// Clear our the cache
this._contrastCache.clear();
this._updateRestoreColors();
Expand Down
1 change: 1 addition & 0 deletions src/common/services/Services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ export interface ITheme {
brightMagenta?: string;
brightCyan?: string;
brightWhite?: string;
extendedAnsi?: string[];
}

export const IUnicodeService = createDecorator<IUnicodeService>('UnicodeService');
Expand Down
2 changes: 2 additions & 0 deletions typings/xterm-headless.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ declare module 'xterm-headless' {
brightCyan?: string;
/** ANSI bright white (eg. `\x1b[1;37m`) */
brightWhite?: string;
/** ANSI extended colors (16-255) */
extendedAnsi?: string[];
}

/**
Expand Down
4 changes: 3 additions & 1 deletion typings/xterm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,8 @@ declare module 'xterm' {
brightCyan?: string;
/** ANSI bright white (eg. `\x1b[1;37m`) */
brightWhite?: string;
/** ANSI extended colors (16-255) */
extendedAnsi?: string[];
}

/**
Expand Down Expand Up @@ -845,7 +847,7 @@ declare module 'xterm' {
* Adds an event listener for when data has been parsed by the terminal,
* after {@link write} is called. This event is useful to listen for any
* changes in the buffer.
*
*
* This fires at most once per frame, after data parsing completes. Note
* that this can fire when there are still writes pending if there is a lot
* of data.
Expand Down

0 comments on commit cbb13aa

Please sign in to comment.