cc-libs/programs/tuidemo.lua

212 lines
4.1 KiB
Lua

local createVersion = require('/apis/libversion');
local command = ...;
local function printUsage()
print('tuidemo usage:');
print();
print('\t\t\ttuidemo');
print('\t\t\ttuidemo version');
print('\t\t\ttuidemo help');
end
if command == 'version' or command == '-version' or command == '--version' then
print('v' .. createVersion().forSelf());
return;
end
if command == 'help' or command == '-help' or command == '--help' then
printUsage();
return;
end
if command ~= nil and command ~= '' then
printUsage();
return;
end
local createEventLoop = require('/apis/eventloop');
local createTui = require('/apis/libtui');
local eventloop = createEventLoop();
local ui = createTui(eventloop);
local Text = ui.Text;
local Button = ui.Button;
local Box = ui.Box;
local List = ui.List;
local page = 1;
local pageCount = 3;
local function previousPage()
page = page - 1;
if page < 1 then
page = pageCount;
end
ui.rerender();
end
local function nextPage()
page = page + 1;
if page > pageCount then
page = 1;
end
ui.rerender();
end
local function Header()
return Box({
direction = 'row',
bgColor = colors.gray,
children = {
Text('Trap UI Demo', {
flex = 1,
color = colors.white,
bgColor = colors.gray,
}),
Button('X', {
color = colors.white,
bgColor = colors.red,
onClick = function(tui)
tui.exitUI('closed');
end,
}),
},
});
end
local function PageText()
return List({
gap = 1,
padding = 1,
children = {
Text('Text components render strings inside their assigned rectangle.'),
Text('This line uses pink background and black foreground.', {
color = colors.black,
bgColor = colors.pink,
}),
Text('Resize the terminal to force a redraw.'),
},
});
end
local function PageLayout()
return Box({
direction = 'row',
gap = 1,
padding = 1,
children = {
Box({
flex = 1,
border = true,
title = 'Left',
children = {
Text('flex = 1'),
},
}),
Box({
width = 18,
border = true,
title = 'Fixed',
children = {
Text('width = 18'),
},
}),
Box({
flex = 2,
border = true,
title = 'Right',
children = {
Text('flex = 2'),
},
}),
},
});
end
local function PageButtons()
return List({
gap = 1,
padding = 1,
children = {
Text('Buttons are clickable hitboxes.'),
Button('Click me to go next', {
color = colors.black,
bgColor = colors.lime,
onClick = function()
nextPage();
end,
}),
Button('Exit demo', {
color = colors.white,
bgColor = colors.red,
onClick = function(tui)
tui.exitUI('button');
end,
}),
},
});
end
local function CurrentPage()
if page == 1 then
return PageText();
end
if page == 2 then
return PageLayout();
end
return PageButtons();
end
local function Footer()
return Box({
direction = 'row',
gap = 1,
children = {
Button('Previous', {
onClick = function()
previousPage();
end,
}),
Text('Page ' .. page .. '/' .. pageCount, { flex = 1 }),
Button('Next', {
onClick = function()
nextPage();
end,
}),
},
});
end
local function App()
return Box({
direction = 'column',
children = {
Header(),
Box({
flex = 1,
border = true,
title = 'Demo page ' .. page,
children = {
CurrentPage(),
},
}),
Footer(),
},
});
end
local finalEvent = ui.render(App);
if finalEvent.type == 'terminate' then
print('> User terminated the app');
elseif finalEvent.type == 'error' then
print('> error name: ' .. tostring(finalEvent.error.name));
print('> error reason/details: ' .. tostring(finalEvent.error.reason));
elseif finalEvent.type == 'exitUI' then
print('> User exited the app using the UI');
end