pencere

Elements

An element is a plain table that a chain of builder methods fills. Lengths are points.

pencere.div()

A box: a column of its children until told otherwise.

What pencere.div() draws What pencere.div() draws
local header = pencere.div():row():items_center():gap(8)
	:child(pencere.div():w(8):h(8):rounded(4):bg("success"))
	:child(pencere.text("postgres"):weight(700))
	:child(pencere.spacer())
	:child(pencere.text("running"):color("dim"))
local figures = pencere.div():row():gap(16)
	:child(pencere.div():gap(2):child(pencere.text("CPU"):color("dim")):child(pencere.text("128.4%")))
	:child(pencere.div():gap(2):child(pencere.text("Memory"):color("dim")):child(pencere.text("851 MiB")))
	:child(pencere.div():gap(2):child(pencere.text("Threads"):color("dim")):child(pencere.text("23")))
return pencere.div():w(320):p(14):gap(12)
	:bg("raised1"):border(1, "border"):rounded(10)
	:shadow(0, 8, 24, pencere.alpha("shadow", 0.4))
	:child(header)
	:child(figures)

pencere.text(string | number)

One line of text, in the font, size and colour of the boxes it is in; with wrap, as many lines as its box needs.

What pencere.text(string | number) draws What pencere.text(string | number) draws
return pencere.div():w(340):gap(8)
	:child(pencere.text("Deploy finished"):text_size(18):weight(700))
	:child(pencere.text("3 services, 12 seconds"):color("dim"))
	:child(pencere.text("Every line of text is one line unless it wraps, and then its box is as tall as the lines it needs."):wrap())
	:child(pencere.text("/usr/local/bin/postgres --serve --port 3032 --config /etc/postgres.conf"):truncate():color("dim"))
	:child(pencere.div():row():gap(12)
		:child(pencere.text("italic"):italic())
		:child(pencere.text("underline"):underline())
		:child(pencere.text("strike"):strike())
		:child(pencere.text("accent"):color("accent")))

pencere.spark(values, color)

A sparkline of numbers, newest last; :peak(value) is its largest.

What pencere.spark(values, color) draws What pencere.spark(values, color) draws
local cpu, net = {}, {}
for index = 1, 60 do
	table.insert(cpu, 30 + 14 * math.sin(index / 5) + 6 * math.sin(index / 1.7))
	table.insert(net, math.max(1, 10 + 9 * math.sin(index / 3.3) * math.sin(index / 9)))
end

local function card(title, value, values, color)
	return pencere.div():w(160):gap(6)
		:child(pencere.div():row():child(pencere.text(title):color("dim")):child(pencere.spacer()):child(pencere.text(value)))
		:child(pencere.spark(values, color):peak(60):w_full():h(36))
end

return pencere.div():row():gap(20)
	:child(card("CPU", "30%", cpu, "blue"))
	:child(card("Network", "3 MiB/s", net, "cyan"))

pencere.image(path)

A picture: a file of the view's own bundle (protocol, Section 9.8); a page of it, for a PDF: :page(n).

What pencere.image(path) draws What pencere.image(path) draws
return pencere.div():row():items_center():gap(20)
	:child(pencere.image("elements/assets/window.svg"):w(64):h(64):tint("accent"))
	:child(pencere.image("elements/assets/window.svg"):w(40):h(40):tint("foreground"))
	:child(pencere.image("elements/assets/window.svg"):w(24):h(24):tint("dim"))

pencere.rich(parts)

A text of many looks: its parts (pencere.span(words), or a string) one after the other, as one paragraph.

What pencere.rich(parts) draws What pencere.rich(parts) draws
return pencere.rich({
	"Build ",
	pencere.span("failed"):color("error"):bold(),
	" in ",
	pencere.span("src/main.rs"):link("file"),
	" at line 12: expected ",
	pencere.span("u32"):bg("raised2"),
	", found ",
	pencere.span("i64"):bg("raised2"),
	". The old build ",
	pencere.span("still runs"):italic(),
	".",
}):wrap():w(360)

pencere.canvas(paths)

A box with paths drawn in it (pencere.path()), in its own points. It has no size of its own.

What pencere.canvas(paths) draws What pencere.canvas(paths) draws
local values = { 14, 22, 18, 30, 26, 38, 34, 46, 40, 52, 48, 60 }

local line = pencere.path()
local area = pencere.path():move_to(0, 120)
for index, value in values do
	local x, y = (index - 1) * 28, 120 - value * 1.8
	if index == 1 then
		line:move_to(x, y)
	else
		line:line_to(x, y)
	end
	area:line_to(x, y)
end
area:line_to((#values - 1) * 28, 120):close()
return pencere.canvas({
	area:fill(pencere.alpha("accent", 0.15)),
	line:stroke(2, "accent"),
	pencere.path():circle((#values - 1) * 28, 120 - 60 * 1.8, 4):fill("accent"),
}):w(320):h(124)

pencere.editor(id, options)

The room of an editor of text the host keeps, lays out and draws (below); div():editor(id, options) the same.

What pencere.editor(id, options) draws What pencere.editor(id, options) draws
local Syntax = require("@pencere/syntax")
local languages = require("@pencere/languages")

local DOC = [[
local pencere = require("pencere")

pencere.serve {
  calls = {
    hello = function(params)
      return { message = "Hello, " .. params.name .. "!" }
    end,
  },
}]]

local language = languages.load("Lua")
local syntax = Syntax.new(language.syntax, "code")
local options = table.clone(language.config)
options.read_only = true

return pencere.editor("code", options):w(480):h(196)

pencere.spacer()

A box that takes the room left over.

What pencere.spacer() draws What pencere.spacer() draws
return pencere.div():w(360):row():items_center():px(12):py(8):border(1, "border"):rounded(6)
	:child(pencere.text("main"))
	:child(pencere.spacer())
	:child(pencere.text("Ln 12, Col 4"):color("dim"))

The builders

What every element takes is in one table of the page API, by what it is for.