implementation ready

This commit is contained in:
Philipp Kunz 2017-01-21 18:10:27 +01:00
parent e284808d29
commit 8bb91ae95d
7 changed files with 203 additions and 1 deletions

14
dist/index.d.ts vendored Normal file
View File

@ -0,0 +1,14 @@
import 'typings-global';
/**
* all the color names that are available for proper xterm translation
*/
export declare type TColorName = 'black' | 'blue' | 'brown' | 'cyan' | 'green' | 'red' | 'orange' | 'white';
export interface IRGB {
r: number;
b: number;
g: number;
}
/**
* color a string with xterm
*/
export declare let coloredString: (stringArg: string, colorFontArg: TColorName, colorBackgroundArg?: TColorName) => string;

54
dist/index.js vendored Normal file
View File

@ -0,0 +1,54 @@
"use strict";
require("typings-global");
const ansiColors = require("ansi-256-colors");
/**
* the color translator function
*/
let colorTranslator = (colorArg) => {
switch (colorArg) {
case 'blue':
return { r: 0, g: 1, b: 5 };
case 'cyan':
return { r: 0, g: 4, b: 4 };
case 'green':
return { r: 2, g: 5, b: 0 };
case 'red':
return { r: 5, g: 0, b: 0 };
case 'orange':
return { r: 5, g: 3, b: 0 };
case 'brown':
return { r: 1, g: 0, b: 0 };
case 'black':
return { r: 0, g: 0, b: 0 };
case 'white':
return { r: 5, g: 5, b: 5 };
default:
return { r: 5, g: 5, b: 5 };
}
};
/**
* colors the font of a string
*/
let coloredFont = (stringArg, colorArg) => {
let rgbCode = colorTranslator(colorArg);
return ansiColors.fg.getRgb(rgbCode.r, rgbCode.g, rgbCode.b) + stringArg;
};
/**
* colors the back of a string
*/
let coloredBackground = (stringArg, colorArg) => {
let rgbCode = colorTranslator(colorArg);
return ansiColors.bg.getRgb(rgbCode.r, rgbCode.g, rgbCode.b) + stringArg;
};
/**
* color a string with xterm
*/
exports.coloredString = (stringArg, colorFontArg, colorBackgroundArg) => {
let returnString = coloredFont(stringArg, colorFontArg);
if (colorBackgroundArg) {
returnString = coloredBackground(returnString, colorBackgroundArg);
}
returnString = returnString + ansiColors.reset;
return returnString;
};
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJma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QUMsQ0FBQyxHQUFHLFNBQVMsQ0FBQTtBQUM1RSxDQUFDLENBQUE7QUFFRDs7R0FFRztBQUNRLFFBQUEsYUFBYSxHQUFHLENBQUMsU0FBaUIsRUFBRSxZQUF3QixFQUFFLGtCQUErQjtJQUNwRyxJQUFJLFlBQVksR0FBRyxXQUFXLENBQUMsU0FBUyxFQUFFLFlBQVksQ0FBQyxDQUFBO0lBQ3ZELEVBQUUsQ0FBQyxDQUFDLGtCQUFrQixDQUFDLENBQUMsQ0FBQztRQUNyQixZQUFZLEdBQUcsaUJBQWlCLENBQUMsWUFBWSxFQUFFLGtCQUFrQixDQUFDLENBQUE7SUFDdEUsQ0FBQztJQUNELFlBQVksR0FBRyxZQUFZLEdBQUcsVUFBVSxDQUFDLEtBQUssQ0FBQTtJQUM5QyxNQUFNLENBQUMsWUFBWSxDQUFBO0FBQ3ZCLENBQUMsQ0FBQSJ9

View File

@ -3,6 +3,7 @@
"version": "1.0.0",
"description": "colors for beautylog",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"scripts": {
"test": "(npmts)"
},
@ -21,5 +22,13 @@
"bugs": {
"url": "https://gitlab.com/pushrocks/beautycolor/issues"
},
"homepage": "https://gitlab.com/pushrocks/beautycolor#README"
"homepage": "https://gitlab.com/pushrocks/beautycolor#README",
"dependencies": {
"ansi-256-colors": "^1.1.0",
"typings-global": "^1.0.14"
},
"devDependencies": {
"smartchai": "^1.0.1",
"typings-test": "^1.0.3"
}
}

