Skip to content

Commit

Permalink
typescript port (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcralph committed Nov 2, 2020
1 parent adc84c5 commit dc181b6
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 72 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.vscode/
build/
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ declare module 'leeks.js' {
/** Alias for `colours` */
export const colors: { [x in Colors]: (t: string) => string; };

/** Change the colour of the given text. (List: https://derpyenterprises.org/docs/#/leeks) */
/** Change the colour of the given text. (List: https://docs.derpyenterprises.org/#/leeks) */
export const colours: { [x in Colors]: (t: string) => string };

/** Change the style of the given text. (List: https://derpyenterprises.org/docs/#/leeks) */
/** Change the style of the given text. (List: https://docs.derpyenterprises.org/#/leeks) */
export const styles: { [x in Styles]: (t: string) => string };

/** Alias for `supportsColour` */
Expand Down
56 changes: 0 additions & 56 deletions lib/data.js

This file was deleted.

40 changes: 40 additions & 0 deletions lib/data/Colors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
enum colors {
black = 30,
red = 31,
green = 32,
yellow = 33,
blue = 34,
magenta = 35,
cyan = 36,
white = 37,
gray = 90,
grey = 90,
blackBright = 90,
redBright = 91,
greenBright = 92,
yellowBright = 93,
blueBright = 94,
magentaBright = 95,
cyanBright = 96,
whiteBright = 97,
bgBlack = 40,
bgRed = 41,
bgGreen = 42,
bgYellow = 43,
bgBlue = 44,
bgMagenta = 45,
bgCyan = 46,
bgWhite = 47,
bgBlackBright = 100,
bgRedBright = 101,
bgGreenBright = 102,
bgYellowBright = 103,
bgBlueBright = 104,
bgMagentaBright = 105,
bgCyanBright = 106,
bgWhiteBright = 107,
bgGray = 100,
bgGrey = 100,
}

export default colors;
18 changes: 18 additions & 0 deletions lib/data/Styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
enum styles {
reset = 0,
bold = 1,
dim = 2,
italic = 3,
underline = 4,
blink = 5,
inverse = 7,
strikethrough = 9,
hidden = 8,
shown = 28,
nostrikethrough = 29,
nounderline = 24,
noblink = 25,
noinverse = 27
}

export default styles;
27 changes: 16 additions & 11 deletions lib/index.js → lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
* @copyright David Ralph (ohlookitsderpy) 2019-2020
* @license MIT
*/
const d = require('./data.js');
import Colors from './data/Colors';
import Styles from './data/Styles';

/** Change the colour of the given text. (List: https://derpyenterprises.org/docs/#/leeks) */
/** Change the colour of the given text. (List: https://docs.derpyenterprises.org/#/leeks) */
let colors = [];
for (let c in d.colors) colors[c] = (t) => `\x1b[${d.colors[c]}m${t}\x1b[0m`;
for (let c in Colors) colors[c] = (t: any) => `\x1b[${Colors[c]}m${t}\x1b[0m`;

/** Change the style of the given text. (List: https://derpyenterprises.org/docs/#/leeks) */
/** Change the style of the given text. (List: https://docs.derpyenterprises.org/#/leeks) */
let styles = [];
for (let s in d.styles) styles[s] = (t) => `\x1b[${d.styles[s]}m${t}\x1b[0m`;
for (let s in Styles) styles[s] = (t: any) => `\x1b[${Styles[s]}m${t}\x1b[0m`;

/**
* Check if colours are supported.
Expand All @@ -24,21 +25,25 @@ let supports = !('NO_COLOR' in process.env) && process.env.FORCE_COLOR !== '0' &
* @param {string} i The 8-bit color to use
* @param {string} t The text to show with the 8-bit colour
*/
module.exports.eightBit = (i, t) => '\033' + `[38;5;${i}m${t}\x1b[0m`;
export function eightBit (i: string, t:string) {
return '\033' + `[38;5;${i}m${t}\x1b[0m`;
}

/**
* Change the background colour of the given text using 8-bit colours.
* @param {string} i The 8-bit colour to use
* @param {string} t The text to show with the 8-bit colour
*/
module.exports.eightBitBg = (i, t) => '\033' + `[48;5;${i}m${t}\x1b[0m`;
export function eightBitBg (i: string, t: string) {
return '\033' + `[48;5;${i}m${t}\x1b[0m`;
}

/**
* Change the colour of the given text using RGB.
* @param {[number, number, number]} rgb An array of the RGB to use
* @param {string} t The text to show with the RGB colour
*/
module.exports.rgb = (rgb, t) => {
export function rgb (rgb: Array<number>, t: string) {
const [r, g, b] = rgb;
return '\033' + `[38;2;${r};${g};${b}m${t}\x1b[0m`;
};
Expand All @@ -48,7 +53,7 @@ module.exports.rgb = (rgb, t) => {
* @param {[number, number, number]} rgb An array of the RGB to use
* @param {string} t The text to show with the RGB colour
*/
module.exports.rgbBg = (rgb, t) => {
export function rgbBg (rgb: Array<number>, t: string) {
const [r, g, b] = rgb;
return '\033' + `[48;2;${r};${g};${b}m${t}\x1b[0m`;
};
Expand All @@ -59,7 +64,7 @@ module.exports.rgbBg = (rgb, t) => {
* @param {string} t The text to show with the hexadecimal colour
* @credit [Stack Overflow](https://stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb)
*/
module.exports.hex = (hex, t) => {
export function hex (hex: string, t: string) {
const bigint = parseInt(hex.replace('#', ''), 16);
return module.exports.rgb([(bigint >> 16) & 255, (bigint >> 8) & 255, bigint & 255], t);
};
Expand All @@ -70,7 +75,7 @@ module.exports.hex = (hex, t) => {
* @param {string} t The text to show with the hexadecimal colour
* @credit [Stack Overflow](https://stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb)
*/
module.exports.hexBg = (hex, t) => {
export function hexBg (hex: string, t: string) {
const bigint = parseInt(hex.replace('#', ''), 16);
return module.exports.rgbBg([(bigint >> 16) & 255, (bigint >> 8) & 255, bigint & 255], t);
};
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "leeks.js",
"version": "0.0.9",
"version": "0.1.0",
"description": "Simple ANSI styling for your terminal.",
"author": "David \"ohlookitsderpy\" Ralph",
"license": "MIT",
"main": "lib/index.js",
"main": "build/index.js",
"types": "index.d.ts",
"bugs": {
"url": "https://github.com/ohlookitsderpy/leeks.js/issues"
Expand Down
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const leeks = require('./lib/index.js'); // Replace with "leeks.js" if not using from the Git repository
const leeks = require('./build/index.js'); // Replace with "leeks.js" if not using from the Git repository

const error = leeks.colors.red;
console.log(error('Error - There were no leeks!'));
Expand Down
11 changes: 11 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "CommonJS",
"lib": ["ES2015", "ES2016", "ES2017", "ES2018", "ES2019", "ES2020", "ESNext"],
"outDir": "./build",
"rootDir": "./lib",
"noImplicitUseStrict": true
},
"include": ["lib/**/*"],
}

0 comments on commit dc181b6

Please sign in to comment.