Skip to content

Instantly share code, notes, and snippets.

@ten0s ten0s/c.erl
Last active Dec 27, 2016

Embed
What would you like to do?
erlang shell ls
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.
@ten0s

This comment has been minimized.

Copy link
Owner Author

ten0s commented Dec 27, 2016

Doesn't shrinks file names to 40 chars and uses real columns count if available.
Not yet completely ready

@ten0s

This comment has been minimized.

Copy link
Owner Author

ten0s commented Dec 27, 2016

rates ok

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.