code / btui

Lines2.2K C1.2K Python549 Markdown219 make156 Lua49
(59 lines)
1 #!/usr/bin/env lua
2 --
3 -- This file contains a basic example of BTUI Lua usage.
4 --
5 local btui = require("btui")
7 btui(function(bt)
8 local key = nil
9 local x, y = 1, 1
10 while key ~= "q" and key ~= "Ctrl-c" do
11 if key == "?" then
12 bt:withdisabled(function()
13 io.write("Press enter to continue.")
14 io.flush()
15 io.read()
16 end)
17 elseif key == "Ctrl-z" then
18 bt:suspend()
19 end
21 bt:clear()
23 bt:withfg(.8,.95,.2, function()
24 bt:linebox(x, y, 30, 1);
25 bt:move(x, y)
26 bt:write("Pressed: ", key)
27 end)
29 local title = "Lua BTUI Demo"
30 local w = #title
31 local center = math.floor((bt:width() - w) / 2)
32 bt:withattributes("fg_blue", "bg_normal", function()
33 bt:shadow(center-2, 0, w+4, 3)
34 end)
35 bt:withattributes("bg_blue", "fg_black", function()
36 bt:fillbox(center-2, 0, w+4, 3)
37 bt:move(center, 1)
38 bt:write(title)
39 end)
41 bt:withattributes("bold", function()
42 bt:move(0, bt:height()-1)
43 bt:write("Press 'q' to quit")
44 end)
46 bt:withattributes("faint", function()
47 local s = ("Size: (%dx%d)"):format(bt:width(), bt:height())
48 bt:move(bt:width()-#s, bt:height()-1)
49 bt:write(s)
50 end)
52 local mouse_x, mouse_y
53 key, mouse_x, mouse_y = bt:getkey()
54 if mouse_x then x, y = mouse_x, mouse_y end
55 end
56 if key == "Ctrl-c" then
57 error("Interrupt received!")
58 end
59 end)