diff --git a/__test__/main.js b/__test__/main.js index 71821b6..7466c5f 100644 --- a/__test__/main.js +++ b/__test__/main.js @@ -14,6 +14,10 @@ Pikmin.bind(pikmin, console); pikmin.info(Symbol.iterator); pikmin.info({ 'test': 123 }); +pikmin.info(Pikmin.colors.hex([ 255, 255, 255 ])); +pikmin.info(Pikmin.colors.rgb('#FFFFFF')); +pikmin.info(Pikmin.colors.convert([ 0, 0, 0 ])); +pikmin.info(Pikmin.colors.convert('#000000')); pikmin.error('This is an error!'); require('./global.js')(); diff --git a/index.d.ts b/index.d.ts index e2be60a..18ec67d 100644 --- a/index.d.ts +++ b/index.d.ts @@ -3,189 +3,185 @@ import { WriteStream, PathLike } from "fs"; /** * The pikmin namespace. Start here! */ -declare namespace Pikmin -{ - /** The loggers collection */ - export const loggers: Pikmin.Collection; +declare namespace Pikmin { + /** The loggers collection */ + export const loggers: Pikmin.Collection; - /** The version of Pikmin */ - export const version: string; + /** The version of Pikmin */ + export const version: string; - /** The colors constant */ - export const colors: Pikmin.Colors; + /** The colors constant */ + export const colors: Pikmin.Colors; + + /** + * Binds the transport's functions into `a` (default: NodeJS.Console) + * @param i The instance to bind + * @param a The class you wanna bind it to, the default is: NodeJS.Console + * @param b Anything else to bind? + */ + export function bind(i: Pikmin.instance, a?: Console, b?: any): void; + + /** + * Unbinds Pikmin from the `NodeJS.Console` interface + * @param i The instance + */ + export function unbind(i: Pikmin.instance): void; + + /** The instance itself */ + export class instance { + [x: string]: (m: any) => void; /** - * Binds the transport's functions into `a` (default: NodeJS.Console) - * @param i The instance to bind - * @param a The class you wanna bind it to, the default is: NodeJS.Console - * @param b Anything else to bind? + * Construct a new instance of the Pikmin instance + * @param options The options to bind */ - export function bind(i: Pikmin.instance, a?: Console, b?: any): void; + constructor(options?: Pikmin.InstanceOptions); + + /** The log object */ + public log: { + [x: string]: any; + + /** + * The binding to bind to the console; accessable by `Pikmin#bind` + */ + __bound__: Console; + } + + /** The name of the instance */ + public name: string; + + /** The base format */ + public baseFormat: string; + + /** An array of transports */ + public transports: Pikmin.Transport[]; /** - * Unbinds Pikmin from the `NodeJS.Console` interface - * @param i The instance + * Adds a transport + * @param transport The transport to add + * @param options Any options to bind */ - export function unbind(i: Pikmin.instance): void; + public addTransport(transport: Pikmin.Transport, options?: { autogen: boolean }): void; + } - /** The instance itself */ - export class instance - { - [x: string]: (m: any) => void; - - /** - * Construct a new instance of the Pikmin instance - * @param options The options to bind - */ - constructor(options?: Pikmin.InstanceOptions); + export class Collection extends Map + { + public filter(i: (a: T) => boolean): T[]; + public map(i: (a: T) => any): T[]; + } - /** The log object */ - public log: { - [x: string]: any; + /** + * The console transport that logs anything to the terminal + */ + export class ConsoleTransport implements Transport { + constructor(options: Pikmin.InstanceOptions & { process: NodeJS.Process }); - /** - * The binding to bind to the console; accessable by `Pikmin#bind` - */ - __bound__: Console; - } + /** The type of the transport, implemented by the Transport interface */ + public type: 'CONSOLE' | 'WEBHOOK' | 'FILE'; - /** The name of the instance */ - public name: string; + /** The name of the transport, implemented by the Transport interface */ + public name: string; - /** The base format */ - public baseFormat: string; + /** The parent of the transport, implemented by the Transport interface; resolves by `undefined` */ + public parent: any | undefined; - /** An array of transports */ - public transports: Pikmin.Transport[]; + /** The defaults of the transport, implemented by the Transport interface */ + public defaults: { inspect: boolean } - /** - * Adds a transport - * @param transport The transport to add - * @param options Any options to bind - */ - public addTransport(transport: Pikmin.Transport, options?: { autogen: boolean }): void; + /** The NodeJS.Process tty, only to use Stdout#write */ + public process: NodeJS.Process; + append(options: { inspect: boolean }, data: string): void; + destroy(): this; + } + + export class FileTransport implements Transport { + constructor(options: { process: NodeJS.Process, path: string | PathLike, flags?: '-a' }); + + /** The type of the transport, implemented by the Transport interface */ + public type: 'CONSOLE' | 'WEBHOOK' | 'FILE'; + + /** The name of the transport, implemented by the Transport interface */ + public name: string; + + /** The parent of the transport, implemented by the Transport interface; resolves by `undefined` */ + public parent: any | undefined; + + /** The defaults of the transport, implemented by the Transport interface */ + public defaults: { inspect: boolean } + + /** The write stream */ + public stream: WriteStream; + append(options: { inspect: boolean }, data: string): void; + destroy(): this; + } + + /** The colors interface, run by `colors` */ + export interface Colors { + black: Color; + red: Color; + green: Color; + yellow: Color; + blue: Color; + magenta: Color; + cyan: Color; + white: Color; + bgBlack: Color; + bgRed: Color; + bgGreen: Color; + bgYellow: Color; + bgBlue: Color; + bgCyan: Color; + bgWhite: Color; + blackBright: Color; + redBright: Color; + greenBright: Color; + yellowBright: Color; + blueBright: Color; + magentaBright: Color; + cyanBright: Color; + whiteBright: Color; + bgBlackBright: Color; + bgRedBright: Color; + bgGreenBright: Color; + bgYellowBright: Color; + bgBlueBright: Color; + bgMagentaBright: Color; + bgCyanBright: Color; + bgWhiteBright: Color; + reset: Color; + bold: Color; + dim: Color; + italic: Color; + underline: Color; + blink: Color; + inverse: Color; + strikethrough: Color; + hex: (hex: string) => string | undefined; + rgb: (val: number[]) => string | undefined; + convert: (value: string | number[], to: 'hex' | 'rgb') => string | number[] | undefined; + strip: (val: string) => string; + supported: boolean; + } + + export interface InstanceOptions { + name?: string; + format?: string; + autogen?: boolean; + transports?: Pikmin.Transport[]; + } + + export interface Transport { + type: 'CONSOLE' | 'WEBHOOK' | 'FILE'; + name: string; + parent: any | undefined; + defaults: { + inspect: boolean } + append(options: { inspect: boolean }, data: string): void; + destroy(): this; + } - export class Collection extends Map - { - public filter(i: (a: T) => boolean): T[]; - public map(i: (a: T) => any): T[]; - } - - /** - * The console transport that logs anything to the terminal - */ - export class ConsoleTransport implements Transport - { - constructor(options: Pikmin.InstanceOptions & { process: NodeJS.Process }); - - /** The type of the transport, implemented by the Transport interface */ - public type: 'CONSOLE' | 'WEBHOOK' | 'FILE'; - - /** The name of the transport, implemented by the Transport interface */ - public name: string; - - /** The parent of the transport, implemented by the Transport interface; resolves by `undefined` */ - public parent: any | undefined; - - /** The defaults of the transport, implemented by the Transport interface */ - public defaults: { inspect: boolean } - - /** The NodeJS.Process tty, only to use Stdout#write */ - public process: NodeJS.Process; - append(options: { inspect: boolean }, data: string): void; - destroy(): this; - } - - export class FileTransport implements Transport - { - constructor(options: { process: NodeJS.Process, path: string | PathLike, flags?: '-a' }); - - /** The type of the transport, implemented by the Transport interface */ - public type: 'CONSOLE' | 'WEBHOOK' | 'FILE'; - - /** The name of the transport, implemented by the Transport interface */ - public name: string; - - /** The parent of the transport, implemented by the Transport interface; resolves by `undefined` */ - public parent: any | undefined; - - /** The defaults of the transport, implemented by the Transport interface */ - public defaults: { inspect: boolean } - - /** The write stream */ - public stream: WriteStream; - append(options: { inspect: boolean }, data: string): void; - destroy(): this; - } - - /** The colors interface, run by `Pikmin.colors` */ - export interface Colors - { - black: Color; - red: Color; - green: Color; - yellow: Color; - blue: Color; - magenta: Color; - cyan: Color; - white: Color; - bgBlack: Color; - bgRed: Color; - bgGreen: Color; - bgYellow: Color; - bgBlue: Color; - bgCyan: Color; - bgWhite: Color; - blackBright: Color; - redBright: Color; - greenBright: Color; - yellowBright: Color; - blueBright: Color; - magentaBright: Color; - cyanBright: Color; - whiteBright: Color; - bgBlackBright: Color; - bgRedBright: Color; - bgGreenBright: Color; - bgYellowBright: Color; - bgBlueBright: Color; - bgMagentaBright: Color; - bgCyanBright: Color; - bgWhiteBright: Color; - reset: Color; - bold: Color; - dim: Color; - italic: Color; - underline: Color; - blink: Color; - inverse: Color; - strikethrough: Color; - strip: (val: string) => string; - supported: boolean; - } - - export interface InstanceOptions - { - name?: string; - format?: string; - autogen?: boolean; - transports?: Pikmin.Transport[]; - } - - export interface Transport - { - type: 'CONSOLE' | 'WEBHOOK' | 'FILE'; - name: string; - parent: any | undefined; - defaults: { - inspect: boolean - } - append(options: { inspect: boolean }, data: string): void; - destroy(): this; - } - - export type Color = (s: string) => string; + export type Color = (s: string) => string; } export = Pikmin; diff --git a/lib/colors/index.js b/lib/colors/index.js index ad5c3dd..30a2422 100644 --- a/lib/colors/index.js +++ b/lib/colors/index.js @@ -1,7 +1,47 @@ const s = require('./styles.js'); +const toHex = (number) => { + number = parseInt(number, 10); + if (isNaN(number)) return '00'; + number = Math.max(0, Math.min(number, 255)); + + return `${'0123456789ABCDEF'.charAt((number - number % 16) / 16)}${'0123456789ABCDEF'.charAt((number % 16))}`; +}; + +const convert = (content = 'FFFFFF', to) => { + if (to === 'hex' || (content instanceof Array && content.length > 2)) return `${toHex(content[0])}${toHex(content[1])}${toHex(content[2])}`; + else if (to === 'rgb' || content.startsWith('#') || (content instanceof String && content.length === 3 || content.length === 6)) { + let rgb = []; + + if (content.length < 1) content = '000000'; + if (content.charAt(0) === '#') content = content.substring(1, content.length); + if (content.length !== 6 && content.length != 3) return undefined; + + if (content.length === 3) { + rgb.push(content.substring(0, 1)); + rgb.push(content.substring(1, 2)); + rgb.push(content.substring(2, 3)); + rgb[0] = rgb[0] + rgb[0]; + rgb[1] = rgb[1] + rgb[1]; + rgb[2] = rgb[2] + rgb[2]; + } else { + rgb.push(content.substring(0, 2)); + rgb.push(content.substring(2, 4)); + rgb.push(content.substring(4, 6)); + } + + rgb[0] = parseInt(rgb[2], 16); + rgb[1] = parseInt(rgb[2], 16); + rgb[2] = parseInt(rgb[2], 16); + + return rgb; + } else return undefined; +}; for (const key in s.colors) exports[key] = (val) => { return this.supported ? `\x1b[${s.colors[key]}m${val}\x1b[0m` : val; }; for (const key in s.styles) exports[key] = (val) => { return this.supported ? `\x1b[${s.styles[key]}m${val}\x1b[0m` : val; }; -exports.strip = (val) => { return (`${val}`).replace(/\x1B\[\d+m/g, ''); }; -exports.supported = (process.env.FORCE_COLOR || process.platform === 'win32' || (process.stdout.isTTY && process.env.TERM && process.env.TERM !== 'dumb')); \ No newline at end of file +exports.hex = (hex = 'FFFFFF') => convert(hex, 'hex'); +exports.rgb = (rgb = [255, 255, 255]) => convert(rgb, 'hex'); +exports.strip = (value) => { return (`${value}`).replace(/\x1B\[\d+m/g, '') }; +exports.convert = (content = 'FFFFFF', to) => convert(content, to); +exports.supported = (process.env.FORCE_COLOR || process.platform === 'win32' || (process.stdout.isTTY && process.env.TERM && process.env.TERM !== 'dumb'));