u.nvim/examples/form.lua
Jonathan Apodaca 75caf0444d
All checks were successful
NeoVim tests / code-quality (push) Successful in 1m26s
add example/form.lua
2025-06-14 00:28:49 -06:00

44 lines
1.4 KiB
Lua

local h = require('u.renderer').h
local Renderer = require('u.renderer').Renderer
local tracker = require 'u.tracker'
-- Create a new, temporary, buffer to the side:
vim.cmd.vnew()
vim.bo.buftype = 'nofile'
vim.bo.bufhidden = 'wipe'
vim.bo.buflisted = false
local renderer = Renderer.new()
-- Create two signals:
local s_name = tracker.create_signal '[(name here)]'
local s_age = tracker.create_signal '[(age here)]'
-- Utility to trim brackets from strings:
local function trimb(s) return (s:gsub('^%[(.*)%]$', '%1')) end
-- Render effect that depends on the signals. This will re-run every time
-- one of the signals changes.
tracker.create_effect(function()
local name = s_name:get()
local age = s_age:get()
-- Each time the signals change, we re-render the buffer:
renderer:render {
h.Type({}, '# Form Example'),
'\n\n',
-- Setting the `signal` attribute to a signal will cause the renderer to
-- update the signal's value whenever text in the tag is changed. This lets
-- us setup two-way data-binding between the buffer and the signals.
{ 'Name: ', h.Structure({ signal = s_name }, name) },
'\n',
{ 'Age: ', h.Structure({ signal = s_age }, age) },
'\n\n',
-- Display the current values of the signals. If you change the values in
-- the tags above, you can see the changes reflected here immediately.
{ 'Hello, "', trimb(name), '", you are "', trimb(age), '" years old.' },
}
end)