xref: /freebsd/stand/lua/drawer.lua (revision c851839897bd827c373c86aef1b6b9d0f1cc538a)
1088b4f5fSWarner Losh--
2088b4f5fSWarner Losh-- Copyright (c) 2015 Pedro Souza <pedrosouza@freebsd.org>
3088b4f5fSWarner Losh-- All rights reserved.
4088b4f5fSWarner Losh--
5088b4f5fSWarner Losh-- Redistribution and use in source and binary forms, with or without
6088b4f5fSWarner Losh-- modification, are permitted provided that the following conditions
7088b4f5fSWarner Losh-- are met:
8088b4f5fSWarner Losh-- 1. Redistributions of source code must retain the above copyright
9088b4f5fSWarner Losh--    notice, this list of conditions and the following disclaimer.
10088b4f5fSWarner Losh-- 2. Redistributions in binary form must reproduce the above copyright
11088b4f5fSWarner Losh--    notice, this list of conditions and the following disclaimer in the
12088b4f5fSWarner Losh--    documentation and/or other materials provided with the distribution.
13088b4f5fSWarner Losh--
14088b4f5fSWarner Losh-- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15088b4f5fSWarner Losh-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16088b4f5fSWarner Losh-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17088b4f5fSWarner Losh-- ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18088b4f5fSWarner Losh-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19088b4f5fSWarner Losh-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20088b4f5fSWarner Losh-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21088b4f5fSWarner Losh-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22088b4f5fSWarner Losh-- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23088b4f5fSWarner Losh-- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24088b4f5fSWarner Losh-- SUCH DAMAGE.
25088b4f5fSWarner Losh--
26088b4f5fSWarner Losh-- $FreeBSD$
27088b4f5fSWarner Losh--
28088b4f5fSWarner Losh
29088b4f5fSWarner Loshlocal color = require("color");
30a7cf0562SKyle Evanslocal core = require("core");
31088b4f5fSWarner Loshlocal screen = require("screen");
32088b4f5fSWarner Losh
33*c8518398SKyle Evanslocal drawer = {};
34*c8518398SKyle Evans
35088b4f5fSWarner Loshdrawer.brand_position = {x = 2, y = 1};
36088b4f5fSWarner Loshdrawer.fbsd_logo = {
37088b4f5fSWarner Losh	"  ______               ____   _____ _____  ",
38088b4f5fSWarner Losh	" |  ____|             |  _ \\ / ____|  __ \\ ",
39088b4f5fSWarner Losh	" | |___ _ __ ___  ___ | |_) | (___ | |  | |",
40088b4f5fSWarner Losh	" |  ___| '__/ _ \\/ _ \\|  _ < \\___ \\| |  | |",
41088b4f5fSWarner Losh	" | |   | | |  __/  __/| |_) |____) | |__| |",
42088b4f5fSWarner Losh	" | |   | | |    |    ||     |      |      |",
43088b4f5fSWarner Losh	" |_|   |_|  \\___|\\___||____/|_____/|_____/ "
44088b4f5fSWarner Losh};
45088b4f5fSWarner Losh
46088b4f5fSWarner Loshdrawer.logo_position = {x = 46, y = 1};
47088b4f5fSWarner Loshdrawer.beastie_color = {
48088b4f5fSWarner Losh	"               \027[31m,        ,",
49088b4f5fSWarner Losh	"              /(        )`",
50088b4f5fSWarner Losh	"              \\ \\___   / |",
51088b4f5fSWarner Losh	"              /- \027[37m_\027[31m  `-/  '",
52088b4f5fSWarner Losh	"             (\027[37m/\\/ \\\027[31m \\   /\\",
53088b4f5fSWarner Losh	"             \027[37m/ /   |\027[31m `    \\",
54088b4f5fSWarner Losh	"             \027[34mO O   \027[37m) \027[31m/    |",
55088b4f5fSWarner Losh	"             \027[37m`-^--'\027[31m`<     '",
56088b4f5fSWarner Losh	"            (_.)  _  )   /",
57088b4f5fSWarner Losh	"             `.___/`    /",
58088b4f5fSWarner Losh	"               `-----' /",
59088b4f5fSWarner Losh	"  \027[33m<----.\027[31m     __ / __   \\",
60088b4f5fSWarner Losh	"  \027[33m<----|====\027[31mO)))\027[33m==\027[31m) \\) /\027[33m====|",
61088b4f5fSWarner Losh	"  \027[33m<----'\027[31m    `--' `.__,' \\",
62088b4f5fSWarner Losh	"               |        |",
63088b4f5fSWarner Losh	"                \\       /       /\\",
64088b4f5fSWarner Losh	"           \027[36m______\027[31m( (_  / \\______/",
65088b4f5fSWarner Losh	"         \027[36m,'  ,-----'   |",
66088b4f5fSWarner Losh	"         `--{__________)\027[37m"
67088b4f5fSWarner Losh};
68088b4f5fSWarner Losh
69088b4f5fSWarner Loshdrawer.beastie = {
70088b4f5fSWarner Losh	"               ,        ,",
71088b4f5fSWarner Losh	"              /(        )`",
72088b4f5fSWarner Losh	"              \\ \\___   / |",
73088b4f5fSWarner Losh	"              /- _  `-/  '",
74088b4f5fSWarner Losh	"             (/\\/ \\ \\   /\\",
75088b4f5fSWarner Losh	"             / /   | `    \\",
76088b4f5fSWarner Losh	"             O O   ) /    |",
77088b4f5fSWarner Losh	"             `-^--'`<     '",
78088b4f5fSWarner Losh	"            (_.)  _  )   /",
79088b4f5fSWarner Losh	"             `.___/`    /",
80088b4f5fSWarner Losh	"               `-----' /",
81088b4f5fSWarner Losh	"  <----.     __ / __   \\",
82088b4f5fSWarner Losh	"  <----|====O)))==) \\) /====|",
83088b4f5fSWarner Losh	"  <----'    `--' `.__,' \\",
84088b4f5fSWarner Losh	"               |        |",
85088b4f5fSWarner Losh	"                \\       /       /\\",
86088b4f5fSWarner Losh	"           ______( (_  / \\______/",
87088b4f5fSWarner Losh	"         ,'  ,-----'   |",
88088b4f5fSWarner Losh	"         `--{__________)"
89088b4f5fSWarner Losh};
90088b4f5fSWarner Losh
91088b4f5fSWarner Loshdrawer.fbsd_logo_shift = {x = 5, y = 4};
92088b4f5fSWarner Loshdrawer.fbsd_logo_v = {
93088b4f5fSWarner Losh	"  ______",
94088b4f5fSWarner Losh	" |  ____| __ ___  ___ ",
95088b4f5fSWarner Losh	" | |__ | '__/ _ \\/ _ \\",
96088b4f5fSWarner Losh	" |  __|| | |  __/  __/",
97088b4f5fSWarner Losh	" | |   | | |    |    |",
98088b4f5fSWarner Losh	" |_|   |_|  \\___|\\___|",
99088b4f5fSWarner Losh	"  ____   _____ _____",
100088b4f5fSWarner Losh	" |  _ \\ / ____|  __ \\",
101088b4f5fSWarner Losh	" | |_) | (___ | |  | |",
102088b4f5fSWarner Losh	" |  _ < \\___ \\| |  | |",
103088b4f5fSWarner Losh	" | |_) |____) | |__| |",
104088b4f5fSWarner Losh	" |     |      |      |",
105088b4f5fSWarner Losh	" |____/|_____/|_____/"
106088b4f5fSWarner Losh};
107088b4f5fSWarner Losh
108088b4f5fSWarner Loshdrawer.orb_shift = {x = 2, y = 4};
109088b4f5fSWarner Loshdrawer.orb_color = {
110088b4f5fSWarner Losh	"  \027[31m```                        \027[31;1m`\027[31m",
111088b4f5fSWarner Losh	" s` `.....---...\027[31;1m....--.```   -/\027[31m",
112088b4f5fSWarner Losh	" +o   .--`         \027[31;1m/y:`      +.\027[31m",
113088b4f5fSWarner Losh	"  yo`:.            \027[31;1m:o      `+-\027[31m",
114088b4f5fSWarner Losh	"   y/               \027[31;1m-/`   -o/\027[31m",
115088b4f5fSWarner Losh	"  .-                  \027[31;1m::/sy+:.\027[31m",
116088b4f5fSWarner Losh	"  /                     \027[31;1m`--  /\027[31m",
117088b4f5fSWarner Losh	" `:                          \027[31;1m:`\027[31m",
118088b4f5fSWarner Losh	" `:                          \027[31;1m:`\027[31m",
119088b4f5fSWarner Losh	"  /                          \027[31;1m/\027[31m",
120088b4f5fSWarner Losh	"  .-                        \027[31;1m-.\027[31m",
121088b4f5fSWarner Losh	"   --                      \027[31;1m-.\027[31m",
122088b4f5fSWarner Losh	"    `:`                  \027[31;1m`:`",
123088b4f5fSWarner Losh	"      \027[31;1m.--             `--.",
124088b4f5fSWarner Losh	"         .---.....----.\027[37m"
125088b4f5fSWarner Losh};
126088b4f5fSWarner Losh
127088b4f5fSWarner Loshdrawer.orb = {
128088b4f5fSWarner Losh	"  ```                        `",
129088b4f5fSWarner Losh	" s` `.....---.......--.```   -/",
130088b4f5fSWarner Losh	" +o   .--`         /y:`      +.",
131088b4f5fSWarner Losh	"  yo`:.            :o      `+-",
132088b4f5fSWarner Losh	"   y/               -/`   -o/",
133088b4f5fSWarner Losh	"  .-                  ::/sy+:.",
134088b4f5fSWarner Losh	"  /                     `--  /",
135088b4f5fSWarner Losh	" `:                          :`",
136088b4f5fSWarner Losh	" `:                          :`",
137088b4f5fSWarner Losh	"  /                          /",
138088b4f5fSWarner Losh	"  .-                        -.",
139088b4f5fSWarner Losh	"   --                      -.",
140088b4f5fSWarner Losh	"    `:`                  `:`",
141088b4f5fSWarner Losh	"      .--             `--.",
142088b4f5fSWarner Losh	"         .---.....----."
143088b4f5fSWarner Losh};
144088b4f5fSWarner Losh
145088b4f5fSWarner Loshdrawer.none = {""};
146088b4f5fSWarner Losh
147088b4f5fSWarner Loshdrawer.none_shift = {x = 17, y = 0};
148088b4f5fSWarner Losh
149088b4f5fSWarner Loshdrawer.menu_position = {x = 6, y = 11};
150088b4f5fSWarner Losh
151088b4f5fSWarner Loshdrawer.box_pos_dim = {x = 3, y = 10, w = 41, h = 11};
152088b4f5fSWarner Losh
153088b4f5fSWarner Loshfunction drawer.drawscreen(menu_opts)
154088b4f5fSWarner Losh	-- drawlogo() must go first.
155088b4f5fSWarner Losh	-- it determines the positions of other elements
156088b4f5fSWarner Losh	drawer.drawlogo();
157088b4f5fSWarner Losh        drawer.drawbrand();
158088b4f5fSWarner Losh        drawer.drawbox();
159088b4f5fSWarner Losh	return drawer.drawmenu(menu_opts);
160088b4f5fSWarner Loshend
161088b4f5fSWarner Losh
162088b4f5fSWarner Loshfunction drawer.drawmenu(m)
163088b4f5fSWarner Losh	x = drawer.menu_position.x;
164088b4f5fSWarner Losh	y = drawer.menu_position.y;
165088b4f5fSWarner Losh
166088b4f5fSWarner Losh	-- print the menu and build the alias table
167088b4f5fSWarner Losh	local alias_table = {};
168088b4f5fSWarner Losh	local entry_num = 0;
169d8757746SKyle Evans	local menu_entries = m.entries;
1702e716cecSKyle Evans	if (type(menu_entries) == "function") then
1712e716cecSKyle Evans		menu_entries = menu_entries();
1722e716cecSKyle Evans	end
173d8757746SKyle Evans	for line_num, e in ipairs(menu_entries) do
1744a4fb4f8SKyle Evans		-- Allow menu items to be conditionally visible by specifying
1754a4fb4f8SKyle Evans		-- a visible function.
1764a4fb4f8SKyle Evans		if (e.visible ~= nil) and (not e.visible()) then
177ddb76e07SKyle Evans			goto continue;
1784a4fb4f8SKyle Evans		end
179a7cf0562SKyle Evans		if (e.entry_type ~= core.MENU_SEPARATOR) then
180088b4f5fSWarner Losh			entry_num = entry_num + 1;
181088b4f5fSWarner Losh			screen.setcursor(x, y + line_num);
182ada26c4aSKyle Evans			local name = "";
183ada26c4aSKyle Evans
184a7cf0562SKyle Evans			if (e.entry_type == core.MENU_CAROUSEL_ENTRY) then
185ada26c4aSKyle Evans				local carid = e.carousel_id;
186ada26c4aSKyle Evans				local caridx = menu.getCarouselIndex(carid);
187ada26c4aSKyle Evans				local choices = e.items();
188ada26c4aSKyle Evans
189ada26c4aSKyle Evans				if (#choices < caridx) then
190ada26c4aSKyle Evans					caridx = 1;
19124a1bd54SKyle Evans				end
192ada26c4aSKyle Evans				name = e.name(caridx, choices[caridx], choices);
193ada26c4aSKyle Evans			else
194ada26c4aSKyle Evans				name = e.name();
195ada26c4aSKyle Evans			end
196ada26c4aSKyle Evans			print(entry_num .. ". " .. name);
197088b4f5fSWarner Losh
198088b4f5fSWarner Losh			-- fill the alias table
199088b4f5fSWarner Losh			alias_table[tostring(entry_num)] = e;
200196ba166SKyle Evans			if (e.alias ~= nil) then
201088b4f5fSWarner Losh				for n, a in ipairs(e.alias) do
202088b4f5fSWarner Losh					alias_table[a] = e;
203088b4f5fSWarner Losh				end
204196ba166SKyle Evans			end
205088b4f5fSWarner Losh		else
206088b4f5fSWarner Losh			screen.setcursor(x, y + line_num);
207088b4f5fSWarner Losh			print(e.name());
208088b4f5fSWarner Losh		end
2094a4fb4f8SKyle Evans		::continue::
210088b4f5fSWarner Losh	end
211088b4f5fSWarner Losh	return alias_table;
212088b4f5fSWarner Loshend
213088b4f5fSWarner Losh
214088b4f5fSWarner Losh
215088b4f5fSWarner Loshfunction drawer.drawbox()
216088b4f5fSWarner Losh	x = drawer.box_pos_dim.x;
217088b4f5fSWarner Losh	y = drawer.box_pos_dim.y;
218088b4f5fSWarner Losh	w = drawer.box_pos_dim.w;
219088b4f5fSWarner Losh	h = drawer.box_pos_dim.h;
220088b4f5fSWarner Losh
221088b4f5fSWarner Losh	local hl = string.char(0xCD);
222088b4f5fSWarner Losh	local vl = string.char(0xBA);
223088b4f5fSWarner Losh
224088b4f5fSWarner Losh	local tl = string.char(0xC9);
225088b4f5fSWarner Losh	local bl = string.char(0xC8);
226088b4f5fSWarner Losh	local tr = string.char(0xBB);
227088b4f5fSWarner Losh	local br = string.char(0xBC);
228088b4f5fSWarner Losh
229088b4f5fSWarner Losh	screen.setcursor(x, y); print(tl);
230088b4f5fSWarner Losh	screen.setcursor(x, y+h); print(bl);
231088b4f5fSWarner Losh	screen.setcursor(x+w, y); print(tr);
232088b4f5fSWarner Losh	screen.setcursor(x+w, y+h); print(br);
233088b4f5fSWarner Losh
234088b4f5fSWarner Losh	for i = 1, w-1 do
235088b4f5fSWarner Losh		screen.setcursor(x+i, y);
236088b4f5fSWarner Losh		print(hl);
237088b4f5fSWarner Losh		screen.setcursor(x+i, y+h);
238088b4f5fSWarner Losh		print(hl);
239088b4f5fSWarner Losh	end
240088b4f5fSWarner Losh
241088b4f5fSWarner Losh	for i = 1, h-1 do
242088b4f5fSWarner Losh		screen.setcursor(x, y+i);
243088b4f5fSWarner Losh		print(vl);
244088b4f5fSWarner Losh		screen.setcursor(x+w, y+i);
245088b4f5fSWarner Losh		print(vl);
246088b4f5fSWarner Losh	end
247088b4f5fSWarner Losh
248088b4f5fSWarner Losh	screen.setcursor(x+(w/2)-9, y);
249088b4f5fSWarner Losh	print("Welcome to FreeBSD");
250088b4f5fSWarner Loshend
251088b4f5fSWarner Losh
252088b4f5fSWarner Loshfunction drawer.draw(x, y, logo)
253088b4f5fSWarner Losh	for i = 1, #logo do
254088b4f5fSWarner Losh		screen.setcursor(x, y + i);
255088b4f5fSWarner Losh		print(logo[i]);
256088b4f5fSWarner Losh	end
257088b4f5fSWarner Loshend
258088b4f5fSWarner Losh
259088b4f5fSWarner Loshfunction drawer.drawbrand()
26024a1bd54SKyle Evans	local x = tonumber(loader.getenv("loader_brand_x")) or
26124a1bd54SKyle Evans	    drawer.brand_position.x;
26224a1bd54SKyle Evans	local y = tonumber(loader.getenv("loader_brand_y")) or
26324a1bd54SKyle Evans	    drawer.brand_position.y;
264088b4f5fSWarner Losh
26524a1bd54SKyle Evans	local logo = load("return " .. tostring(loader.getenv("loader_brand")))() or
26624a1bd54SKyle Evans	    drawer.fbsd_logo;
267088b4f5fSWarner Losh	drawer.draw(x, y, logo);
268088b4f5fSWarner Loshend
269088b4f5fSWarner Losh
270088b4f5fSWarner Loshfunction drawer.drawlogo()
27124a1bd54SKyle Evans	local x = tonumber(loader.getenv("loader_logo_x")) or
27224a1bd54SKyle Evans	    drawer.logo_position.x;
27324a1bd54SKyle Evans	local y = tonumber(loader.getenv("loader_logo_y")) or
27424a1bd54SKyle Evans	    drawer.logo_position.y;
275088b4f5fSWarner Losh
276088b4f5fSWarner Losh	local logo = loader.getenv("loader_logo");
277088b4f5fSWarner Losh	local s = {x = 0, y = 0};
278088b4f5fSWarner Losh	local colored = color.isEnabled();
279088b4f5fSWarner Losh
28024a1bd54SKyle Evans	if (logo == "beastie") then
28124a1bd54SKyle Evans		if (colored) then
282088b4f5fSWarner Losh			logo = drawer.beastie_color;
283088b4f5fSWarner Losh		end
28424a1bd54SKyle Evans	elseif (logo == "beastiebw") then
285088b4f5fSWarner Losh		logo = drawer.beastie;
28624a1bd54SKyle Evans	elseif (logo == "fbsdbw") then
287088b4f5fSWarner Losh		logo = drawer.fbsd_logo_v;
288088b4f5fSWarner Losh		s = drawer.fbsd_logo_shift;
28924a1bd54SKyle Evans	elseif (logo == "orb") then
29024a1bd54SKyle Evans		if (colored) then
291088b4f5fSWarner Losh			logo = drawer.orb_color;
292088b4f5fSWarner Losh		end
293088b4f5fSWarner Losh		s = drawer.orb_shift;
29424a1bd54SKyle Evans	elseif (logo == "orbbw") then
295088b4f5fSWarner Losh		logo = drawer.orb;
296088b4f5fSWarner Losh		s = drawer.orb_shift;
29724a1bd54SKyle Evans	elseif (logo == "tribute") then
298088b4f5fSWarner Losh		logo = drawer.fbsd_logo;
29924a1bd54SKyle Evans	elseif (logo == "tributebw") then
300088b4f5fSWarner Losh		logo = drawer.fbsd_logo;
30124a1bd54SKyle Evans	elseif (logo == "none") then
302088b4f5fSWarner Losh		--centre brand and text if no logo
303088b4f5fSWarner Losh		drawer.brand_position.x = drawer.brand_position.x + drawer.none_shift.x;
304088b4f5fSWarner Losh		drawer.brand_position.y = drawer.brand_position.y + drawer.none_shift.y;
305088b4f5fSWarner Losh		drawer.menu_position.x = drawer.menu_position.x + drawer.none_shift.x;
306088b4f5fSWarner Losh		drawer.menu_position.y = drawer.menu_position.y + drawer.none_shift.y;
307088b4f5fSWarner Losh		drawer.box_pos_dim.x = drawer.box_pos_dim.x + drawer.none_shift.x;
308088b4f5fSWarner Losh		drawer.box_pos_dim.y = drawer.box_pos_dim.y + drawer.none_shift.y;
309088b4f5fSWarner Losh		--prevent redraws from moving menu further
310088b4f5fSWarner Losh		drawer.none_shift.x = 0;
311088b4f5fSWarner Losh		drawer.none_shift.y = 0;
312088b4f5fSWarner Losh		logo = drawer.none;
313088b4f5fSWarner Losh	end
31424a1bd54SKyle Evans	if (not logo) then
31524a1bd54SKyle Evans		if (colored) then
316088b4f5fSWarner Losh			logo = drawer.orb_color;
317088b4f5fSWarner Losh		else
318088b4f5fSWarner Losh			logo = drawer.orb;
319088b4f5fSWarner Losh		end
320088b4f5fSWarner Losh	end
321088b4f5fSWarner Losh	drawer.draw(x + s.x, y + s.y, logo);
322088b4f5fSWarner Loshend
323088b4f5fSWarner Losh
32424a1bd54SKyle Evansreturn drawer;
325