Lua runner with rich builtin stdlib
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
a/lua/tui_styles.lua

79 lines
4.1 KiB

-- Showcase of the tui module's output features: styled markup, message
-- blocks, outline blocks, and the raw/screenInfo building blocks.
-- Non-interactive — safe to run piped (colors degrade to plain text).
--
-- Run with: cargo run -- lua/tui_styles.lua
local function banner(s)
print("\n=== " .. s .. " ===")
end
----------------------------------------------------------------------
banner("1. tui.styled — built-in tags")
----------------------------------------------------------------------
print(tui.styled("<info>info is green</info>, <comment>comment is yellow</comment>"))
print(tui.styled("<error> error: white on red </error> <question> question: black on cyan </question>"))
----------------------------------------------------------------------
banner("2. Inline styles: fg / bg / options")
----------------------------------------------------------------------
print(tui.styled("<fg=magenta>named colors</>, <fg=bright-cyan>bright variants</>, <fg=gray>gray</>"))
print(tui.styled("<fg=#c0392b>hex truecolor #c0392b</> and <fg=#0af>short #0af</>"))
print(tui.styled("<bg=blue>background</> <options=bold>bold</> <options=underscore>underscore</>"))
print(tui.styled("<fg=black;bg=green;options=bold> all combined in one tag </>"))
----------------------------------------------------------------------
banner("3. Nesting, links, escaping")
----------------------------------------------------------------------
-- Inner tags override only what they set; the rest is inherited.
print(tui.styled("<error>outer <options=bold>bold inherits the red bg</> and back</error>"))
-- </> closes the innermost style; fg=default resets to the terminal color.
print(tui.styled("<fg=red>red <fg=default>terminal default</> red again</>"))
-- OSC-8 hyperlink (clickable in supporting terminals, plain text elsewhere).
print(tui.styled("read <href=https://example.com/docs>the docs</> for details"))
-- \< escapes a bracket; unknown tags pass through literally, never error.
print(tui.styled("literal \\<info> stays, <not-a-tag> too, a < b as well"))
----------------------------------------------------------------------
banner("4. Message blocks (Symfony style)")
----------------------------------------------------------------------
tui.success("Deployment finished without errors")
tui.warning({ "Disk usage above 80%", "Consider pruning old artifacts" })
tui.error("Build failed")
tui.caution("This will overwrite production data")
tui.info("14 files processed")
tui.note("Long messages word-wrap to the terminal width and continuation lines stay aligned under the label")
tui.comment("git push --force-with-lease")
-- The generic form: everything is configurable.
tui.block(
"Custom label, style and prefix",
{ label = "HEY", style = "fg=black;bg=cyan", prefix = " > ", padding = true }
)
----------------------------------------------------------------------
banner("5. Outline blocks (colored borders, plain content)")
----------------------------------------------------------------------
tui.outlineSuccess("All tests passed")
tui.outlineWarning("3 deprecation warnings", { title = "Heads up" }) -- opts.title overrides the preset
tui.outlineBlock({ "Generic box.", "Multiple messages are separated by a blank line." }, {
title = "Stats",
style = "fg=magenta",
})
----------------------------------------------------------------------
banner("6. tui.raw + tui.screenInfo — progress bar building blocks")
----------------------------------------------------------------------
local info = tui.screenInfo()
print("screen:", (info.width or "?") .. "x" .. (info.height or "?"), "tty:", info.tty, "colors:", info.colors)
-- A minimal in-place progress bar: \r rewinds the line, tui.raw prints
-- without a newline and flushes. Width adapts to the terminal; without a
-- tty the \r redraw trick is pointless, so print the final state once.
local width = math.clamp((info.width or 80) - 20, 10, 40)
local start = info.tty and 0 or width
for i = start, width do
local bar = string.rep("#", i) .. string.rep(".", width - i)
tui.raw("\r[", bar, "] ", math.floor(i / width * 100 + 0.5), "%")
os.sleep(0.02)
end
tui.raw("\n")