2017-01-21 17:10:27 +00:00
|
|
|
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' |
|
2017-01-21 17:26:22 +00:00
|
|
|
'black' |
|
2017-01-21 17:10:27 +00:00
|
|
|
'blue' |
|
|
|
|
'brown' |
|
|
|
|
'cyan' |
|
|
|
|
'green' |
|
|
|
|
'orange' |
|
2017-01-21 17:26:22 +00:00
|
|
|
'pink' |
|
|
|
|
'red' |
|
2017-01-21 17:10:27 +00:00
|
|
|
'white'
|
|
|
|
|
|
|
|
export interface IRGB {
|
|
|
|
r: number,
|
|
|
|
b: number,
|
|
|
|
g: number
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the color translator function
|
|
|
|
*/
|
|
|
|
let colorTranslator = (colorArg: TColorName): IRGB => {
|
|
|
|
switch (colorArg) {
|
2017-01-21 17:26:22 +00:00
|
|
|
case 'black':
|
|
|
|
return {r: 0, g: 0, b: 0}
|
2017-01-21 17:10:27 +00:00
|
|
|
case 'blue':
|
|
|
|
return {r: 0, g: 1, b: 5}
|
2017-01-21 17:26:22 +00:00
|
|
|
case 'brown':
|
|
|
|
return {r: 1, g: 0 , b: 0}
|
2017-01-21 17:10:27 +00:00
|
|
|
case 'cyan':
|
2017-01-21 18:46:51 +00:00
|
|
|
return {r: 2,g: 4, b: 4}
|
2017-01-21 17:10:27 +00:00
|
|
|
case 'green':
|
2017-01-21 19:27:25 +00:00
|
|
|
return {r: 2, g: 4, b: 1}
|
2017-01-21 17:10:27 +00:00
|
|
|
case 'orange':
|
2017-01-21 19:27:25 +00:00
|
|
|
return {r: 5, g: 3, b: 1}
|
2017-01-21 17:26:22 +00:00
|
|
|
case 'pink':
|
2017-01-21 19:27:25 +00:00
|
|
|
return {r: 3, g: 2, b: 4}
|
2017-01-21 17:26:22 +00:00
|
|
|
case 'red':
|
|
|
|
return {r: 5, g: 0, b: 0}
|
2017-01-21 17:10:27 +00:00
|
|
|
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
|
|
|
|
}
|