Skip to content

Commit 7aff44d

Browse files
committed
Add Colorize.rainbow.
1 parent 410cb83 commit 7aff44d

File tree

5 files changed

+46
-4
lines changed

5 files changed

+46
-4
lines changed

build-tests/install-test-workspace/workspace/common/pnpm-lock.yaml

Lines changed: 10 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@rushstack/terminal",
5+
"comment": "Add a `Colorize.rainbow` API.",
6+
"type": "minor"
7+
}
8+
],
9+
"packageName": "@rushstack/terminal"
10+
}

common/reviews/api/terminal.api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ export class Colorize {
6363
// (undocumented)
6464
static magentaBackground(text: string): string;
6565
// (undocumented)
66+
static rainbow(text: string): string;
67+
// (undocumented)
6668
static red(text: string): string;
6769
// (undocumented)
6870
static redBackground(text: string): string;

libraries/terminal/src/Colorize.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ export enum SgrParameterAttribute {
4545
HiddenOff = 28
4646
}
4747

48+
const RAINBOW_SEQUENCE: SgrParameterAttribute[] = [
49+
SgrParameterAttribute.RedForeground,
50+
SgrParameterAttribute.YellowForeground,
51+
SgrParameterAttribute.GreenForeground,
52+
SgrParameterAttribute.CyanForeground,
53+
SgrParameterAttribute.BlueForeground,
54+
SgrParameterAttribute.MagentaForeground
55+
];
56+
4857
/**
4958
* The static functions on this class are used to produce colored text
5059
* for use with a terminal that supports ANSI escape codes.
@@ -256,6 +265,20 @@ export class Colorize {
256265
);
257266
}
258267

268+
public static rainbow(text: string): string {
269+
return Colorize._applyColorSequence(text, RAINBOW_SEQUENCE);
270+
}
271+
272+
private static _applyColorSequence(text: string, sequence: SgrParameterAttribute[]): string {
273+
let result: string = '';
274+
const sequenceLength: number = sequence.length;
275+
for (let i: number = 0; i < text.length; i++) {
276+
result += AnsiEscape.getEscapeSequenceForAnsiCode(sequence[i % sequenceLength]) + text[i];
277+
}
278+
279+
return result + AnsiEscape.getEscapeSequenceForAnsiCode(SgrParameterAttribute.DefaultForeground);
280+
}
281+
259282
private static _wrapTextInAnsiEscapeCodes(startCode: number, endCode: number, text: string): string {
260283
return (
261284
AnsiEscape.getEscapeSequenceForAnsiCode(startCode) +

libraries/terminal/src/test/AnsiEscape.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Colorize } from '../Colorize';
66

77
describe(AnsiEscape.name, () => {
88
it('calls removeCodes() successfully', () => {
9-
const coloredInput: string = Colorize.green('Hello, world!');
9+
const coloredInput: string = Colorize.rainbow('Hello, world!');
1010
const decoloredInput: string = AnsiEscape.removeCodes(coloredInput);
1111
expect(coloredInput).not.toBe(decoloredInput);
1212
expect(decoloredInput).toBe('Hello, world!');

0 commit comments

Comments
 (0)