Permalink
Browse files

[lib] second

  • Loading branch information...
1 parent b113f8f commit f8cdd30a56e61273e7964b818118f03b9b22a041 Paolo Fragomeni committed Apr 20, 2012
Showing with 43 additions and 21 deletions.
  1. +2 −1 .gitignore
  2. +7 −3 README.md
  3. +12 −0 bin/dir
  4. +22 −17 dir.js
  5. BIN screenshot.png
View
@@ -12,4 +12,5 @@ logs
results
node_modules
-npm-debug.log
+npm-debug.log
+.DS_Store
View
@@ -1,5 +1,3 @@
-node-dir
-========
# Synopsis
An interactive representation of an object for the CLI similar to that of console.dir() in webkit.
@@ -10,6 +8,8 @@ Why? Sometimes you have a lot of data that gets dumped to the screen. It's hard
# Usage
First, require the module. Then use `console.dir(somecode)` in your program and then use `tab` or `shift+tab` to cycle through the object's members. You can hit `space`, `return` or `enter` to expand a member. See the example below.
+There is a CLI version too if you want to use it. `npm install dir -g`.
+
```js
console.dir = require('../dir');
@@ -32,4 +32,8 @@ var stuff = {
console.dir(stuff);
-```
+```
+
+The output looks something like this.
+
+<img/>
View
12 bin/dir
@@ -0,0 +1,12 @@
+#!/usr/bin/env node
+
+var fs = require('fs');
+console.dir = require('../dir');
+
+var filename = process.argv[2];
+
+if (!filename) {
+ throw new Error('Requires a filename');
+}
+
+console.dir(JSON.parse(fs.readFileSync(filename)));
View
39 dir.js
@@ -7,16 +7,8 @@ var stdin = process.openStdin();
tty.setRawMode(true);
var tabSize = 2;
-var filename;
var displayed = 0;
-if (process.argv[0] === 'node') {
- filename = process.argv[2];
-}
-else {
- filename = process.argv[1];
-}
-
var meta = [], map = [0];
var selection = 1;
@@ -31,9 +23,11 @@ var write = function write(s) {
//
var up = function up(i) {
+var maxLineLen = process.stdout.getWindowSize()[0]/2;
+
if (i > 0) {
while(i--) {
- write(ws(80, true) + '\033[1A\r');
+ write(ws(maxLineLen, true) + '\033[1A\r');
}
}
else {
@@ -96,14 +90,25 @@ var constructMeta = function constructMeta(parentType, depth, node, itemPrefix)
switch(type) {
case 'string':
+
+ meta.push({
+ description: itemPrefix + '\033[31m"' + node + '"\033[0m',
+ expanded: false,
+ displayed: first,
+ type: type,
+ depth: depth,
+ index: seed
+ });
+
+ break;
case 'number':
case 'boolean':
case 'undefined':
case 'regexp':
case 'null':
meta.push({
- description: itemPrefix + node,
+ description: itemPrefix + '\033[31m' + node + '\033[0m',
expanded: false,
displayed: first,
type: type,
@@ -127,7 +132,7 @@ var constructMeta = function constructMeta(parentType, depth, node, itemPrefix)
case 'array':
meta.push({
- description: itemPrefix + 'Array[' + node.length + ']',
+ description: itemPrefix + '\033[36mArray[\033[0m' + node.length + '\033[36m]\033[0m',
expanded: false,
displayed: first,
type: type,
@@ -149,7 +154,7 @@ var constructMeta = function constructMeta(parentType, depth, node, itemPrefix)
case 'object':
meta.push({
- description: itemPrefix + 'Object',
+ description: itemPrefix + '\033[36mObject\033[0m',
expanded: false,
displayed: first,
type: type,
@@ -184,13 +189,13 @@ var renderMeta = function renderMeta() {
if (displayed === selection) {
write('\033[30;47m');
+ write(meta[i].description.replace(/\033\[[0-9;]*m/g, '') + '\n');
+ write('\033[0m');
+ }
+ else {
+ write(meta[i].description + '\n');
}
- write(meta[i].description + '\n');
-
- if (displayed === selection) {
- write('\033[0m');
- }
}
}
};
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f8cdd30

Please sign in to comment.