simplinput

Synchronous, cross-target standard input (stdin) reading for Gleam.

Types

Errors that can occur when reading from stdin.

pub type InputError {
  Eof
  NotUtf8
  ReadError(String)
}

Constructors

  • Eof

    End of input was reached before any bytes could be read.

  • NotUtf8

    Input bytes could not be decoded as valid UTF-8.

  • ReadError(String)

    An underlying OS or runtime I/O error occurred.

Values

pub fn fold_chunks(
  size: Int,
  from initial: acc,
  with fun: fn(acc, BitArray) -> acc,
) -> Result(acc, InputError)

Streams chunks of size bytes from stdin until EOF.

Accumulates full size-byte chunks until EOF (only the final chunk may be shorter).

pub fn fold_lines(
  from initial: acc,
  with fun: fn(acc, String) -> acc,
) -> Result(acc, InputError)

Streams lines from stdin until EOF, folding them with fun.

For large inputs, read() followed by string.split is much faster.

pub fn is_tty() -> Bool

Returns True if stdin is attached to an interactive terminal.

pub fn prompt(message: String) -> Result(String, InputError)

Writes message to stdout and reads one line from stdin using the same semantics as read_line.

pub fn read() -> Result(String, InputError)

Reads all remaining bytes from stdin until EOF and decodes them as UTF-8.

pub fn read_bits() -> Result(BitArray, InputError)

Reads all remaining bytes from stdin until EOF as a raw BitArray.

pub fn read_chunk(count: Int) -> Result(BitArray, InputError)

Reads up to count raw bytes from stdin.

pub fn read_line() -> Result(String, InputError)

Reads a single line from stdin up to the next \n or EOF, stripping trailing \r\n or \n.

pub fn read_lines() -> Result(List(String), InputError)

Reads all remaining lines from stdin until EOF, stripping trailing \r\n or \n from each line.

✨ Search Document