1
test/test.d.ts vendored Normal file
View File

@ -0,0 +1 @@
import 'typings-test';

18
test/test.js Normal file
View File

@ -0,0 +1,18 @@
"use strict";
require("typings-test");
const beautycolor = require("../dist/index");
describe('beautycolor', function () {
it('should produce a blue font', function () {
console.log(beautycolor.coloredString('this is a blue font, no background', 'blue'));
});
it('should produce a red string with green background', function () {
console.log(beautycolor.coloredString('this is a red font with green background', 'red', 'green'));
});
it('should produce different font colors', function () {
console.log(beautycolor.coloredString('blue', 'blue'), beautycolor.coloredString('brown', 'brown'), beautycolor.coloredString('red', 'red'), beautycolor.coloredString('orange', 'orange'), beautycolor.coloredString('green', 'green'), beautycolor.coloredString('cyan', 'cyan'));
});
it('should produce different baclground colors', function () {
console.log(beautycolor.coloredString('blue', 'white', 'blue'), beautycolor.coloredString('brown', 'white', 'brown'), beautycolor.coloredString('red', 'white', 'red'), beautycolor.coloredString('orange', 'white', 'orange'), beautycolor.coloredString('green', 'white', 'green'), beautycolor.coloredString('cyan', 'white', 'cyan'));
});
});
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLHdCQUFxQjtBQUVyQiw2Q0FBNEM7QUFFNUMsUUFBUSxDQUFDLGFBQWEsRUFBRTtJQUNwQixFQUFFLENBQUMsNEJBQTRCLEVBQUU7UUFDN0IsT0FBTyxDQUFDLEdBQUcsQ0FBQyxXQUFXLENBQUMsYUFBYSxDQUFDLG9DQUFvQyxFQUFFLE1BQU0sQ0FBQyxDQUFDLENBQUE7SUFDeEYsQ0FBQyxDQUFDLENBQUE7SUFDRixFQUFFLENBQUMsbURBQW1ELEVBQUU7UUFDcEQsT0FBTyxDQUFDLEdBQUcsQ0FBQyxXQUFXLENBQUMsYUFBYSxDQUFDLDBDQUEwQyxFQUFFLEtBQUssRUFBQyxPQUFPLENBQUMsQ0FBQyxDQUFBO0lBQ3JHLENBQUMsQ0FBQyxDQUFBO0lBQ0YsRUFBRSxDQUFDLHNDQUFzQyxFQUFFO1FBQ3ZDLE9BQU8sQ0FBQyxHQUFHLENBQ1AsV0FBVyxDQUFDLGFBQWEsQ0FBQyxNQUFNLEVBQUMsTUFBTSxDQUFDLEVBQ3hDLFdBQVcsQ0FBQyxhQUFhLENBQUMsT0FBTyxFQUFDLE9BQU8sQ0FBQyxFQUMxQyxXQUFXLENBQUMsYUFBYSxDQUFDLEtBQUssRUFBQyxLQUFLLENBQUMsRUFDdEMsV0FBVyxDQUFDLGFBQWEsQ0FBQyxRQUFRLEVBQUMsUUFBUSxDQUFDLEVBQzVDLFdBQVcsQ0FBQyxhQUFhLENBQUMsT0FBTyxFQUFFLE9BQU8sQ0FBQyxFQUMzQyxXQUFXLENBQUMsYUFBYSxDQUFDLE1BQU0sRUFBRSxNQUFNLENBQUMsQ0FDNUMsQ0FBQTtJQUNMLENBQUMsQ0FBQyxDQUFBO0lBQ0YsRUFBRSxDQUFDLDRDQUE0QyxFQUFFO1FBQzdDLE9BQU8sQ0FBQyxHQUFHLENBQ1AsV0FBVyxDQUFDLGFBQWEsQ0FBQyxNQUFNLEVBQUUsT0FBTyxFQUFFLE1BQU0sQ0FBQyxFQUNsRCxXQUFXLENBQUMsYUFBYSxDQUFDLE9BQU8sRUFBRSxPQUFPLEVBQUUsT0FBTyxDQUFDLEVBQ3BELFdBQVcsQ0FBQyxhQUFhLENBQUMsS0FBSyxFQUFFLE9BQU8sRUFBRSxLQUFLLENBQUMsRUFDaEQsV0FBVyxDQUFDLGFBQWEsQ0FBQyxRQUFRLEVBQUUsT0FBTyxFQUFFLFFBQVEsQ0FBQyxFQUN0RCxXQUFXLENBQUMsYUFBYSxDQUFDLE9BQU8sRUFBRSxPQUFPLEVBQUUsT0FBTyxDQUFDLEVBQ3BELFdBQVcsQ0FBQyxhQUFhLENBQUMsTUFBTSxFQUFFLE9BQU8sRUFBRSxNQUFNLENBQUMsQ0FDckQsQ0FBQTtJQUNMLENBQUMsQ0FBQyxDQUFBO0FBQ04sQ0FBQyxDQUFDLENBQUEifQ==

