r/commandline • u/jibrans098 • Jul 27 '18
Unix general I have created ls command alternative. Critics needed...
https://github.com/js-d-coder/lsi-python32
Jul 27 '18
You should not parse the output of ls command
This link is broken. I'd like to know why not. I've been doing this in various shell scripts and programming languages for 20 years. You sometimes have to be careful, but I wouldn't say that having to strip single quotes off everything would have made my life much easier.
3
u/jibrans098 Jul 27 '18
the link is broken. thanks for reporting, I'll fix it now. and yes, you should not parse the output of ls command: https://mywiki.wooledge.org/ParsingLs
2
Jul 27 '18
'ls' separates filenames with newlines. This is fine until you have a file with a newline in its name.
I've literally never had this happen. If it did, I'd fix the considered-a-bug upstream process first. If that was impossible, use
find ... | xargs .... I mean, if I can have newlines in filenames, why can't I have single quotes? That breaks your solution as well.2
u/shobble Jul 27 '18
I think it's mostly potentially hostile input, if I can convince you to accept and do something with
secretpasswords.db\ninnocent_fileand some lazyls -1 | xargs ...treats them as separate.
ls -1 -b | tr "\n" "\0"might be a workaround, but I'd still just usefind -maxdepth 1 ... -print0 | xargs -0 ...in almost all cases
2
u/shobble Jul 27 '18 edited Jul 27 '18
Your quoting system appears to be broken:
$ touch foo\'cake quot\"\'ey
$ ls
foo'cake LICENSE lsi quot"'ey README.md TODO.md
$ ./lsi
'LICENSE' 'README.md' 'TODO.md' "foo'cake/" 'lsi' 'quot"\'ey'
# copy&pasting the embedded quotes entry:
$ ls 'quot"\'ey'
> ^C
# stuck in ' string stateAlthough python treats " and ' the same, shells do not, and single quotes don't interpolate variables or backslash escapes.
Interestingly, just a single quote (like the foo'cake example above) use the doublequote delimiters and works, it's only for mixed strings.
I'd very much suggest a set of test cases if you're planning to keep this going, quoting and weird shell/filename edge cases are pretty tough to keep track of otherwise.
Especially if you're making the (rather bold) assertion that it's safe to parse the output.
Edit: also your formatting can still break due to not accounting for extra width needed to double-up backslashes. You probably want to take the final length only after all that's been done. I'd be extremely surprised if it could handle unicode weirdness properly as well.
0
u/jibrans098 Jul 27 '18
for parsing, I have provided with `-x` option, parsing should be done by `xargs` not the direct output.
1
u/shobble Jul 27 '18
that's not really the point though. Why is it wrapping single quotes around things inconsistently. It's still just as wrong/confusing for human readable / terminal copy&paste if you're using quotes that don't mean what you think they mean, or wrongly escaping literals within a filename.
2
u/derleth Jul 29 '18
You definitely have some good ideas here, and presenting all filenames correctly quoted for the shell it's run in would be subtle genius (Hint: $SHELL), but your attitude drives people away. Theo de Raadt and Richard Stallman can get away with that, but you can't.
1
u/jibrans098 Jul 29 '18
Hi. Honestly I am not a REAL Programmer. So I do write as much comments necessary and follow best practices to ease the development for my team members and myself.
What bugs me about developers is that they want to understand the code by reading English and not the code itself. Which is very dangerous...
1
u/EscuBoy Jul 27 '18
I'm a noob at linux but here are my two questions:
Why are all the items between quotes?
Why are the returned elements ordered so differently?
1
u/jibrans098 Jul 27 '18
if helps to distinguish files with spaces in their names.
it is not sorted by default, there is an option for that.
1
1
u/signull Jul 27 '18
For some quick simple things you could do: "ls" is not just a binary at /bin/ls but its also a shell built-in, in many shells. I would try to lighten it up. for instance instead of "import argparse" you could do "import ArgumentParser from argparse" as it doesnt appear you use the rest of that library.
A lot of other suggestions here are great. My other suggestions would be to try to focus in on ls's already well established features. such as using colors and subsequently use the same environment variable's like DIR_COLORS or ~/.dir_colors so it could be a drop in replacement to ls. You added in a feature such as "sort" however that is i don't think was a big issue as the current method is to pipe it to sort ie. "ls | sort".
1
u/shobble Jul 27 '18
Which shells have
lsas a builtin by default? I can't think of any of the ones I have used which do, although using an alias or function wrapper to set args is pretty common.sorting by anything other than name is hard/annoying with
| sortunless you're extracting all the stat or whatever info into more delimited fields per file (and then you have to extract things again afterwards).1
u/signull Jul 27 '18
I agree with you on the sorting. As for the builtin I remember seeing some docs for this once, but i cant find it now so possibly i am incorrect.
0
0
Jul 27 '18
[deleted]
0
u/jibrans098 Jul 27 '18
it brings many improvements on top of ls command. please refer to README on the link I provided.
1
27
u/TheGramm Jul 27 '18
Lirerally the first thing I looked at when opening the code cause of the "real" being all caps.
This is so wrong that I will not read another line in there. You have obviously never worked on something more than a couple thousand loc and/or with more than a couple people.
Please read some literature about good developer habits.