Current section

Files

Jump to
string_width src string_width_ffi.erl
Raw

src/string_width_ffi.erl

-module(string_width_ffi).
-export([
fold_codepoints/3,
utf_codepoint_to_string/1,
get_terminal_size/0
]).
fold_codepoints(Binary, State, Fun) ->
case Binary of
<<Cp/utf8, Rest/binary>> -> fold_codepoints(Rest, Fun(State, Cp), Fun);
_ -> State
end.
utf_codepoint_to_string(Cp) ->
<<Cp/utf8>>.
get_terminal_size() ->
case {io:rows(), io:columns()} of
{{ok, Rows}, {ok, Cols}} -> {ok, {Rows, Cols}};
_ -> case {os:getenv("LINES"), os:getenv("COLUMNS")} of
{false, _} -> {error, nil};
{_, false} -> {error, nil};
{LinesStr, ColumnsStr} ->
case catch {list_to_integer(LinesStr), list_to_integer(ColumnsStr)} of
{EnvRows, EnvCols} when is_integer(EnvRows) andalso is_integer(EnvCols) ->
{ok, {EnvRows, EnvCols}};
_ -> {error, nil}
end
end
end.