32
test/test.ts Normal file
View File

@ -0,0 +1,32 @@
import 'typings-test'
import * as beautycolor from '../dist/index'
describe('beautycolor', function(){
it('should produce a blue font', function(){
console.log(beautycolor.coloredString('this is a blue font, no background', 'blue'))
})
it('should produce a red string with green background', function(){
console.log(beautycolor.coloredString('this is a red font with green background', 'red','green'))
})
it('should produce different font colors', function(){
console.log(
beautycolor.coloredString('blue','blue'),
beautycolor.coloredString('brown','brown'),
beautycolor.coloredString('red','red'),
beautycolor.coloredString('orange','orange'),
beautycolor.coloredString('green', 'green'),
beautycolor.coloredString('cyan', 'cyan')
)
})
it('should produce different baclground colors', function(){
console.log(
beautycolor.coloredString('blue', 'white', 'blue'),
beautycolor.coloredString('brown', 'white', 'brown'),
beautycolor.coloredString('red', 'white', 'red'),
beautycolor.coloredString('orange', 'white', 'orange'),
beautycolor.coloredString('green', 'white', 'green'),
beautycolor.coloredString('cyan', 'white', 'cyan')
)
})
})

74
ts/index.ts Normal file
View File

@ -0,0 +1,74 @@
import 'typings-global'
import * as ansiColors from 'ansi-256-colors'
/**
* all the color names that are available for proper xterm translation
*/
export type TColorName = 'black' |
'blue' |
'brown' |
'cyan' |
'green' |
'red' |
'orange' |
'white'
export interface IRGB {
r: number,
b: number,
g: number
}
/**
* the color translator function
*/
let colorTranslator = (colorArg: TColorName): IRGB => {
switch (colorArg) {
case 'blue':
return {r: 0, g: 1, b: 5}
case 'cyan':
return {r: 0,g: 4, b: 4}
case 'green':
return {r: 2, g: 5, b: 0}
case 'red':
return {r: 5, g: 0, b: 0}
case 'orange':
return {r: 5, g: 3, b: 0}
case 'brown':
return {r: 1, g: 0 , b: 0}
case 'black':
return {r: 0, g: 0, b: 0}
case 'white':
return {r: 5, g: 5, b: 5}
default:
return {r: 5, g: 5, b: 5}
}
}
/**
* colors the font of a string
*/
let coloredFont = (stringArg: string, colorArg: TColorName) => {
let rgbCode: IRGB = colorTranslator(colorArg)
return ansiColors.fg.getRgb(rgbCode.r, rgbCode.g, rgbCode.b) + stringArg
}
/**
* colors the back of a string
*/
let coloredBackground = (stringArg: string, colorArg: TColorName) => {
let rgbCode = colorTranslator(colorArg)
return ansiColors.bg.getRgb(rgbCode.r, rgbCode.g, rgbCode.b) + stringArg
}
/**
* color a string with xterm
*/
export let coloredString = (stringArg: string, colorFontArg: TColorName, colorBackgroundArg?: TColorName): string => {
let returnString = coloredFont(stringArg, colorFontArg)
if (colorBackgroundArg) {
returnString = coloredBackground(returnString, colorBackgroundArg)
}
returnString = returnString + ansiColors.reset
return returnString
}