Last active
December 27, 2016 13:12
-
-
Save ten0s/a13c6e18109e3fc9a58710d157aeb7ae to your computer and use it in GitHub Desktop.
erlang shell ls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ls(Dir) -> | |
case file:list_dir(Dir) of | |
{ok, Entries} -> | |
ls_print(sort(Entries)); | |
{error, enotdir} -> | |
ls_print([Dir]); | |
{error, Error} -> | |
format("~ts\n", [file:format_error(Error)]) | |
end. | |
ls_print([]) -> ok; | |
ls_print(L) -> | |
Cols = columns(), | |
Width = min([Cols, max(lengths(L)) + 5]), | |
ls_print(L, Cols, Width, 0). | |
ls_print(X, Cols, Width, Len) when Width + Len > Cols -> | |
io:nl(), | |
ls_print(X, Cols, Width, 0); | |
ls_print([H|T], Cols, Width, 0) when Cols div Width =< 1 -> | |
io:format("~ts", [H]), | |
io:nl(), | |
ls_print(T, Cols, Width, 0); | |
ls_print([H|T], Cols, Width, Len) -> | |
io:format("~-*ts", [Width, H]), | |
ls_print(T, Cols, Width, Width + Len); | |
ls_print([], _, _, _) -> | |
ok. | |
lengths(X) -> | |
lengths(X, []). | |
lengths([H|T], L) -> lengths(T, [length(H)|L]); | |
lengths([], L) -> L. | |
columns() -> | |
case io:columns() of | |
{ok, Cols} -> | |
Cols; | |
{error, _} -> | |
80 | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
rates ok