aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/gleam/io.gleam82
-rw-r--r--src/gleam_stdlib.erl10
-rw-r--r--src/gleam_stdlib.mjs8
3 files changed, 99 insertions, 1 deletions
diff --git a/src/gleam/io.gleam b/src/gleam/io.gleam
index b28da87..f9b093b 100644
--- a/src/gleam/io.gleam
+++ b/src/gleam/io.gleam
@@ -26,6 +26,32 @@ if javascript {
"../gleam_stdlib.mjs" "print"
}
+/// Writes a string to standard error.
+///
+/// If you want your output to be printed on its own line see `eprintln`.
+///
+/// ## Example
+///
+/// ```
+/// > io.eprint("Hi pop")
+/// // -> Hi pop
+/// Nil
+/// ```
+///
+pub fn eprint(string: String) -> Nil {
+ do_eprint(string)
+}
+
+if erlang {
+ external fn do_eprint(string: String) -> Nil =
+ "gleam_stdlib" "eprint"
+}
+
+if javascript {
+ external fn do_eprint(String) -> Nil =
+ "../gleam_stdlib.mjs" "eprint"
+}
+
/// Writes a string to standard output, appending a newline to the end.
///
/// ## Example
@@ -50,6 +76,30 @@ if javascript {
"../gleam_stdlib.mjs" "log"
}
+/// Writes a string to standard error, appending a newline to the end.
+///
+/// ## Example
+///
+/// ```gleam
+/// > io.eprintln("Hi pop")
+/// // -> Hi mum
+/// Nil
+/// ```
+///
+pub fn eprintln(string: String) -> Nil {
+ do_eprintln(string)
+}
+
+if erlang {
+ external fn do_eprintln(string: String) -> Nil =
+ "gleam_stdlib" "eprintln"
+}
+
+if javascript {
+ external fn do_eprintln(String) -> Nil =
+ "../gleam_stdlib.mjs" "error"
+}
+
/// Prints a value to standard output (stdout) yielding Gleam syntax.
///
/// The value is returned after being printed so it can be used in pipelines.
@@ -85,3 +135,35 @@ pub fn debug(term: anything) -> anything {
term
}
+
+/// Prints a value to standard error (stderr) yielding Gleam syntax.
+///
+/// The value is returned after being printed so it can be used in pipelines.
+///
+/// ## Example
+///
+/// ```gleam
+/// > io.edebug("Hi pop")
+/// // -> <<"Hi pop">>
+/// "Hi pop"
+///
+/// > io.edebug(Ok(1))
+/// // -> {ok, 1}
+/// Ok(1)
+///
+/// > import list
+/// > [1, 2]
+/// > |> list.map(fn(x) { x + 1 })
+/// > |> io.edebug
+/// > |> list.map(fn(x) { x * 2 })
+/// // -> [2, 3]
+/// [4, 6]
+/// ```
+///
+pub fn edebug(term: anything) -> anything {
+ term
+ |> string.inspect
+ |> eprintln
+
+ term
+}
diff --git a/src/gleam_stdlib.erl b/src/gleam_stdlib.erl
index b533153..1ed963c 100644
--- a/src/gleam_stdlib.erl
+++ b/src/gleam_stdlib.erl
@@ -10,7 +10,7 @@
percent_encode/1, percent_decode/1, regex_check/2, regex_split/2,
base_decode64/1, parse_query/1, bit_string_concat/1, size_of_tuple/1,
decode_tuple/1, tuple_get/2, classify_dynamic/1, print/1, println/1,
- inspect/1, float_to_string/1]).
+ eprint/1, eprintln/1, inspect/1, float_to_string/1]).
%% Taken from OTP's uri_string module
-define(DEC2HEX(X),
@@ -319,6 +319,14 @@ println(String) ->
io:put_chars([String, $\n]),
nil.
+eprint(String) ->
+ io:put_chars(standard_error, String),
+ nil.
+
+eprintln(String) ->
+ io:put_chars(standard_error, [String, $\n]),
+ nil.
+
inspect(true) ->
"True";
inspect(false) ->
diff --git a/src/gleam_stdlib.mjs b/src/gleam_stdlib.mjs
index a612367..8a2d88f 100644
--- a/src/gleam_stdlib.mjs
+++ b/src/gleam_stdlib.mjs
@@ -228,6 +228,14 @@ export function print(string) {
}
}
+export function eprint(string) {
+ if (typeof process === "object") {
+ process.stderr.write(string); // We can write without a trailing newline
+ } else {
+ console.error(string); // We're in a browser. Newlines are mandated
+ }
+}
+
export function ceiling(float) {
return Math.ceil(float);
}