Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

piping support #22

Merged
merged 1 commit into from

2 participants

@TooTallNate

I swear, you owe me a beer or something for this one ;)

$ echo '{ foo: "bar" }' | cdir

^ pure pwnage

Closes #3.

note: currently crashes on OS X (assertion error) due to joyent/node#3072, so go make noise on that issue to get it fixed.

@TooTallNate TooTallNate piping support
 $ echo '{ foo: "bar" }' | cdir

 ^ pure pwnage

Closes #3.
6990853
@hij1nx hij1nx merged commit 8a55b70 into asyncly:master
@hij1nx
Owner

of course!! at nodeconf! =D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 16, 2012
  1. @TooTallNate

    piping support

    TooTallNate authored
     $ echo '{ foo: "bar" }' | cdir
    
     ^ pure pwnage
    
    Closes #3.
This page is out of date. Refresh to see the latest.
Showing with 38 additions and 13 deletions.
  1. +26 −4 bin/cdir
  2. +9 −9 cdir.js
  3. +3 −0  package.json
View
30 bin/cdir
@@ -1,12 +1,34 @@
#!/usr/bin/env node
var fs = require('fs');
-console.dir = require('../cdir');
+var vm = require('vm');
var filename = process.argv[2];
+var subject = '';
-if (!filename) {
- throw new Error('Requires a filename');
+if (filename) {
+ // read the contents of the given filename
+ fs.readFile(filename, 'utf8', function(err, file) {
+ if (err) throw err;
+ subject = file;
+ cdir();
+ });
+} else {
+ // read the contents of stdin
+ process.stdin.setEncoding('utf8');
+ process.stdin.on('data', function(data) {
+ subject += data;
+ });
+ process.stdin.on('end', function() {
+ cdir();
+ });
+ process.stdin.resume();
+ filename = '[stdin]';
}
-console.dir(JSON.parse(fs.readFileSync(filename)));
+function cdir() {
+ var sandbox = {};
+ subject = vm.runInNewContext('(' + subject + ')', sandbox, filename);
+ console.dir = require('../cdir');
+ console.dir(subject);
+}
View
18 cdir.js
@@ -1,13 +1,15 @@
var tty = require('tty');
+var ttys = require('ttys');
var rl = require('readline');
-var stdin;
+var stdin = ttys.stdin;
+var stdout = ttys.stdout;
//
// shorthand write to stdout
//
var write = function write (s) {
- process.stdout.write(s);
+ stdout.write(s);
}
//
@@ -132,7 +134,6 @@ if (typeof JSON.decycle !== 'function') {
//
module.exports = function dir (obj, options) {
- var stdin;
var displayed = 0;
var copybuffer = 0;
@@ -170,7 +171,7 @@ module.exports = function dir (obj, options) {
var buffer = '';
var description = ws(indent, true);
- var maxWidth = process.stdout.getWindowSize()[0] - indent - 6;
+ var maxWidth = stdout.getWindowSize()[0] - indent - 6;
for (var i = 0, cpos = 0, l = str.length; i < l; i++, cpos++) {
@@ -217,8 +218,8 @@ module.exports = function dir (obj, options) {
var truncatedNode = '0';
var truncated = false;
- if (node.length > process.stdout.getWindowSize()[0] - extLen) {
- truncatedNode = '' + '\033[31m"' + node.substr(0, process.stdout.getWindowSize()[0]/2) + '..."\033[0m';
+ if (node.length > stdout.getWindowSize()[0] - extLen) {
+ truncatedNode = '' + '\033[31m"' + node.substr(0, stdout.getWindowSize()[0]/2) + '..."\033[0m';
truncated = true;
}
else {
@@ -444,10 +445,10 @@ module.exports = function dir (obj, options) {
// include that before the prompt as the default.
//
if (lastsearch !== '') {
- process.stdout.write('(' + lastsearch + ') /');
+ stdout.write('(' + lastsearch + ') /');
}
else {
- process.stdout.write('/');
+ stdout.write('/');
}
}
else if (searchmode === true && typeof key !== 'undefined' && key.name === 'backspace') {
@@ -652,7 +653,6 @@ module.exports = function dir (obj, options) {
setRawMode.call(stdin, mode);
}
- stdin = process.stdin;
stdin.resume();
var wasRaw;
View
3  package.json
@@ -21,6 +21,9 @@
"bin": {
"cdir": "./bin/cdir"
},
+ "dependencies": {
+ "ttys": "*"
+ },
"engines" : ["node >= 0.6.x"],
"main" : "./cdir",
"scripts": {
Something went wrong with that request. Please try again.