Skip to content

Commit

Permalink
[lib] better string handling, expand functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Paolo Fragomeni committed Apr 20, 2012
1 parent e5210ab commit 68e16ab
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 11 deletions.
83 changes: 74 additions & 9 deletions cdir.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,30 +80,85 @@ var getType = function getType(o) {
//
var indent = 0;
var seed = -1;
var constructMeta = function constructMeta(parentType, depth, node, itemPrefix) {
var constructMeta = function constructMeta(parentType, depth, node, itemPrefix, dontPreface) {

itemPrefix = itemPrefix || '';

var first = meta.length === 0;
var type = getType(node);
seed++;

switch(type) {
case 'string':
function addStrData(node) {
indent++;
depth++;

var buffer = '';
var description = ws(indent, true);
var maxWidth = process.stdout.getWindowSize()[0] - indent - 6;

for (var i = 0, cpos = 0, l = node.length; i < l; i++, cpos++) {

buffer += node[i];

if (cpos >= maxWidth) {

cpos = 0;

meta.push({
description: description + '\033[31m"' + buffer + '"\033[0m',
expanded: false,
displayed: first,
type: type,
depth: depth,
index: seed++
});

if (node.length > process.stdout.getWindowSize()[0]/2) {
node = node.substr(0, process.stdout.getWindowSize()[0]/2) + '...';
buffer = '';
}

}

if (buffer.length > 0) {

meta.push({
description: itemPrefix + '\033[31m"' + node + '"\033[0m',
description: description + '\033[31m"' + buffer + '"\033[0m',
expanded: false,
displayed: first,
type: type,
depth: depth,
index: seed
index: seed++
});

}

indent--;
}

switch(type) {
case 'string':

var extLen = (indent + itemPrefix.length) - 2;
var truncatedNode = '0';

if (node.length > process.stdout.getWindowSize()[0] - extLen) {
truncatedNode = '▸ ' + '\033[31m"' + node.substr(0, process.stdout.getWindowSize()[0]/2) + '..."\033[0m';
}
else {
truncatedNode = '\033[31m"' + node + '"\033[0m';
}

meta.push({
description: itemPrefix + truncatedNode,
expanded: false,
displayed: first,
type: type,
depth: depth,
index: seed
});

addStrData(node);


break;
case 'number':
case 'boolean':
Expand All @@ -124,14 +179,17 @@ var constructMeta = function constructMeta(parentType, depth, node, itemPrefix)
case 'function':

meta.push({
description: itemPrefix + '[Function]',
description: itemPrefix + '▸ \033[36m[Function]\033[0m',
expanded: false,
displayed: first,
type: type,
depth: depth,
index: seed
});

addStrData(node.toString());


break;
case 'array':

Expand Down Expand Up @@ -238,13 +296,20 @@ var listener = function listener(chunk, key) {
//
if ((key.name === 'space' || key.name === 'enter' ||
key.name === 'right' || key.name === 'left') &&
(meta[index].type === 'array' || meta[index].type === 'object')) {
(meta[index].type === 'array' || meta[index].type === 'object' ||
meta[index].type === 'function' || meta[index].type === 'string')) {

var start = selection;
var stop = meta.length;
var next = meta[index].depth+1;
var started = false;

if (meta[index].type === 'string' &&
(meta[index].description.indexOf('▸') === -1 &&
meta[index].description.indexOf('▾') === -1)) {
return;
}

if (meta[index].expanded) {

meta[index].description = meta[index].description.replace('▾', '▸');
Expand Down
31 changes: 29 additions & 2 deletions example/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var stuff = {
"interacive",
"prompt"
],
"version" : "0.0.1",
"version" : function(a) { if ('x' && 1) { return 'A short string'; } else if (a) { console.log('foobar'); return 2000; } return 10; },
"author" : "Paolo Fragomeni <paolo@nodejitsu.com>",
"repository" : {
"type" : "git",
Expand All @@ -37,7 +37,8 @@ var stuff = {
"prompt"
],
"vsersion" : "0.0.1",
"asuthor" : "Paolo Fragomeni <paolo@nodejitsu.com>Paolo Fragomeni <paolo@nodejitsu.com>Paolo Fragomeni <paolo@nodejitsu.com>Paolo Fragomeni <paolo@nodejitsu.com>Paolo Fragomeni <paolo@nodejitsu.com>",
"asuthor" : "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
"asuadadasdsadthor" : "Paolo Fragomeni <paolo@nodejitsu.com>Paolo Fragomeni <paolo@nodejitsu.com>Paolo Fragomeni <paolo@nodejitsu.com>Paolo Fragomeni <paolo@nodejitsu.com>Paolo Fragomeni <paolo@nodejitsu.com>",
"rsepository" : {
"type" : "git",
"url" : "http://github.com/hij1nx/node-dir.git"
Expand All @@ -49,6 +50,32 @@ var stuff = {
"masin" : "./dir",
"sscripts": {
"test": "node tests/test.js"
},
"foo": {
"a" : "dir",
"descsription" : "An interactive console.dir() for the terminal.",
"tasgs" : [
"console",
"debug",
"debugging",
"json",
"interacive",
"prompt"
],
"vsersion" : "0.0.1",
"asuthor" : "Paolo Fragomeni <paolo@nodejitsu.com>Paolo Fragomeni <paolo@nodejitsu.com>Paolo Fragomeni <paolo@nodejitsu.com>Paolo Fragomeni <paolo@nodejitsu.com>Paolo Fragomeni <paolo@nodejitsu.com>",
"rsepository" : {
"type" : "git",
"url" : "http://github.com/hij1nx/node-dir.git"
},
"bugss" : {
"url" : "http://github.com/hij1nx/node-dir/issues"
},
"ensgines" : ["node >= 0.6.x"],
"masin" : "./dir",
"sscripts": {
"test": "node tests/test.js"
}
}
};

Expand Down

0 comments on commit 68e16ab

Please sign in to comment.