fix(alias): ls alias lx is an illegal option on darwin bsd style ls#1752
fix(alias): ls alias lx is an illegal option on darwin bsd style ls#1752casaper wants to merge 1 commit intosorin-ionescu:masterfrom
Conversation
|
👍 Edit: correct PR number. |
modules/utility/init.zsh
Outdated
| alias la='ll -A' # Lists human readable sizes, hidden files. | ||
| alias lm='la | "$PAGER"' # Lists human readable sizes, hidden files through pager. | ||
| alias lx='ll -XB' # Lists sorted by extension (GNU only). | ||
| if [[ "$OSTYPE" != darwin* ]]; then |
There was a problem hiding this comment.
Can we move this to the top? Makes the code more maintainable in the long run. (this can be considered as a nitpick :) )
There was a problem hiding this comment.
can do. But I just considered to keep changes at a minimum.
But regarding readability I also had something in mind, and that is why I did #1793 with helpers. Presuming that helper would exist, would that calm your nit'ish feeling a little? 😉
There was a problem hiding this comment.
#1793 is highly optional. I was just mentioning it, because I interpreted your comment as a readability one.
Will add belak then... thx for the hint 😄
There was a problem hiding this comment.
My comment is totality a readability one 😄
There was a problem hiding this comment.
I changed this code now. Now I check for the capability of ls instead of the OS running.
Just the fact that you're running on darwin doesn't prove your having a BSD style ls.
Since I'm aware of its existence I myself have the gnu coreutils installed here, so for myself this fix is not needed any more. 😉
|
Added a comment |
|
I'm not sure the best way to do this, but this alias should be enabled only if ls is coreutils based. I believe this will also be broken on BSD. It's good to know though. I'd be happy to merge this with a slightly better check. |
What would you think if I checked for ls running with code 0 with This way it would really only depend on ls's capabilities, and not on the OS at all. if ls -XB &> /dev/null; then
alias lx='ll -XB' # Lists sorted by extension (GNU only).
fiHaven't tried if that would work well everywhere. The idea just came up when you asked for a better check... |
018a6df to
cb76c48
Compare
The option `-X` lets MacOS (and probably also other BSD-ish OS) ls complain about an illegal option: ``` ls -lhXB ls: illegal option -- X usage: ls [-ABCFGHLOPRSTUWabcdefghiklmnopqrstuwx1] [file ...] ```
cb76c48 to
cd12f76
Compare
indrajitr
left a comment
There was a problem hiding this comment.
Looks good, other than the way to test for gnu coreutils.
| alias sl='ls' # I often screw this up. | ||
|
|
||
| # Only make alias if ls supports -XB (darwins BSD style ls doesn't) | ||
| if ls -XB &> /dev/null; then |
There was a problem hiding this comment.
Wondering if you would consider if is-callable 'dircolors' instead.
ls -XB might be expensive depending on the directory on which it is executed.
These things keep on adding to the startup time (unless we memoize it on first call).
There was a problem hiding this comment.
Maybe we could use ls /dev/null since that should only be a single file and should be fast.
There was a problem hiding this comment.
Yes, ls -XB /dev/null would be a great idea! That said, would be nice to be consistently using one way to detect BSD/GNU versions. Turns out we use is-callable 'dircolors' to detect this already.
There was a problem hiding this comment.
Or just detect the options via --help like so:
if grep -q '\-X' <(ls --help 2>&1); then
# ...
fiWe do similar thing in rsync module.
There was a problem hiding this comment.
@indrajitr I wasn't aware of the performance penalty my idea could introduce, and your concern makes quite a lot of sense.
On the part of finding a better solution:
At first glance the most straight forward solution @belak has suggested ls -XB /dev/null seems feasible, but in the end the callable 'dircolors' appears to be the most efficient and elegant alternative.
But regarding the test with dircolors, I have one question: Is there an unlikeley case I might not see, where dircolors is not callable and gnu-ls is present or the other way around?
Fixes illegal option
-Xon MacOSThe option
-Xgets MacOS'ls(and probably also other BSD-ish OS) to complain about an illegal option:user@host ~ $ ls -lhXB ls: illegal option -- X usage: ls [-ABCFGHLOPRSTUWabcdefghiklmnopqrstuwx1] [file ...]Proposed Changes
lxwith option-Xon machines that are not DarwinI would also exclude other BSD like OS but since I do not have such a host at hand I cannot verify if that is even really needed for them.