1 |
|
-module(klsn_io). |
2 |
|
|
3 |
|
-export([ |
4 |
|
format/1 |
5 |
|
, format/2 |
6 |
|
, get_line/0 |
7 |
|
, get_line/1 |
8 |
|
]). |
9 |
|
|
10 |
|
%% @doc |
11 |
|
%% Print *Format* (an io:format()-compatible string) to the current |
12 |
|
%% group leader using UTF-8 encoding. |
13 |
|
-spec format(io:format()) -> ok. |
14 |
|
format(Format) -> |
15 |
1 |
format(Format, []). |
16 |
|
|
17 |
|
%% @doc |
18 |
|
%% Same as format/1 but with an explicit *Data* list that will be |
19 |
|
%% interpolated into *Format*. |
20 |
|
-spec format(io:format(), [term()]) -> ok. |
21 |
|
format(Format, Data) -> |
22 |
1 |
io:format(device(), Format, Data). |
23 |
|
|
24 |
|
%% @doc |
25 |
|
%% Read a single line from STDIN without printing any prompt. The returned |
26 |
|
%% value is a UTF-8 binary or the atom returned by io:get_line/2 when the |
27 |
|
%% server signals end-of-file. |
28 |
|
-spec get_line() -> unicode:unicode_binary() | io:server_no_data(). |
29 |
|
get_line() -> |
30 |
1 |
get_line(<<>>). |
31 |
|
|
32 |
|
%% @doc |
33 |
|
%% Read a single line from STDIN after outputting *Prompt*. |
34 |
|
-spec get_line(io:prompt()) -> unicode:unicode_binary() | io:server_no_data(). |
35 |
|
get_line(Prompt) -> |
36 |
1 |
io:get_line(device(), Prompt). |
37 |
|
|
38 |
|
-spec device() -> io:device(). |
39 |
|
device() -> |
40 |
2 |
Device = erlang:group_leader(), |
41 |
2 |
io:setopts(Device, [{encoding,utf8}, {binary, true}]), |
42 |
2 |
Device. |
43 |
|
|