diff options
author | Shritesh Bhattarai <shr@ite.sh> | 2021-01-14 12:39:03 -0500 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2021-01-21 16:57:29 +0000 |
commit | 21668ef71d5669f148ea6f8db5736d11cb011241 (patch) | |
tree | f5904be15b2db9ce76a33c7858f35e102a2238a5 | |
parent | 6ad66f973f4a26d67983fae645b79585d4e12c47 (diff) | |
download | gleam_stdlib-21668ef71d5669f148ea6f8db5736d11cb011241.tar.gz gleam_stdlib-21668ef71d5669f148ea6f8db5736d11cb011241.zip |
Add io:get_line
-rw-r--r-- | src/gleam/io.gleam | 18 | ||||
-rw-r--r-- | src/gleam_stdlib.erl | 9 |
2 files changed, 26 insertions, 1 deletions
diff --git a/src/gleam/io.gleam b/src/gleam/io.gleam index 2b083b1..88fece6 100644 --- a/src/gleam/io.gleam +++ b/src/gleam/io.gleam @@ -55,3 +55,21 @@ pub fn debug(term: anything) -> anything { erl_print("~tp\n", [term]) term } + +/// Error value returned by `get_line` function +/// +pub type GetLineError { + Eof + NoData +} + +/// Reads a line from standard input with the given prompt. +/// +/// # Example +/// +/// > io.get_line("Language: ") +/// // -> Language: <- gleam +/// Ok("gleam\n") +/// +pub external fn get_line(prompt: String) -> Result(String, GetLineError) = + "gleam_stdlib" "get_line" diff --git a/src/gleam_stdlib.erl b/src/gleam_stdlib.erl index 500ed10..edf3d6e 100644 --- a/src/gleam_stdlib.erl +++ b/src/gleam_stdlib.erl @@ -11,7 +11,7 @@ string_pad/4, decode_tuple2/1, decode_map/1, bit_string_int_to_u32/1, bit_string_int_from_u32/1, bit_string_append/2, bit_string_part_/3, decode_bit_string/1, compile_regex/2, regex_match/2, regex_split/2, - regex_scan/2, base_decode64/1, wrap_list/1, rescue/1]). + regex_scan/2, base_decode64/1, wrap_list/1, rescue/1, get_line/1]). should_equal(Actual, Expected) -> ?assertEqual(Expected, Actual). should_not_equal(Actual, Expected) -> ?assertNotEqual(Expected, Actual). @@ -210,3 +210,10 @@ rescue(F) -> error:X -> {error, {errored, X}}; exit:X -> {error, {exited, X}} end. + +get_line(Prompt) -> + case io:get_line(Prompt) of + eof -> {error, eof}; + {error, _} -> {error, no_data}; + Data -> {ok, Data} + end.
\ No newline at end of file |