ansi-fragments
Advanced tools
Comparing version 0.1.0 to 0.2.0
import IFragment from './IFragment'; | ||
export declare type AnsiColor = 'black' | 'red' | 'green' | 'yellow' | 'blue' | 'magenta' | 'cyan' | 'white' | 'brightBlack' | 'brightRed' | 'brightGreen' | 'brightYellow' | 'brightBlue' | 'brightMagenta' | 'brightCyan' | 'brightWhite' | 'gray' | 'bgBlack' | 'bgRed' | 'bgGreen' | 'bgYellow' | 'bgBlue' | 'bgMagenta' | 'bgCyan' | 'bgWhite' | 'bgBrightBlack' | 'bgBrightRed' | 'bgBrightGreen' | 'bgBrightYellow' | 'bgBrightBlue' | 'bgBrightMagenta' | 'bgBrightCyan' | 'bgBrightWhite'; | ||
export declare type AnsiColor = 'black' | 'red' | 'green' | 'yellow' | 'blue' | 'magenta' | 'cyan' | 'white' | 'brightBlack' | 'brightRed' | 'brightGreen' | 'brightYellow' | 'brightBlue' | 'brightMagenta' | 'brightCyan' | 'brightWhite' | 'gray' | 'bgBlack' | 'bgRed' | 'bgGreen' | 'bgYellow' | 'bgBlue' | 'bgMagenta' | 'bgCyan' | 'bgWhite' | 'bgBrightBlack' | 'bgBrightRed' | 'bgBrightGreen' | 'bgBrightYellow' | 'bgBrightBlue' | 'bgBrightMagenta' | 'bgBrightCyan' | 'bgBrightWhite' | 'none'; | ||
export declare function color(ansiColor: AnsiColor, ...children: Array<string | IFragment>): Color; | ||
@@ -4,0 +4,0 @@ export declare class Color implements IFragment { |
@@ -19,3 +19,6 @@ "use strict"; | ||
const children = utils_1.buildChildren(this.children); | ||
if (this.color in colorette_1.default) { | ||
if (this.color === 'none') { | ||
return children; | ||
} | ||
else if (this.color in colorette_1.default) { | ||
// tslint:disable-next-line: no-unsafe-any no-any | ||
@@ -22,0 +25,0 @@ return colorette_1.default[this.color](children); |
import IFragment from './IFragment'; | ||
export declare type AnsiModifier = 'dim' | 'bold' | 'hidden' | 'italic' | 'underline' | 'strikethrough'; | ||
export declare type AnsiModifier = 'dim' | 'bold' | 'hidden' | 'italic' | 'underline' | 'strikethrough' | 'none'; | ||
export declare function modifier(ansiModifier: AnsiModifier, ...children: Array<string | IFragment>): Modifier; | ||
@@ -4,0 +4,0 @@ export declare class Modifier implements IFragment { |
@@ -19,3 +19,6 @@ "use strict"; | ||
const children = utils_1.buildChildren(this.children); | ||
if (this.modifier in colorette_1.default) { | ||
if (this.modifier === 'none') { | ||
return children; | ||
} | ||
else if (this.modifier in colorette_1.default) { | ||
// tslint:disable-next-line: no-unsafe-any no-any | ||
@@ -22,0 +25,0 @@ return colorette_1.default[this.modifier](children); |
@@ -6,2 +6,4 @@ export { color, AnsiColor } from './fragments/Color'; | ||
export { fixed } from './fragments/Fixed'; | ||
export { ifElse } from './fragments/IfElse'; | ||
export { provide } from './fragments/Provide'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -13,2 +13,6 @@ "use strict"; | ||
exports.fixed = Fixed_1.fixed; | ||
var IfElse_1 = require("./fragments/IfElse"); | ||
exports.ifElse = IfElse_1.ifElse; | ||
var Provide_1 = require("./fragments/Provide"); | ||
exports.provide = Provide_1.provide; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "ansi-fragments", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"main": "build/index.js", | ||
@@ -25,2 +25,3 @@ "license": "MIT", | ||
"lint": "tslint -t stylish --project tsconfig.json", | ||
"test": "jest", | ||
"build": "tsc --build tsconfig.json", | ||
@@ -38,7 +39,14 @@ "clean": "del build", | ||
"@callstack/tslint-config": "^0.1.0", | ||
"@types/jest": "^23.3.13", | ||
"@types/slice-ansi": "^2.0.0", | ||
"@types/strip-ansi": "^3.0.0", | ||
"jest": "^24.0.0", | ||
"ts-jest": "^23.10.5", | ||
"tslint": "^5.12.0", | ||
"typescript": "^3.2.2" | ||
}, | ||
"jest": { | ||
"preset": "ts-jest", | ||
"rootDir": "./src" | ||
} | ||
} |
109
README.md
@@ -12,2 +12,14 @@ # ansi-fragments | ||
- [ansi-fragments](#ansi-fragments) | ||
- [Installation](#installation) | ||
- [Usage](#usage) | ||
- [API](#api) | ||
- [`color`](#color) | ||
- [`modifier`](#modifier) | ||
- [`container`](#container) | ||
- [`pad`](#pad) | ||
- [`fixed`](#fixed) | ||
- [`ifElse`](#ifelse) | ||
- [`provide`](#provide) | ||
## Installation | ||
@@ -46,4 +58,11 @@ | ||
#### `color(ansiColor: AnsiColor, ...children: Array<string | IFragment>): IFragment` | ||
#### `color` | ||
```ts | ||
color( | ||
ansiColor: AnsiColor, | ||
...children: Array<string | IFragment> | ||
): IFragment | ||
``` | ||
Creates fragment for standard ANSI [colors](./src/fragments/Color.ts). | ||
@@ -57,4 +76,11 @@ | ||
#### `modifier(ansiModifier: AnsiModifier, ...children: Array<string | IFragment>): IFragment` | ||
#### `modifier` | ||
```ts | ||
modifier( | ||
ansiModifier: AnsiModifier, | ||
...children: Array<string | IFragment> | ||
): IFragment | ||
``` | ||
Creates fragment for standard ANSI [modifiers](./src/fragments/Modifier.ts): `dim`, `bold`, `hidden`, `italic`, `underline`, `strikethrough`. | ||
@@ -68,4 +94,8 @@ | ||
#### `container(...children: Array<string | IFragment>): IFragment` | ||
#### `container` | ||
```ts | ||
container(...children: Array<string | IFragment>): IFragment | ||
``` | ||
Creates fragment, which sole purpose is to hold and build nested fragments. | ||
@@ -83,4 +113,8 @@ | ||
#### `pad(count: number, separator?: string): IFragment` | ||
#### `pad` | ||
```ts | ||
pad(count: number, separator?: string): IFragment | ||
``` | ||
Creates fragment, which repeats given separator (default: ` `) given number of times. | ||
@@ -94,4 +128,12 @@ | ||
#### `fixed(value: number, bias: 'start' | 'end', ...children: Array<string | IFragment>): IFragment` | ||
#### `fixed` | ||
```ts | ||
fixed( | ||
value: number, | ||
bias: 'start' | 'end', | ||
...children: Array<string | IFragment> | ||
): IFragment | ||
``` | ||
Creates fragment, which makes sure the children will always build to given number of non-ANSI characters. It will either trim the results or add necessary amount of spaces. The `bias` control if trimming/padding should be done at the start of the string representing built children or at the end. | ||
@@ -105,3 +147,60 @@ | ||
#### `ifElse` | ||
```ts | ||
ifElse( | ||
condition: Condition, | ||
ifTrueFragment: string | IFragment, | ||
elseFragment: string | IFragment | ||
): IFragment | ||
type ConditionValue = boolean | string | number | null | undefined; | ||
type Condition = ConditionValue | (() => ConditionValue); | ||
``` | ||
Change the output based on condition. Condition can ba a primitive value, which can be casted to boolean or a function. If conation or return value of condition is evaluated to `true`, the first argument - `ifTrueFragment` will be used, otherwise `elseFragment`. | ||
```js | ||
let condition = getConditionValue() | ||
ifElse( | ||
() => condition, | ||
color('red', 'ERROR'), | ||
color('yellow', 'WARNING') | ||
) | ||
``` | ||
#### `provide` | ||
```ts | ||
provide<T>( | ||
value: T, | ||
builder: (value: T) => string | IFragment | ||
): IFragment | ||
``` | ||
Provides given value to a builder function, which should return `string` or fragment. Useful in situations when the output is connected with some calculated value - using `provide` you only need to calculate final value once and forward it to custom styling logic. | ||
```js | ||
provide(getMessageFromSomewhere(), value => { | ||
switch (value.level) { | ||
case 'error': | ||
return container( | ||
color('red', modifier('bold', value.level.toUpperCase())), | ||
pad(2), | ||
value.log | ||
); | ||
case 'info': | ||
return container( | ||
color('blue', value.level.toUpperCase()), | ||
pad(2), | ||
value.log | ||
); | ||
default: | ||
return container(value.level.toUpperCase(), pad(2), value.log); | ||
} | ||
}) | ||
``` | ||
<!-- badges (common) --> | ||
@@ -108,0 +207,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
33821
46
312
212
8