Read bounded byte chunks from standard input and write checked text to standard output or standard error.
Import
import stdlib/console as console
For an async function, select the async module instead:
import stdlib/console/async as console
Await effectful operations. In the signatures below, an ordinary effectful function becomes async under the async import; parameter and result types stay the same. Pure functions and value methods remain ordinary calls. The two imports share their value and error types.
read()
read(limit: Nat): Result[Option[Bytes],ConsoleError]
Parameters
limit: Nat— maximum bytes for this read, from 1 through 65,536.
Returns: Result[Option[Bytes],ConsoleError]. Ok(Some(data)) contains a non-empty chunk; Ok(None()) means EOF.
A read may return fewer bytes than requested. It is not a line reader. UTF-8 characters can span chunks: assemble a bounded complete buffer before decoding, or retain incomplete trailing bytes. Only one async stdin read may be outstanding.
Errors: InvalidReadLimit for an invalid limit; ReadBusy for an overlapping read; I/O, worker-resource or unsupported-host failures are reported separately.
write()
write(message: Str): Result[Unit,ConsoleError]
Parameters
message: Str— at most 16,384 UTF-8 bytes, excluding any added newline.
Returns: Result[Unit,ConsoleError].
Writes the text to standard output without adding a newline. The async form is awaited. Check the result; a failed diagnostic write cannot reliably report itself through the same broken stream.
Errors: OutputTooLarge when the message exceeds the byte limit; I/O, worker-resource and unsupported-host failures are retained.
write_line()
write_line(message: Str): Result[Unit,ConsoleError]
Parameters
message: Str— at most 16,384 UTF-8 bytes, excluding any added newline.
Returns: Result[Unit,ConsoleError].
Writes the text to standard output and adds a newline. The async form is awaited. Check the result; a failed diagnostic write cannot reliably report itself through the same broken stream.
Errors: OutputTooLarge when the message exceeds the byte limit; I/O, worker-resource and unsupported-host failures are retained.
write_error()
write_error(message: Str): Result[Unit,ConsoleError]
Parameters
message: Str— at most 16,384 UTF-8 bytes, excluding any added newline.
Returns: Result[Unit,ConsoleError].
Writes the text to standard error without adding a newline. The async form is awaited. Check the result; a failed diagnostic write cannot reliably report itself through the same broken stream.
Errors: OutputTooLarge when the message exceeds the byte limit; I/O, worker-resource and unsupported-host failures are retained.
write_error_line()
write_error_line(message: Str): Result[Unit,ConsoleError]
Parameters
message: Str— at most 16,384 UTF-8 bytes, excluding any added newline.
Returns: Result[Unit,ConsoleError].
Writes the text to standard error and adds a newline. The async form is awaited. Check the result; a failed diagnostic write cannot reliably report itself through the same broken stream.
Errors: OutputTooLarge when the message exceeds the byte limit; I/O, worker-resource and unsupported-host failures are retained.
ConsoleError
record ConsoleError { operation: ConsoleOperation, kind: ConsoleErrorKind, native: HostError }
operation distinguishes reading (including the limit), stdout writes and stderr writes. The error does not retain the output text. Use problem.kind for control flow and render for a human-readable explanation. The native field preserves the original failure. Host-backed native errors contain operation: Str, code: Str and native_code: Nat; a zero native number means no host error number was supplied.
enum ConsoleErrorKind {
InvalidReadLimit, OutputTooLarge, ReadBusy, IOFailure, ResourceLimit, UnsupportedHost, OtherFailure
}
ConsoleError.render()
pure render(): Str
Parameters
None.
Returns: Str, a human-readable explanation of this error, including the available operation and failure details.
Pure. Use the structured error fields for control flow; the wording is diagnostic text and must not be parsed.
Example
Read the complete bounded-input program. It handles EOF, the total input limit, invalid UTF-8 and failed output.
Complete example: async console input
import stdlib/console/async as console
import stdlib/encoding/utf8 as utf8
import stdlib/host/async as host
async read_message(limit: Nat): Result[Bytes,Str] {
mut data = bytes()
mut finished = false
mut failure: Option[Str] = None()
while !finished {
match await console.read(512) {
Error(problem) => { failure = Some(problem.render()); finished = true },
Ok(input) => match input {
None() => { finished = true },
Some(chunk) => {
if data.len() + chunk.len() > limit {
failure = Some("Input exceeds the ${limit}-byte message limit.")
finished = true
} else { data = data.join(chunk) }
}
}
}
}
match failure { Some(message) => Error(message), None() => Ok(data) }
}
async main(): Unit {
match await read_message(4096) {
Error(message) => {
reported = await console.write_error_line(message)
await host.exit(1)
},
Ok(data) => match utf8.decode(data) {
Error(problem) => {
reported = await console.write_error_line("Input is not valid UTF-8.")
await host.exit(1)
},
Ok(text) => {
count = text.len()
match await console.write_line("Received ${count} characters.") {
Error(write_error) => await host.exit(1), Ok(written) => ()
}
}
}
}
}Implementation: view source on GitHub.