commit 1361b59551f0269f3c4d42a6591779f74d3a23bd
parent 5177df77362041f342a70436759b14cd44aa64f6
Author: Drew DeVault <sir@cmpwn.com>
Date: Sat, 6 Feb 2021 17:19:25 -0500
fmt: add fprintln and friends
These just add a newline to the end, which is something I've wished for
from Go for ages.
Diffstat:
1 file changed, 33 insertions(+), 0 deletions(-)
diff --git a/fmt/fmt.ha b/fmt/fmt.ha
@@ -52,10 +52,25 @@ export type formattable = (...types::numeric | uintptr
export fn printf(fmt: str, args: formattable...) (io::error | size) =
fprintf(os::stdout, fmt, args...);
+// Formats text for printing writes it to [os::stdout], followed by a line feed.
+export fn println(fmt: str, args: formattable...) (io::error | size) =
+ fprintln(os::stdout, fmt, args...);
+
// Formats text for printing writes it to [os::stderr].
export fn errorf(fmt: str, args: formattable...) (io::error | size) =
fprintf(os::stderr, fmt, args...);
+// Formats text for printing writes it to [os::stderr], followed by a line feed.
+export fn errorln(fmt: str, args: formattable...) (io::error | size) =
+ fprintln(os::stderr, fmt, args...);
+
+// Formats text for printing writes it to [os::stderr], followed by a line feed,
+// then exits the program with an error status.
+export @noreturn fn fatal(fmt: str, args: formattable...) void = {
+ fprintln(os::stderr, fmt, args...);
+ os::exit(1);
+};
+
type base = enum {
DECIMAL,
LOWER_HEX,
@@ -155,6 +170,24 @@ export fn fprintf(
return n;
};
+// Formats text for printing and writes it to an [io::stream], followed by a
+// line feed.
+export fn fprintln(
+ s: *io::stream,
+ fmt: str,
+ args: formattable...
+) (io::error | size) = {
+ let z = match(fprintf(s, fmt, args...)) {
+ z: size => z,
+ err: io::error => return err,
+ };
+ z += match (io::write(s, ['\n': u32: u8])) {
+ z: size => z,
+ err: io::error => return err,
+ };
+ return z;
+};
+
fn format(
out: *io::stream,
arg: formattable,