%% Stdio helpers for the Glint language server. %% Only used on the Erlang target. -module(glint_lsp_ffi). -export([read_bytes/1, write_bytes/1, read_line/0]). %% Read exactly N bytes from stdin as a bit array / binary. read_bytes(N) when is_integer(N), N >= 0 -> case file:read(standard_io, N) of {ok, Data} when is_binary(Data) -> {ok, Data}; {ok, Data} when is_list(Data) -> {ok, list_to_binary(Data)}; eof -> {error, <<"eof">>}; {error, Reason} -> {error, iolist_to_binary(io_lib:format("~p", [Reason]))} end. %% Write raw bytes to stdout (must not go through the logger). write_bytes(Data) when is_binary(Data) -> ok = file:write(standard_io, Data), ok; write_bytes(Data) when is_list(Data) -> ok = file:write(standard_io, list_to_binary(Data)), ok. %% Read one line from stdin (including the trailing newline when present). read_line() -> case io:get_line("") of eof -> {error, <<"eof">>}; {error, Reason} -> {error, iolist_to_binary(io_lib:format("~p", [Reason]))}; Line when is_list(Line) -> {ok, unicode:characters_to_binary(Line)}; Line when is_binary(Line) -> {ok, Line} end.