All files / format / ansi.ts

0.00% Branches 0/1
90.00% Lines 36/40
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 
 
x2
 
 
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
 
x2
 
x1160
 
 
x2
x2
x2
x2
x2
x2
x2
x2
 
x2
x2
x211
x211
x211
x211
x211
x211
x211
x211
x211
x211
x211
x2



I







































// TODO id -> color registry
import type { Stringy } from '../index.ts';
import { env } from '../deps.ts';

export const ifColor  = x => NO_COLOR ? '' : x;
export const escaped  = x => `\x1b[${x}`;
export const NO_COLOR = env.NO_COLOR === '1'                                                                                                                                                                                   
export const RESET    = escaped('0m'); // `\x1b[0m`
export const BOLD     = ifColor(escaped('1m'));
export const DIM      = ifColor(escaped('38;5;245m'));
export const RED      = ifColor(escaped(`31m`));
export const GREEN    = ifColor(escaped(`32m`));
export const YELLOW   = ifColor(escaped(`33m`));
export const BLUE     = ifColor(escaped(`34m`));
export const PURPLE   = ifColor(escaped(`35m`));
export const FG255    = (x: number) => ifColor(escaped(`38;5;${x}m`));
export const BG255    = (x: number) => ifColor(escaped(`48;5;${x}m`));
export const ORANGE   = FG255(208);

export const reset  = (...args: unknown[]) => [...args, RESET].join('');
export const fg255  = (x: number) => (text: string) => `\x1b[38;5;${x}m`+`${text}${RESET}`;
export const bg255  = (x: number) => (text: string) => `\x1b[48;5;${x}m`+`${text}${RESET}`;
export const bold   = (...x: Stringy[]) => reset(BOLD, ...x);
export const dim    = (...x: Stringy[]) => reset(DIM, ...x);
export const red    = (...x: Stringy[]) => reset(RED, ...x);
export const green  = (...x: Stringy[]) => reset(GREEN, ...x);
export const yellow = (...x: Stringy[]) => reset(YELLOW, ...x);
export const blue   = (...x: Stringy[]) => reset(BLUE, ...x);
export const purple = (...x: Stringy[]) => reset(PURPLE, ...x);
export const orange = (...x: Stringy[]) => reset(ORANGE, ...x); // FIXME

export const gray = (depth: number, x: Stringy) => reset(GRAY(depth), x);
export const GRAY = (depth: number) => [
  '\x1b[38;5;255m',
  '\x1b[38;5;254m',
  '\x1b[38;5;253m',
  '\x1b[38;5;252m',
  '\x1b[38;5;251m',
  '\x1b[38;5;250m',
  '\x1b[38;5;249m',
  '\x1b[38;5;248m',
  '\x1b[38;5;247m',
  '\x1b[38;5;246m',
  '\x1b[38;5;245m',
][depth]