Skip to content

Commit

Permalink
Merge pull request #8 from joutvhu/development
Browse files Browse the repository at this point in the history
Support 256 colors
  • Loading branch information
joutvhu authored Apr 20, 2021
2 parents 012f2f1 + e5abceb commit b34a6da
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 30 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,26 @@ console.log(terminal.style.italic.bgBlack.white.underline('Hello'));
console.log(terminal.style.white.inverse.bgMagentaBright('Hello'));
```

### True color support
### Support 256 color and True color

Control Terminal support True color (16 million colors) on terminal applications.
Control Terminal support 256 colors and True color (16 million colors) on terminal applications.

The following color models can be used:

256 colors:

- `ansi256(code: number)`

```js
terminal.style.ansi256(12)('Blue text');
```

- `bgAnsi256(code: number)`

```js
terminal.style.bgAnsi256(12)('Blue background');
```

RGB:

- `rgb(red: number, green: number, blue: number)`
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "control-terminal",
"version": "1.0.0-alpha.1",
"version": "1.0.0-alpha.2",
"description": "Control terminal cursor, screen and create string styling",
"scripts": {
"build": "npm run clean && tsc --project tsconfig.json",
Expand Down
24 changes: 16 additions & 8 deletions src/cursor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class TerminalCursor {
public escape(command: string): void {
public escape(command: string): TerminalCursor {
process.stdout.write(`\x1b${command}`);
return this;
}

public goTo(line: number, column: number): TerminalCursor {
Expand Down Expand Up @@ -48,33 +49,40 @@ class TerminalCursor {
return this;
}

public hide(): void {
public hide(): TerminalCursor {
process.stdout.write('\x1b[?25l');
return this;
}

public clearScreen(): void {
public clearScreen(): TerminalCursor {
process.stdout.write('\x1b[2J');
return this;
}

public reset(): void {
public reset(): TerminalCursor {
process.stdout.write('\x1bc');
return this;
}

public eraseToEndLine(): void {
public eraseToEndLine(): TerminalCursor {
process.stdout.write('\x1b[K');
return this;
}

public eraseToEndScreen(): void {
public eraseToEndScreen(): TerminalCursor {
process.stdout.write('\x1b[J');
return this;
}

public eraseCurrentLine(): void {
public eraseCurrentLine(): TerminalCursor {
process.stdout.write('\r\x1b[K');
return this;
}

public erasePreviousLine(line: number = 1): void {
public erasePreviousLine(line: number = 1): TerminalCursor {
if (typeof line === 'number' && line > 0)
process.stdout.write(`\x1b[${line}A\r\x1b[K`);
return this;
}

public save(): TerminalCursor {
Expand Down
26 changes: 19 additions & 7 deletions src/style.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {hexToRgb, rgbToRgb} from './util';
import {hexToRgb, validColors} from './util';

interface TerminalStyle {
reset: TerminalStyleBuilder;
Expand Down Expand Up @@ -50,10 +50,14 @@ interface TerminalStyle {
bgCyanBright: TerminalStyleBuilder;
bgWhiteBright: TerminalStyleBuilder;

ansi256(code: number): TerminalStyleBuilder;

rgb(red: number, green: number, blue: number): TerminalStyleBuilder;

hex(hex: string): TerminalStyleBuilder;

bgAnsi256(code: number): TerminalStyleBuilder;

bgRgb(red: number, green: number, blue: number): TerminalStyleBuilder;

bgHex(hex: string): TerminalStyleBuilder;
Expand Down Expand Up @@ -89,8 +93,6 @@ const STYLES: any = {
magenta: 35,
cyan: 36,
white: 37,

// Bright color
blackBright: 90,
redBright: 91,
greenBright: 92,
Expand All @@ -109,8 +111,6 @@ const STYLES: any = {
bgMagenta: 45,
bgCyan: 46,
bgWhite: 47,

// Bright color
bgBlackBright: 100,
bgRedBright: 101,
bgGreenBright: 102,
Expand Down Expand Up @@ -153,9 +153,15 @@ function defineProperties(builder: TerminalStyleBuilder | any, styles: [string,
}
};
}
properties.ansi256 = {
value: (code: number): TerminalStyleBuilder => {
[code] = validColors(code);
return createBuilder([...styles, [`\x1b[38;5;${code}m`, '\x1b[39m']]);
}
};
properties.rgb = {
value: (red: number, green: number, blue: number): TerminalStyleBuilder => {
[red, green, blue] = rgbToRgb(red, green, blue);
[red, green, blue] = validColors(red, green, blue);
return createBuilder([...styles, [`\x1b[38;2;${red};${green};${blue}m`, '\x1b[39m']]);
}
};
Expand All @@ -173,9 +179,15 @@ function defineProperties(builder: TerminalStyleBuilder | any, styles: [string,
}
};
}
properties.bgAnsi256 = {
value: (code: number): TerminalStyleBuilder => {
[code] = validColors(code);
return createBuilder([...styles, [`\x1b[48;5;${code}m`, '\x1b[49m']]);
}
};
properties.bgRgb = {
value: (red: number, green: number, blue: number): TerminalStyleBuilder => {
[red, green, blue] = rgbToRgb(red, green, blue);
[red, green, blue] = validColors(red, green, blue);
return createBuilder([...styles, [`\x1b[48;2;${red};${green};${blue}m`, '\x1b[49m']]);
}
};
Expand Down
16 changes: 4 additions & 12 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
export function rgbToRgb(r: number, g: number, b: number): number[] {
return [
r < 0 ? 0 : r > 255 ? 255 : r,
g < 0 ? 0 : g > 255 ? 255 : g,
b < 0 ? 0 : b > 255 ? 255 : b
];
export function validColors(...colors: number[]): number[] {
return colors.map(value => value < 0 ? 0 : value > 255 ? 255 : value);
}

export function hexToRgb(hex: string): number[] {
Expand All @@ -13,12 +9,8 @@ export function hexToRgb(hex: string): number[] {
return [0, 0, 0];
let {color}: any = matches.groups;
if (color.length === 3)
color = color.split('').map((character: any) => character + character).join('');
color = color.split('').map((char: string) => char + char).join('');
const integer = Number.parseInt(color, 16);

return [
(integer >> 16) & 0xFF,
(integer >> 8) & 0xFF,
integer & 0xFF
];
return [(integer >> 16) & 0xFF, (integer >> 8) & 0xFF, integer & 0xFF];
}

0 comments on commit b34a6da

Please sign in to comment.