Skip to content

Use inspect() to show a list in console instead of raw array object#137

Closed
piranna wants to merge 8 commits intoshelljs:masterfrom
piranna:master
Closed

Use inspect() to show a list in console instead of raw array object#137
piranna wants to merge 8 commits intoshelljs:masterfrom
piranna:master

Conversation

@piranna
Copy link
Copy Markdown

@piranna piranna commented Jun 29, 2014

As shown on http://nodejs.org/api/util.html#util_customizing_util_inspect_colors, objects can has a inspect() method that will be used by util.inspect() on the Node.js REPL, so we can show on the console an output more similar to the actual one on the shell, while still returning the actual array so it can be used by other functions:

> require('./global')
{}
> var a = ls()
undefined
> a
LICENSE
README.md
bin
global.js
make.js
package.json
scripts
shell.js
src
test
> a instanceof Array
true
> a.join()
'LICENSE,README.md,bin,global.js,make.js,package.json,scripts,shell.js,src,test'
> a.inspect
[Function: inspect]
> a.inspect()
'LICENSE\nREADME.md\nbin\nglobal.js\nmake.js\npackage.json\nscripts\nshell.js\nsrc\ntest'
> JSON.stringify(a)
'["LICENSE","README.md","bin","global.js","make.js","package.json","scripts","shell.js","src","test"]'

I've only apply this to the array returned by ls(), but it could be done in some other places. A good candidate to do so is grep(), where it's currently returning a multiline string while it could be better to return an array (with inspect() method) instead as ls() does.

I'm using Object.defineProperty() to set inspect() so it doesn't became an enumerable property.

@piranna
Copy link
Copy Markdown
Author

piranna commented Jul 5, 2014

I've added the inspect() method to the output of other commands including grep, and unified their output result (now all are arrays, not strings separated by endlines). I'll add support for the -l parameter en ls to show how flexible can this be.

@nfischer
Copy link
Copy Markdown
Member

@ariporad what are your thoughts? I like the idea of having grep() and sed() returns lists similarly to how ls() does (it seems more useful to me). The output on the node commandline isn't as important to me, and I think it might actually be confusing to display something other than what is returned.

@piranna the colors definitely rock though! The only significant one missing from ls() is green for executables, I think.

@piranna would you be interested in refactoring this into several smaller PRs? I think ls -l, for example, would benefit users, and we could merge that much sooner than the whole chunk.

@ariporad
Copy link
Copy Markdown
Contributor

Yeah. I'm not sure about the inspect bit... It could be confusing to some people.

@ariporad
Copy link
Copy Markdown
Contributor

@piranna: I'm not really sure about the inspect() thing, I feel like it's a little counter-intuitive.

@nfischer: I personally vote to close this. What do you think?

@nfischer
Copy link
Copy Markdown
Member

@ariporad I don't think we can merge this as-is, because it implements too many things.

@piranna I'd really like to merge some of these features, but I would need them to be done in separate PRs. If you or anyone else would like to refactor some of these features into separate PRs, that would be very appreciated.

@nfischer nfischer mentioned this pull request Jan 30, 2016
@piranna
Copy link
Copy Markdown
Author

piranna commented Jan 30, 2016

The output on the node commandline isn't as important to me, and I think it might actually be confusing to display something other than what is returned.

When used on interactive REPL it matters, specially with the -l flag.

@piranna the colors definitely rock though! The only significant one missing from ls() is green for executables, I think.

Colors come from the $LS_COLORS environment variable, so they are the ones defined on the system, I can't do here nothing.

@piranna would you be interested in refactoring this into several smaller PRs? I think ls -l, for example, would benefit users, and we could merge that much sooner than the whole chunk.

I started Coreutils.js, my own implementation on the Unix commands using streams of objects instead of arrays. I think this is fairly a better approach to that problem.

@piranna I'd really like to merge some of these features, but I would need them to be done in separate PRs. If you or anyone else would like to refactor some of these features into separate PRs, that would be very appreciated.

You can cherry pick the parts you are interested about, but definitely I think inspect() support is a plus, if not it would not be used on the Node.js core libraries... One thing is how are the data internally, and how it's represented, and an example of this is the -l flag that's only purposse are visual artifacts.

@nfischer
Copy link
Copy Markdown
Member

@ariporad I vote to put off adding inspect(), and focus on the underlying details, like ls -l, and turning multi-line output into arrays where it makes sense.

@ariporad
Copy link
Copy Markdown
Contributor

@nfischer: SGTM.

@ariporad
Copy link
Copy Markdown
Contributor

@nfischer: I vote we close this pull request, because it's really tied to the current ls implementation, which is about to be replaced.

@nfischer nfischer closed this Feb 21, 2016
@nfischer
Copy link
Copy Markdown
Member

SGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants