Skip to content

Commit

Permalink
setting linenumber printing
Browse files Browse the repository at this point in the history
- can be set in .cardinalrx
- if turned on that can be turned off via --nonum
  • Loading branch information
thlorenz committed Nov 3, 2012
1 parent d0e6051 commit 6ef47b7
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 23 deletions.
37 changes: 35 additions & 2 deletions bin/cdl.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,12 +4,28 @@ var cardinal = require('..')
, settings = require('../settings') , settings = require('../settings')
, args = process.argv , args = process.argv
, theme = settings.resolveTheme() , theme = settings.resolveTheme()
, opts = settings.getSettings()
, highlighted , highlighted
; ;


opts.theme = theme;

function usage() {
var msg = [
'Usage: cdl <filename.js> [options]'
, ''
, 'Options (~/.cardinalrc overrides):'
, ' --nonum: turn off line printing'
, ''
, 'Unix Pipe Example: cat filename.js | grep console | cdl'
, ''
].join('\n');
console.log(msg);
}

function highlightFile () { function highlightFile () {
try { try {
highlighted = cardinal.highlightFileSync(args[2], { theme: theme }); highlighted = cardinal.highlightFileSync(args[2], opts);
console.log(highlighted); console.log(highlighted);
} catch (e) { } catch (e) {
console.trace(); console.trace();
Expand All @@ -20,17 +36,34 @@ function highlightFile () {
// E.g., "cardinal myfile.js" // E.g., "cardinal myfile.js"
if (args.length === 3) return highlightFile(); if (args.length === 3) return highlightFile();


var opt = args[3];

// E.g., "cardinal myfile.js --nonum"
if (opt && opt.indexOf('--') === 0 ) {
if ((/^--(nonum|noline)/i).test(opt)) opts.linenos = false;
else {
usage();
return console.error('Unknown option: ', opt);
}

return highlightFile();
}


// UNIX pipes e.g., "cat myfile.js | grep console | cardinal // UNIX pipes e.g., "cat myfile.js | grep console | cardinal
var stdin = process.stdin var stdin = process.stdin
, stdout = process.stdout; , stdout = process.stdout;


// line numbers don't make sense when we are printing line by line
opts.linenos = false;

stdin.resume(); stdin.resume();
stdin.setEncoding('utf-8'); stdin.setEncoding('utf-8');
stdin stdin
.on('data', function (chunk) { .on('data', function (chunk) {
chunk.split('\n').forEach(function (line) { chunk.split('\n').forEach(function (line) {
try { try {
stdout.write(cardinal.highlight(line, theme) + '\n'); stdout.write(cardinal.highlight(line, opts) + '\n');
} catch (e) { } catch (e) {
// line doesn't represent a valid js snippet and therefore cannot be parsed -> just print as is // line doesn't represent a valid js snippet and therefore cannot be parsed -> just print as is
stdout.write(line + '\n'); stdout.write(line + '\n');
Expand Down
40 changes: 24 additions & 16 deletions settings.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,42 +2,50 @@ var path = require('path')
, util = require('util') , util = require('util')
, fs = require('fs') , fs = require('fs')
, utl = require('./utl') , utl = require('./utl')
, home = process.env.HOME; , home = process.env.HOME

, settings;
// home_ mainly to be used during tests
// Resolves the preferred theme from the .cardinalrc found in the HOME directory
// If it couldn't be resolved, undefined is returned
function resolveTheme (home_) {
var settingsJson
, settings
, themePath;


function getSettings (home_) {
if (settings) return settings;
try { try {
settingsJson = fs.readFileSync(path.join(home_ || home, '.cardinalrc'), 'utf-8'); settingsJson = fs.readFileSync(path.join(home_ || home, '.cardinalrc'), 'utf-8');
} catch (_) { } catch (_) {
// no .cardinalrc found - not a problem // no .cardinalrc found - not a problem
return undefined; return undefined;
} }

try { try {
return JSON.parse(settingsJson);
} catch (e) {
// Have a .cardinalrc, but something about it is wrong - warn the user
// Coudn't parse the contained JSON
console.error(e);
return undefined;
}
}


settings = JSON.parse(settingsJson); // home_ mainly to be used during tests
// Resolves the preferred theme from the .cardinalrc found in the HOME directory
// If it couldn't be resolved, undefined is returned
function resolveTheme (home_) {
var themePath
, settings = getSettings(home_);

if (!settings || !settings.theme) return undefined;


if (!settings.theme) return undefined; try {

// allow specifying just the name of a built-in theme or a full path to a custom theme // allow specifying just the name of a built-in theme or a full path to a custom theme
themePath = utl.isPath(settings.theme) ? settings.theme : path.join(__dirname, 'themes', settings.theme); themePath = utl.isPath(settings.theme) ? settings.theme : path.join(__dirname, 'themes', settings.theme);


return require(themePath); return require(themePath);
} catch (e) { } catch (e) {
// Have a .cardinalrc, but something about it is wrong - warn the user // Specified theme path is invalid
// Either we couldn't parse the contained JSON, or the specified theme path is invalid
console.error(e); console.error(e);
return undefined; return undefined;
} }
} }


module.exports = { module.exports = {
resolveTheme: resolveTheme resolveTheme: resolveTheme
, getSettings: getSettings
}; };


41 changes: 36 additions & 5 deletions test/settings.js
Original file line number Original file line Diff line number Diff line change
@@ -1,14 +1,20 @@
'use strict'; 'use strict';
/*jshint asi: true*/ /*jshint asi: true*/


var test = require('tap').test var test = require('tap').test
, path = require('path') , path = require('path')
, fs = require('fs') , fs = require('fs')
, settings = require('../settings')
, existsSync = fs.existsSync || path.existsSync
, hideSemicolonsTheme = require('../themes/hide-semicolons') , hideSemicolonsTheme = require('../themes/hide-semicolons')
, home = path.join(__dirname, 'fixtures', 'home') , home = path.join(__dirname, 'fixtures', 'home')
, rcpath = path.join(home, '.cardinalrc') , rcpath = path.join(home, '.cardinalrc')
, existsSync = fs.existsSync || path.existsSync
, settingsResolve = require.resolve('../settings')
, settings

function setup () {
delete require.cache[settingsResolve]
settings = require(settingsResolve)
}


function writerc(config) { function writerc(config) {
fs.writeFileSync(rcpath, JSON.stringify(config), 'utf-8') fs.writeFileSync(rcpath, JSON.stringify(config), 'utf-8')
Expand All @@ -25,22 +31,47 @@ function resolveTheme (config) {
return result; return result;
} }


function getSettings (config) {
writerc(config)
var result = settings.getSettings(home)
removerc()
return result;
}

if (!existsSync(home)) fs.mkdirSync(home); if (!existsSync(home)) fs.mkdirSync(home);


test('no .cardinalrc in home', function (t) { test('no .cardinalrc in home', function (t) {
setup()
var theme = settings.resolveTheme(home) var theme = settings.resolveTheme(home)
t.equals(theme, undefined, 'resolves no theme') t.equals(theme, undefined, 'resolves no theme')
t.end() t.end()
}) })


test('.cardinalrc with theme "hide-semicolons" in home', function (t) { test('.cardinalrc with theme "hide-semicolons" in home', function (t) {
setup()
var theme = resolveTheme({ theme: "hide-semicolons" }) var theme = resolveTheme({ theme: "hide-semicolons" })
t.deepEquals(theme, hideSemicolonsTheme, 'resolves hide-semicolons theme') t.deepEquals(theme, hideSemicolonsTheme, 'resolves hide-semicolons theme')
t.end() t.end()
}) })


test('.cardinalrc with full path to "hide-semicolons.js" in home', function (t) { test('.cardinalrc with full path to "hide-semicolons.js" in home', function (t) {
setup()
var theme = resolveTheme({ theme: path.join(__dirname, '..', 'themes', 'hide-semicolons.js') }) var theme = resolveTheme({ theme: path.join(__dirname, '..', 'themes', 'hide-semicolons.js') })
t.deepEquals(theme, hideSemicolonsTheme, 'resolves hide-semicolons theme') t.deepEquals(theme, hideSemicolonsTheme, 'resolves hide-semicolons theme')
t.end() t.end()
}) })

test('.cardinalrc with linenos: true', function (t) {
setup()
var opts = { linenos: true }
t.deepEquals(getSettings(opts), opts)
t.end()
})

test('.cardinalrc with linenos: true and theme', function (t) {
setup()
var opts = { linenos: true, theme: 'some theme' }
t.deepEquals(getSettings(opts), opts)
t.end()
})

0 comments on commit 6ef47b7

Please sign in to comment.