~/bend-docscommunity

main.bend relies on unsafe/foreign

raw source on the hub · import ezsys@0.2.0.0/main.bend as Main

ezsys: system helpers for Bend 2: whole-file reads and atomic saves, environment variables with a default, the wall clock as ISO 8601 UTC, text templates, Python's str helpers (strip, replace, split, find, case, width, digit order), a directory's names, and a failure written as Python does. This file is the whole public interface; everything under src/ is internal.

6 imports
import Base
import ./src/sys.bend as Sys
import ./src/clock.bend as Clock
import ./src/text.bend as Text
import ./src/errno.bend as Err
import ./src/str.bend as Str

Definitions

def file.read source · line 16 · raw

@path:String -> IO(Result<&1, &1, Pair(U32, String), String>)

a whole file's text; a failure to open or read it is a Fail, never an exit

def file.write source · line 20 · raw

@path:String -> @text:String -> IO(Result<&1, &1, Pair(U32, String), Unit>)

replace a file's whole text, creating it when it is missing

def file.save source · line 28 · raw

@path:String -> @text:String -> IO(Result<&1, &1, Pair(U32, String), Unit>)

write <path>.tmp, flush it, and rename it over path, so a reader sees the old text or the new, never part of either

def env.or source · line 36 · raw

@name:String -> @fallback:String -> IO(String)

an environment variable's value (even an empty one), or fallback when it is unset

def time.now source · line 41 · raw

IO(Pair(U32, U32))

the wall clock: whole seconds since 1970-01-01T00:00:00Z, and the milliseconds past them

def time.iso source · line 46 · raw

@secs:U32 -> @ms:U32 -> String

ISO 8601 UTC, YYYY-MM-DDTHH:MM:SS.mmmZ, of seconds since the epoch and a millisecond

def time.civil source · line 51 · raw

@secs:U32 -> Pair(U32, Pair(U32, Pair(U32, Pair(U32, Pair(U32, Pair(U32, U32))))))

the civil fields of seconds since the epoch, in UTC: year, month, day, hour, minute, second and ISO weekday (1 Monday to 7 Sunday)

def time.iso_week source · line 55 · raw

@secs:U32 -> Pair(U32, U32)

the ISO 8601 week-numbering year and week of seconds since the epoch

def time.iso_week_text source · line 59 · raw

@secs:U32 -> String

the ISO 8601 week as date -u +%G-W%V writes it, e.g. 2026-W43

def time.now_iso source · line 63 · raw

IO(String)

the wall clock as ISO 8601 UTC

def text.fill source · line 68 · raw

@template:String -> @pairs:List<&1, Pair(String, String)> -> String

every {{key}} in a template replaced by its value; unknown and unclosed markers are left as written

def text.fill_with source · line 72 · raw

@open:String -> @close:String -> @template:String -> @pairs:List<&1, Pair(String, String)> -> String

the same with other delimiters, e.g. <!-- and -->

def text.strip source · line 82 · raw

@txt:String -> String

the text with Python's whitespace removed from both ends (str.strip())

def text.lstrip source · line 86 · raw

@txt:String -> String

the text with Python's whitespace removed from its start (str.lstrip())

def text.rstrip source · line 90 · raw

@txt:String -> String

the text with Python's whitespace removed from its end (str.rstrip())

def text.replace source · line 95 · raw

@txt:String -> @old:String -> @new:String -> String

every old replaced by new, left to right, never overlapping (str.replace); an empty old puts new around every character

def text.split source · line 100 · raw

@txt:String -> @sep:String -> List<&2, String>

the pieces between every sep (str.split(sep)); an empty sep answers the text as its one piece

def text.find source · line 105 · raw

@txt:String -> @needle:String -> Maybe<&2, U32>

the index in code points where needle first starts, or None (str.find)

def text.lower source · line 110 · raw

@txt:String -> String

the text in lowercase (str.lower) for ASCII, Latin-1, Latin Extended-A, Greek and Cyrillic; other code points are kept

def text.upper source · line 115 · raw

@txt:String -> String

the text in uppercase (str.upper) for ASCII, Latin-1, Latin Extended-A, Greek and Cyrillic; other code points are kept

def text.pad_right source · line 119 · raw

@txt:String -> @width:Nat -> String

the text with spaces after it up to width code points (str.ljust)

def text.truncate source · line 123 · raw

@txt:String -> @width:Nat -> String

the first width code points of the text (txt[:width])

def digits.cmp source · line 127 · raw

@lhs:String -> @rhs:String -> Cmp

two strings of ASCII digits in numeric order, any length

def dir.list source · line 132 · raw

@path:String -> IO(Result<&1, &1, Pair(U32, String), List<&2, String>>)

a directory's names, sorted by code point, with no "." or ".."; a failure to read it is a Fail, never an exit

def err.os source · line 139 · raw

@code:U32 -> @why:String -> @path:String -> String

a failed file operation as Python's OSError writes it: [Errno 2] No such file or directory: '/tmp/x'