Skip to content

Instantly share code, notes, and snippets.

@ten0s
Last active December 27, 2016 13:12
Show Gist options
  • Select an option

  • Save ten0s/a13c6e18109e3fc9a58710d157aeb7ae to your computer and use it in GitHub Desktop.

Select an option

Save ten0s/a13c6e18109e3fc9a58710d157aeb7ae to your computer and use it in GitHub Desktop.
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
Copy link
Copy Markdown
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
Copy link
Copy Markdown
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