Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

setting linenumber printing

- can be set in .cardinalrx
- if turned on that can be turned off via --nonum
  • Loading branch information...
commit 6ef47b79db2c3c5ce23914c404a4562ee292bd7d 1 parent d0e6051
@thlorenz authored
Showing with 95 additions and 23 deletions.
  1. +35 −2 bin/cdl.js
  2. +24 −16 settings.js
  3. +36 −5 test/settings.js
View
37 bin/cdl.js
@@ -4,12 +4,28 @@ var cardinal = require('..')
, settings = require('../settings')
, args = process.argv
, theme = settings.resolveTheme()
+ , opts = settings.getSettings()
, 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 () {
try {
- highlighted = cardinal.highlightFileSync(args[2], { theme: theme });
+ highlighted = cardinal.highlightFileSync(args[2], opts);
console.log(highlighted);
} catch (e) {
console.trace();
@@ -20,17 +36,34 @@ function highlightFile () {
// E.g., "cardinal myfile.js"
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
var stdin = process.stdin
, stdout = process.stdout;
+// line numbers don't make sense when we are printing line by line
+opts.linenos = false;
+
stdin.resume();
stdin.setEncoding('utf-8');
stdin
.on('data', function (chunk) {
chunk.split('\n').forEach(function (line) {
try {
- stdout.write(cardinal.highlight(line, theme) + '\n');
+ stdout.write(cardinal.highlight(line, opts) + '\n');
} catch (e) {
// line doesn't represent a valid js snippet and therefore cannot be parsed -> just print as is
stdout.write(line + '\n');
View
40 settings.js
@@ -2,42 +2,50 @@ var path = require('path')
, util = require('util')
, fs = require('fs')
, utl = require('./utl')
- , home = process.env.HOME;
-
-// 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;
+ , home = process.env.HOME
+ , settings;
+function getSettings (home_) {
+ if (settings) return settings;
try {
settingsJson = fs.readFileSync(path.join(home_ || home, '.cardinalrc'), 'utf-8');
} catch (_) {
// no .cardinalrc found - not a problem
return undefined;
}
-
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
themePath = utl.isPath(settings.theme) ? settings.theme : path.join(__dirname, 'themes', settings.theme);
return require(themePath);
} catch (e) {
- // Have a .cardinalrc, but something about it is wrong - warn the user
- // Either we couldn't parse the contained JSON, or the specified theme path is invalid
+ // Specified theme path is invalid
console.error(e);
return undefined;
}
}
module.exports = {
- resolveTheme: resolveTheme
+ resolveTheme: resolveTheme
+ , getSettings: getSettings
};
View
41 test/settings.js
@@ -1,14 +1,20 @@
'use strict';
/*jshint asi: true*/
-var test = require('tap').test
- , path = require('path')
- , fs = require('fs')
- , settings = require('../settings')
- , existsSync = fs.existsSync || path.existsSync
+var test = require('tap').test
+ , path = require('path')
+ , fs = require('fs')
, hideSemicolonsTheme = require('../themes/hide-semicolons')
, home = path.join(__dirname, 'fixtures', 'home')
, 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) {
fs.writeFileSync(rcpath, JSON.stringify(config), 'utf-8')
@@ -25,22 +31,47 @@ function resolveTheme (config) {
return result;
}
+function getSettings (config) {
+ writerc(config)
+ var result = settings.getSettings(home)
+ removerc()
+ return result;
+}
+
if (!existsSync(home)) fs.mkdirSync(home);
test('no .cardinalrc in home', function (t) {
+ setup()
var theme = settings.resolveTheme(home)
t.equals(theme, undefined, 'resolves no theme')
t.end()
})
test('.cardinalrc with theme "hide-semicolons" in home', function (t) {
+ setup()
var theme = resolveTheme({ theme: "hide-semicolons" })
t.deepEquals(theme, hideSemicolonsTheme, 'resolves hide-semicolons theme')
t.end()
})
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') })
t.deepEquals(theme, hideSemicolonsTheme, 'resolves hide-semicolons theme')
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()
+})
+
Please sign in to comment.
Something went wrong with that request. Please try again.