Skip to content

Standard Grains

Praia ships with a set of standard library grains. These are installed to <libdir>/grains/ and available in any project without installation.

Import them with use:

use "testing"
use "router"
use "colors"
GrainDescription
routerExpress-style HTTP routing with path parameters, middleware support
middlewareCORS, JSON body parsing, auth, request IDs for the router
cookieHTTP cookie parsing and building
sessionServer-side session management (in-memory store)
GrainDescription
testingTest framework with test(), assertEqual(), done()
GrainDescription
csvCSV parsing and generation
tomlTOML parser
iniINI file parser
htmlHTML utilities
markdownMarkdown processing
templateString templating
GrainDescription
colorsANSI color and style helpers (red, green, bold, RGB, etc.)
progressProgress bars and spinners
tableFormatted text tables
loggerStructured logging with levels (debug, info, warn, error)
GrainDescription
stringsExtended string utilities
collectionsData structure utilities
mathExtended math functions
geometryGeometry helpers
datetimeDate and time utilities
timersTimer utilities
uuidUUID generation
validateInput validation
argsCommand-line argument parsing
diffText diffing
syncSynchronization primitives
GrainDescription
hexHex encoding/decoding, integer conversion, hex dumps
reAdvanced regex with named groups, split, escape
use "testing"
testing.test("addition", lam{ in
testing.assertEqual(1 + 1, 2, nil)
})
testing.done()

Run with praia test to discover and execute test files.

use "router"
use "middleware"
use "logger"
let log = logger.create("API")
let server = router.create()
server.use(middleware.cors())
server.use(middleware.jsonBody())
server.use(logger.middleware(log))
server.get("/", lam{ req, params in
return http.json({message: "hello"})
})
server.listen(8080)
use "colors"
print(colors.red("error:") + " something went wrong")
print(colors.green("ok"))
print(colors.bold(colors.blue("important")))
print(colors.rgb("custom color", 255, 128, 0))
use "progress"
let p = progress.bar({width: 30, showCount: true})
p.total(100)
for (i in 0..101) {
p.update(i)
time.sleep(10)
}
p.done()
use "table"
let users = [
{name: "Alice", age: 30, role: "admin"},
{name: "Bob", age: 25, role: "user"}
]
print(table.render(users))
use "hex"
hex.encode("Hello") // "48656c6c6f"
hex.decode("48656c6c6f") // "Hello"
hex.fromInt(255, 4) // "00ff"
hex.toInt("0xDEADBEEF") // 3735928559
print(hex.dump("Hello, World!\n"))