Skip to content

Commit

Permalink
Color themes
Browse files Browse the repository at this point in the history
  • Loading branch information
igorshubovych committed Mar 5, 2016
1 parent cf1c8f2 commit a898fb6
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 26 deletions.
15 changes: 11 additions & 4 deletions bin/tldr
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/usr/bin/env node

var tldr = require('../lib/tldr');
var pkg = require('../package');
var program = require('commander');
var pkg = require('../package');
var tldr = require('../lib/tldr');
var config = require('../lib/config');
var platform = require('../lib/platform');

program
.version(pkg.version)
Expand All @@ -21,6 +23,7 @@ program
.option('--linux', 'Override the operating system with Linux')
.option('--osx', 'Override the operating system with OSX')
.option('--sunos', 'Override the operating system with SunOS')
.option('-t, --theme [theme]', 'Color theme (bw, bw-simple, original)')
//
// CACHE MANAGEMENT
//
Expand Down Expand Up @@ -62,14 +65,18 @@ if (program.sunos) {
program.os = 'sunos';
}


if (program.os) {
var config = require('../lib/config');
var platform = require('../lib/platform');
if (platform.isSupported(program.os)) {
config.get().platform = program.os;
}
}

if (program.theme) {
// TODO: Validate theme
config.get().theme = program.theme;
}

if (program.list) {
tldr.list(program.singleColumn);
} else if (program.listAll) {
Expand Down
33 changes: 32 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,36 @@
"command-background": "black",
"command-foreground": "red",
"command-token": "white"
}
},
"themes": {
"bw": {
"name": "bold, underline",
"description": "bold",
"exampleDescription": "",
"exampleCode": "",
"exampleArgument": "underline"
},
"plain": {
"name": "",
"description": "",
"exampleDescription": "",
"exampleCode": "",
"exampleArgument": ""
},
"original": {
"name": "bold",
"description": "",
"exampleDescription": "green",
"exampleCode": "red",
"exampleArgument": ""
},
"simple": {
"name": "bold,cyan",
"description": "",
"exampleDescription": "green",
"exampleCode": "",
"exampleArgument": "dim"
}
},
"theme": "bw"
}
25 changes: 9 additions & 16 deletions lib/render.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
/*eslint-disable no-unused-vars */
var colors = require('colors');
/*eslint-disable no-unused-vars */
var config = require('./config');

var TEXT = config.get().colors['text'];
var CMD_BG = config.get().colors['command-background'] + 'BG';
var CMD_FG = config.get().colors['command-foreground'];
var CMD_TOK = config.get().colors['command-token'];
var theme = require('./theme');

exports.toANSI = function(page) {
var output = [];

output.push(' ' + page.name);
output.push(' ' + page.description.replace(/\n/g, '\n '));
output.push(' ' + theme.renderName(page.name));
output.push('');
output.push(' ' + theme.renderDescription(page.description.replace(/\n/g, '\n ')));
output.push('');

page.examples.forEach(function(example) {
output.push(' - ' + example.description[TEXT]);
output.push(' ' + highlight(example.code));
output.push(theme.renderExampleDescription(' - ' + example.description));
output.push(highlight(example.code));
output.push('');
});

Expand All @@ -33,10 +26,10 @@ exports.toANSI = function(page) {
function highlight(code) {
var parts = code.split(/\{\{(.*?)\}\}/);
// every second part is a token
return ' ' + parts.reduce(function(memo, item, i) {
return ' ' + parts.reduce(function(memo, item, i) {
if (i % 2) {
return memo + item[CMD_BG][CMD_TOK];
return memo + theme.renderExampleArgument(item);
}
return memo + item[CMD_BG][CMD_FG];
return memo + theme.renderExampleCode(item);
}, '');
}
55 changes: 55 additions & 0 deletions lib/theme.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
var _ = require('lodash');
var config = require('./config');

var theme = null;

function getTheme() {
if (!theme) {
theme = loadTheme();
}
return theme;
}

function loadTheme() {
var themes = config.get().themes;
var currentTheme = config.get().theme;
return themes[currentTheme];
}

function buildStylingFunction(styles) {
if (_.isEmpty(styles)) {
return _.identity;
}
var stylingFunction = require('chalk');
var stylesPath = styles.replace(/,\s*/, '.');
return _.get(stylingFunction, stylesPath);
}

function renderPart(partName, text) {
// var theme = theme;
var styles = getTheme()[partName];
var stylingFunction = buildStylingFunction(styles);
return stylingFunction(text);
}

module.exports = {
renderName: function(text) {
return renderPart('name', text);
},

renderDescription: function(text) {
return renderPart('description', text);
},

renderExampleDescription: function(text) {
return renderPart('exampleDescription', text);
},

renderExampleCode: function(text) {
return renderPart('exampleCode', text);
},

renderExampleArgument: function(text) {
return renderPart('exampleArgument', text);
}
};
17 changes: 12 additions & 5 deletions test/render.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
var should = require('should');
/*eslint-disable no-unused-vars */
var render = require('../lib/render');
var config = require('../lib/config');

describe('Render', function() {

beforeEach(function() {
config.get().theme = 'plain';
});

it('surrounds the output with blank lines', function() {
var text = render.toANSI({
name: 'tar',
Expand All @@ -21,7 +26,8 @@ describe('Render', function() {
description: 'archive utility',
examples: []
});
text.should.containEql(' tar\n');
text.should.containEql('tar');
text.should.containEql('archive utility');
});

it('contains the description name', function() {
Expand All @@ -30,7 +36,8 @@ describe('Render', function() {
description: 'archive utility\nwith support for compression',
examples: []
});
text.should.containEql(' archive utility\n with support for compression\n');
text.should.containEql('archive utility');
text.should.containEql('with support for compression');
});

it('highlights replaceable {{tokens}}', function() {
Expand All @@ -42,9 +49,9 @@ describe('Render', function() {
code: 'hello {{token}} bye'
}]
});
text.should.containEql('hello '.blackBG.red);
text.should.containEql('token'.blackBG.white);
text.should.containEql(' bye'.blackBG.red);
text.should.containEql('hello ');
text.should.containEql('token');
text.should.containEql(' bye');
});

it('should correctly render see also section', function() {
Expand Down

0 comments on commit a898fb6

Please sign in to comment.