Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor TypeScript definition to CommonJS compatible export #40

Merged
merged 1 commit into from
Apr 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
287 changes: 158 additions & 129 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,132 @@
import {LiteralUnion} from 'type-fest';
import cliBoxes, {BoxStyle} from 'cli-boxes';

/**
* Placeholder type allowing hex values in `borderColor` and `backgroundColor`.
*
* @todo Remove if [TypeScript issue](https://github.com/Microsoft/TypeScript/issues/29729) is resolved.
*/
type HexColor = string & {hex?: any};

/**
* Characters used for custom border.
*
* @example
*
* // affffb
* // e e
* // dffffc
*
* const border: CustomBorderStyle = {
* topLeft: 'a',
* topRight: 'b',
* bottomRight: 'c',
* bottomLeft: 'd',
* vertical: 'e',
* horizontal: 'f'
* };
*/
export interface CustomBorderStyle extends BoxStyle {}

/**
* Border styles from [`cli-boxes`](https://github.com/sindresorhus/cli-boxes).
*/
export const enum BorderStyle {
declare namespace boxen {
/**
Characters used for custom border.

@example
```
// affffb
// e e
// dffffc

const border: CustomBorderStyle = {
topLeft: 'a',
topRight: 'b',
bottomRight: 'c',
bottomLeft: 'd',
vertical: 'e',
horizontal: 'f'
};
```
*/
interface CustomBorderStyle extends BoxStyle {}

/**
Spacing used for `padding` and `margin`.
*/
interface Spacing {
readonly top: number;
readonly right: number;
readonly bottom: number;
readonly left: number;
}

interface Options {
/**
Color of the box border.
*/
readonly borderColor?: LiteralUnion<
| 'black'
| 'red'
| 'green'
| 'yellow'
| 'blue'
| 'magenta'
| 'cyan'
| 'white'
| 'gray'
| 'grey'
| 'blackBright'
| 'redBright'
| 'greenBright'
| 'yellowBright'
| 'blueBright'
| 'magentaBright'
| 'cyanBright'
| 'whiteBright',
string
>;

/**
Style of the box border.

@default BorderStyle.Single
*/
readonly borderStyle?: BorderStyle | CustomBorderStyle;

/**
Reduce opacity of the border.

@default false
*/
readonly dimBorder?: boolean;

/**
Space between the text and box border.

@default 0
*/
readonly padding?: number | Spacing;

/**
Space around the box.

@default 0
*/
readonly margin?: number | Spacing;

/**
Float the box on the available terminal screen space.

@default 'left'
*/
readonly float?: 'left' | 'right' | 'center';

/**
Color of the background.
*/
readonly backgroundColor?: LiteralUnion<
| 'black'
| 'red'
| 'green'
| 'yellow'
| 'blue'
| 'magenta'
| 'cyan'
| 'white'
| 'blackBright'
| 'redBright'
| 'greenBright'
| 'yellowBright'
| 'blueBright'
| 'magentaBright'
| 'cyanBright'
| 'whiteBright',
string
>;

/**
Align the text in the box based on the widest line.

@default 'left'
*/
readonly align?: 'left' | 'right' | 'center';
}
}

declare const enum BorderStyle {
Single = 'single',
Double = 'double',
Round = 'round',
Expand All @@ -39,110 +135,43 @@ export const enum BorderStyle {
Classic = 'classic'
}

/**
* Spacing used for `padding` and `margin`.
*/
export interface Spacing {
readonly top: number;
readonly right: number;
readonly bottom: number;
readonly left: number;
}

export interface Options {
declare const boxen: {
/**
* Color of the box border.
*/
readonly borderColor?:
| 'black'
| 'red'
| 'green'
| 'yellow'
| 'blue'
| 'magenta'
| 'cyan'
| 'white'
| 'gray'
| 'grey'
| 'blackBright'
| 'redBright'
| 'greenBright'
| 'yellowBright'
| 'blueBright'
| 'magentaBright'
| 'cyanBright'
| 'whiteBright'
| HexColor;
Creates a box in the terminal.

/**
* Style of the box border.
*
* @default BorderStyle.Single
*/
readonly borderStyle?: BorderStyle | CustomBorderStyle;
@param text - The text inside the box.
@returns The box.

/**
* Reduce opacity of the border.
*
* @default false
*/
readonly dimBorder?: boolean;
@example
```
import boxen = require('boxen');

/**
* Space between the text and box border.
*
* @default 0
*/
readonly padding?: number | Spacing;
console.log(boxen('unicorn', {padding: 1}));
// ┌─────────────┐
// │ │
// │ unicorn │
// │ │
// └─────────────┘

/**
* Space around the box.
*
* @default 0
*/
readonly margin?: number | Spacing;
console.log(boxen('unicorn', {padding: 1, margin: 1, borderStyle: 'double'}));
//
// ╔═════════════╗
// ║ ║
// ║ unicorn ║
// ║ ║
// ╚═════════════╝
//
```
*/
(text: string, options?: boxen.Options): string;

/**
* Float the box on the available terminal screen space.
*
* @default 'left'
*/
readonly float?: 'left' | 'right' | 'center';
Border styles from [`cli-boxes`](https://github.com/sindresorhus/cli-boxes).
*/
BorderStyle: typeof BorderStyle;

/**
* Color of the background.
*/
readonly backgroundColor?:
| 'black'
| 'red'
| 'green'
| 'yellow'
| 'blue'
| 'magenta'
| 'cyan'
| 'white'
| 'blackBright'
| 'redBright'
| 'greenBright'
| 'yellowBright'
| 'blueBright'
| 'magentaBright'
| 'cyanBright'
| 'whiteBright'
| HexColor;

/**
* Align the text in the box based on the widest line.
*
* @default 'left'
*/
readonly align?: 'left' | 'right' | 'center';
}
// TODO: Remove this for the next major release
default: typeof boxen;
};

/**
* Creates a box in the terminal.
*
* @param text - The text inside the box.
* @returns The box.
*/
export default function boxen(text: string, options?: Options): string;
export = boxen;
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ const boxen = (text, opts) => {
};

module.exports = boxen;
// TODO: Remove this for the next major release
module.exports.default = boxen;

module.exports._borderStyles = cliBoxes;
5 changes: 3 additions & 2 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {expectType} from 'tsd-check';
import boxen, {Spacing, BorderStyle, CustomBorderStyle} from '.';
import {expectType} from 'tsd';
import boxen = require('.');
import {Spacing, BorderStyle, CustomBorderStyle} from '.';

const border: CustomBorderStyle = {
topLeft: ' ',
Expand Down
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"node": ">=6"
},
"scripts": {
"test": "xo && nyc ava && tsd-check"
"test": "xo && nyc ava && tsd"
},
"files": [
"index.js",
Expand All @@ -33,17 +33,18 @@
],
"dependencies": {
"ansi-align": "^3.0.0",
"camelcase": "^5.0.0",
"camelcase": "^5.3.1",
"chalk": "^2.4.2",
"cli-boxes": "^2.0.0",
"cli-boxes": "^2.1.0",
"string-width": "^3.0.0",
"term-size": "^1.2.0",
"type-fest": "^0.3.0",
"widest-line": "^2.0.0"
},
"devDependencies": {
"ava": "^1.2.1",
"ava": "^1.4.1",
"nyc": "^13.3.0",
"tsd-check": "^0.3.0",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}