Download
62 lines 1.8 KiBLuaModified 2026-04-05 raw
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
#!/usr/bin/env lua
--
-- This file contains a basic example of BTUI Lua usage.
--
local btui = require("btui")

btui(function(bt)
    local key = nil
    local x, y = 1, 1
    while key ~= "q" and key ~= "Ctrl-c" do
        if key == "?" then
            bt:withdisabled(function()
                io.write("Press enter to continue.")
                io.flush()
                io.read()
            end)
        elseif key == "Ctrl-z" then
            bt:suspend()
        end

        bt:batch(function()
            bt:clear()
            bt:move(15, 15)

            bt:withfg(.8,.95,.2, function()
                bt:linebox(x, y, 30, 1);
                bt:move(x, y)
                bt:write("Pressed: ", key)
            end)

            local title = "Lua BTUI Demo"
            local w = #title
            local center = math.floor((bt:width() - w) / 2)
            bt:withattributes("fg_blue", "bg_normal", function()
                bt:shadow(center-2, 0, w+4, 3)
            end)
            bt:withattributes("bg_blue", "fg_black", function()
                bt:fillbox(center-2, 0, w+4, 3)
                bt:move(center, 1)
                bt:write(title)
            end)

            bt:withattributes("bold", function()
                bt:move(0, bt:height()-1)
                bt:write("Press 'q' to quit")
            end)

            bt:withattributes("faint", function()
                local s = ("Size: (%dx%d)"):format(bt:width(), bt:height())
                bt:move(bt:width()-#s, bt:height()-1)
                bt:write(s)
            end)
        end)

        local mouse_x, mouse_y
        key, mouse_x, mouse_y = bt:getkey()
        if mouse_x then x, y = mouse_x, mouse_y end
    end
    if key == "Ctrl-c" then
        error("Interrupt received!")
    end
end)