Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

ward.io

local io = require("ward.io")

Ward serializes reads/writes with internal mutexes so concurrent operations do not interleave unpredictably.

io.read_all(opts?) -> bytes string

Reads all remaining stdin into a string.

Optional opts:

  • max_bytes (number|integer) - if provided, fails when stdin exceeds this limit.
local s = io.read_all()

-- hard cap (1 MiB)
local s2 = io.read_all({ max_bytes = 1024 * 1024 })

io.read_line() -> byts string|nil

Reads one line from stdin.

  • Returns nil on EOF.
local line = io.read_line()
if line == nil then return end
print("got:", line)

io.read_lines() -> function

Returns an iterator-like function. Each call reads one line from stdin and returns bytes string|nil (nil on EOF).

local next_line = io.read_lines()
while true do
  local line = next_line()
  if line == nil then break end
  print(line)
end

Output

  • io.write_stdout(data) -> true
  • io.write_stderr(data) -> true
  • io.flush_stdout() -> nil
  • io.flush_stderr() -> nil
io.write_stdout("hello")
io.write_stderr("warn\n")
io.flush_stdout()