Skip to content

Commit

Permalink
feat: Added 141 X11 web colors, added test for background color
Browse files Browse the repository at this point in the history
  • Loading branch information
lisaychuang committed Aug 2, 2018
1 parent 86b47a5 commit e8a3c02
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 11 deletions.
Binary file modified .vs/slnx.sqlite
Binary file not shown.
5 changes: 0 additions & 5 deletions src/color.ts

This file was deleted.

145 changes: 145 additions & 0 deletions src/colors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
const X11_COLORS = [
'aliceBlue',
'antiqueWhite',
'aqua',
'aquamarine',
'azure',
'beige',
'bisque',
'black',
'blanchedAlmond',
'blue',
'blueViolet',
'brown',
'burlyWood',
'cadetBlue',
'chartreuse',
'chocolate',
'coral',
'cornflowerBlue',
'cornsilk',
'crimson',
'cyan',
'darkBlue',
'darkCyan',
'darkGoldenrod',
'darkGray',
'darkGreen',
'darkKhaki',
'darkMagenta',
'darkOliveGreen',
'darkOrange',
'darkOrchid',
'darkRed',
'darkSalmon',
'darkSeaGreen',
'darkSlateBlue',
'darkSlateGray',
'darkTurquoise',
'darkViolet',
'deepPink',
'deepSkyBlue',
'dimGray',
'dodgerBlue',
'fireBrick',
'floralWhite',
'forestGreen',
'fuchsia',
'gainsboro',
'ghostWhite',
'gold',
'goldenrod',
'gray',
'green',
'greenYellow',
'honeydew',
'hotPink',
'indianRed',
'indigo',
'ivory',
'khaki',
'lavender',
'lavenderBlush',
'lawnGreen',
'lemonChiffon',
'lightBlue',
'lightCoral',
'lightCyan',
'lightGoldenrodYellow',
'lightGray',
'lightGreen',
'lightPink',
'lightSalmon',
'lightSeaGreen',
'lightSkyBlue',
'lightSlateGray',
'lightSteelBlue',
'lightYellow',
'lime',
'limeGreen',
'linen',
'magenta',
'maroon',
'mediumAquamarine',
'mediumBlue',
'mediumOrchid',
'mediumPurple',
'mediumSeaGreen',
'mediumSlateBlue',
'mediumSpringGreen',
'mediumTurquoise',
'mediumVioletRed',
'midnightBlue',
'mintCream',
'mistyRose',
'moccasin',
'navajoWhite',
'navy',
'oldLace',
'olive',
'oliveDrab',
'orange',
'orangeRed',
'orchid',
'paleGoldenrod',
'paleGreen',
'paleTurquoise',
'paleVioletRed',
'papayaWhip',
'peachPuff',
'peru',
'pink',
'plum',
'powderBlue',
'purple',
'rebeccaPurple',
'red',
'rosyBrown',
'royalBlue',
'saddleBrown',
'salmon',
'sandyBrown',
'seaGreen',
'seashell',
'sienna',
'silver',
'skyBlue',
'slateBlue',
'slateGray',
'snow',
'springGreen',
'steelBlue',
'tan',
'teal',
'thistle',
'tomato',
'turquoise',
'violet',
'wheat',
'white',
'whiteSmoke',
'yellow',
'yellowGreen'
];

export default X11_COLORS;
12 changes: 6 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import COLORS from './color';
import COLOR_STYLES from './styles';

// Log levels (lower number are more severe)
const LVL_ERROR = 1;
Expand Down Expand Up @@ -27,7 +27,7 @@ export interface Printer {
}

export type ILoggerInstance = Printer & {
[K in keyof typeof COLORS]: ILoggerInstance
[K in keyof typeof COLOR_STYLES]: ILoggerInstance
} & {
txt(str: string): ILoggerInstance
};
Expand Down Expand Up @@ -97,7 +97,7 @@ class Logger {
}

/**
* According to the styles in './color.ts', set up
* According to the COLOR_STYLES in './style.ts', set up
* a property for each, kind of like
* ```ts
* {
Expand All @@ -110,15 +110,15 @@ class Logger {
*/
private setupStyles() {
// Loop over each style name (i.e. "red")
for (let c in COLORS) {
for (let c in COLOR_STYLES) {
// Make sure the property is on the instance, not the prototype
if (COLORS.hasOwnProperty(c)) {
if (COLOR_STYLES.hasOwnProperty(c)) {
// Define a new property on this, of name c (i.e. "red")
// that is getter-based (instead of value based)
const self = this;
Object.defineProperty(this, c, {
get() {
const cStyle = COLORS[c as keyof typeof COLORS]; // i.e. ('color: red;')
const cStyle = COLOR_STYLES[c as keyof typeof COLOR_STYLES]; // i.e. ('color: red;')
self.stylesInProgress.push(cStyle);
return this;
}
Expand Down
20 changes: 20 additions & 0 deletions src/styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import X11_COLORS from './colors';

interface StyleObj { [k: string]: string; };

const COLOR_STYLES: StyleObj = { };
for (let c of X11_COLORS) {
// i.e. "Red"
COLOR_STYLES[c] = `color: ${c};`;
// i.e. "bgRed"
COLOR_STYLES[`bg${c[0].toUpperCase()}${c.substring(1)}`] = `background-color: ${c};`;
}

// const fontStyles: StyleObj = {
// bold: 'font-weight: 500;'
// };

export default {
...COLOR_STYLES,
// ...fontStyles
};
21 changes: 21 additions & 0 deletions test/colors-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,27 @@ QUnit.test('Logger in red works', assert => {
);
});

QUnit.test('Logger in blue background works', assert => {
const printer = makeTestPrinter();
const logger = new Logger(4, printer);
assert.ok(logger.bgBlue instanceof Logger, 'logger.bgBlue is a logger');
assert.equal(
typeof logger.bgBlue.log,
'function',
'logger.bgBlue.log is a function'
);
logger.bgBlue.log('my background is blue');

logCountAssert(
{
message: 'after logging with blue background, we should see one log message',
assert,
printer
},
{ e: 0, w: 0, l: 1, d: 0 }
);
});

// logger.red().log('foo')

// logger
Expand Down

0 comments on commit e8a3c02

Please sign in to comment.