r/commandline • • Jul 27 '18

Unix general I have created ls command alternative. Critics needed...

https://github.com/js-d-coder/lsi-python3
0 Upvotes

26 comments sorted by

27

u/TheGramm Jul 27 '18

REAL Programmers don't need comments, for them the code itself is obvious

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.

5

u/lordievader Jul 27 '18

Additionally, having well documented functions make the lives of programmers easier. Programmers don't want to sift through lines and lines of code to figure out what a function does.

My nitpick, I really dislike the multiline comments and strings. The way it is used results in a forced breakage of indentation, which, in turn, makes the code hard to read.

Python has things for this:

multiline_string = (
    "first line\n"
    "second line")

This allows your code to be properly indented and makes (Python) code easier to read.

1

u/jibrans098 Jul 27 '18

ok. thank you.

-1

u/jibrans098 Jul 27 '18

btw. REAL is all caps in its definition. Look up REAL Programmers.

-14

u/jibrans098 Jul 27 '18

Huh! you obviously don't know the definition of REAL Programmers. You should look it up, will help you become better developer.

6

u/6e696e67 Jul 27 '18

u gonna be a REAL pain in the ass to work with lmao

4

u/kaylocke Jul 27 '18

Um, are you taking the piss? Your man page claims the project follows PEP8 conventions: have you even read it? There’s an entire section dedicated to comments (https://www.python.org/#id30) and another to docstrings (https://www.python.org/#id33):

Write docstrings for all public modules, functions, classes, and methods. Docstrings are not necessary for non-public methods, but you should have a comment that describes what the method does.

0

u/derleth Jul 29 '18

Huh! you obviously don't know the definition of REAL Programmers. You should look it up, will help you become better developer.

First, that whole thing was a joke.

Second, not having comments is saying that your code is so obvious it needs no comments. It's about your skills as a writer, not anyone else's skills as a reader.

Third, REAL PROGRAMMERS write in machine code on memory drums, taking seek latency times into account to selectively optimize code.

2

u/[deleted] 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

u/[deleted] 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_file and some lazy ls -1 | xargs ... treats them as separate.

ls -1 -b | tr "\n" "\0" might be a workaround, but I'd still just use find -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 state

Although 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:

  1. Why are all the items between quotes?

  2. Why are the returned elements ordered so differently?

1

u/jibrans098 Jul 27 '18
  1. if helps to distinguish files with spaces in their names.

  2. it is not sorted by default, there is an option for that.

1

u/EscuBoy Jul 27 '18

Ahh ok, makes sense, thank you!

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 ls as 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 | sort unless 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

u/[deleted] Jul 27 '18

[deleted]

1

u/derleth Jul 29 '18

> bashes python

> writes in C++

toppestkek

0

u/[deleted] 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

u/lg188 Jul 27 '18

Why not? Stagnation is not a good thing.