]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - stand/lua/drawer.lua
stand/lua: Move drawer.menu_name_handlers further up
[FreeBSD/FreeBSD.git] / stand / lua / drawer.lua
1 --
2 -- Copyright (c) 2015 Pedro Souza <pedrosouza@freebsd.org>
3 -- All rights reserved.
4 --
5 -- Redistribution and use in source and binary forms, with or without
6 -- modification, are permitted provided that the following conditions
7 -- are met:
8 -- 1. Redistributions of source code must retain the above copyright
9 --    notice, this list of conditions and the following disclaimer.
10 -- 2. Redistributions in binary form must reproduce the above copyright
11 --    notice, this list of conditions and the following disclaimer in the
12 --    documentation and/or other materials provided with the distribution.
13 --
14 -- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 -- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 -- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 -- ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 -- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 -- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 -- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 -- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 -- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 -- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 -- SUCH DAMAGE.
25 --
26 -- $FreeBSD$
27 --
28
29 local color = require("color");
30 local core = require("core");
31 local screen = require("screen");
32
33 local drawer = {};
34
35 drawer.menu_name_handlers = {
36         -- Menu name handlers should take the menu being drawn and entry being
37         -- drawn as parameters, and return the name of the item.
38         -- This is designed so that everything, including menu separators, may
39         -- have their names derived differently. The default action for entry
40         -- types not specified here is to call and use entry.name().
41         [core.MENU_CAROUSEL_ENTRY] = function(drawing_menu, entry)
42                 local carid = entry.carousel_id;
43                 local caridx = menu.getCarouselIndex(carid);
44                 local choices = entry.items();
45
46                 if (#choices < caridx) then
47                         caridx = 1;
48                 end
49                 return entry.name(caridx, choices[caridx], choices);
50         end,
51 };
52
53 drawer.brand_position = {x = 2, y = 1};
54 drawer.fbsd_logo = {
55         "  ______               ____   _____ _____  ",
56         " |  ____|             |  _ \\ / ____|  __ \\ ",
57         " | |___ _ __ ___  ___ | |_) | (___ | |  | |",
58         " |  ___| '__/ _ \\/ _ \\|  _ < \\___ \\| |  | |",
59         " | |   | | |  __/  __/| |_) |____) | |__| |",
60         " | |   | | |    |    ||     |      |      |",
61         " |_|   |_|  \\___|\\___||____/|_____/|_____/ "
62 };
63
64 drawer.logo_position = {x = 46, y = 1};
65 drawer.beastie_color = {
66         "               \027[31m,        ,",
67         "              /(        )`",
68         "              \\ \\___   / |",
69         "              /- \027[37m_\027[31m  `-/  '",
70         "             (\027[37m/\\/ \\\027[31m \\   /\\",
71         "             \027[37m/ /   |\027[31m `    \\",
72         "             \027[34mO O   \027[37m) \027[31m/    |",
73         "             \027[37m`-^--'\027[31m`<     '",
74         "            (_.)  _  )   /",
75         "             `.___/`    /",
76         "               `-----' /",
77         "  \027[33m<----.\027[31m     __ / __   \\",
78         "  \027[33m<----|====\027[31mO)))\027[33m==\027[31m) \\) /\027[33m====|",
79         "  \027[33m<----'\027[31m    `--' `.__,' \\",
80         "               |        |",
81         "                \\       /       /\\",
82         "           \027[36m______\027[31m( (_  / \\______/",
83         "         \027[36m,'  ,-----'   |",
84         "         `--{__________)\027[37m"
85 };
86
87 drawer.beastie = {
88         "               ,        ,",
89         "              /(        )`",
90         "              \\ \\___   / |",
91         "              /- _  `-/  '",
92         "             (/\\/ \\ \\   /\\",
93         "             / /   | `    \\",
94         "             O O   ) /    |",
95         "             `-^--'`<     '",
96         "            (_.)  _  )   /",
97         "             `.___/`    /",
98         "               `-----' /",
99         "  <----.     __ / __   \\",
100         "  <----|====O)))==) \\) /====|",
101         "  <----'    `--' `.__,' \\",
102         "               |        |",
103         "                \\       /       /\\",
104         "           ______( (_  / \\______/",
105         "         ,'  ,-----'   |",
106         "         `--{__________)"
107 };
108
109 drawer.fbsd_logo_shift = {x = 5, y = 4};
110 drawer.fbsd_logo_v = {
111         "  ______",
112         " |  ____| __ ___  ___ ",
113         " | |__ | '__/ _ \\/ _ \\",
114         " |  __|| | |  __/  __/",
115         " | |   | | |    |    |",
116         " |_|   |_|  \\___|\\___|",
117         "  ____   _____ _____",
118         " |  _ \\ / ____|  __ \\",
119         " | |_) | (___ | |  | |",
120         " |  _ < \\___ \\| |  | |",
121         " | |_) |____) | |__| |",
122         " |     |      |      |",
123         " |____/|_____/|_____/"
124 };
125
126 drawer.orb_shift = {x = 2, y = 4};
127 drawer.orb_color = {
128         "  \027[31m```                        \027[31;1m`\027[31m",
129         " s` `.....---...\027[31;1m....--.```   -/\027[31m",
130         " +o   .--`         \027[31;1m/y:`      +.\027[31m",
131         "  yo`:.            \027[31;1m:o      `+-\027[31m",
132         "   y/               \027[31;1m-/`   -o/\027[31m",
133         "  .-                  \027[31;1m::/sy+:.\027[31m",
134         "  /                     \027[31;1m`--  /\027[31m",
135         " `:                          \027[31;1m:`\027[31m",
136         " `:                          \027[31;1m:`\027[31m",
137         "  /                          \027[31;1m/\027[31m",
138         "  .-                        \027[31;1m-.\027[31m",
139         "   --                      \027[31;1m-.\027[31m",
140         "    `:`                  \027[31;1m`:`",
141         "      \027[31;1m.--             `--.",
142         "         .---.....----.\027[37m"
143 };
144
145 drawer.orb = {
146         "  ```                        `",
147         " s` `.....---.......--.```   -/",
148         " +o   .--`         /y:`      +.",
149         "  yo`:.            :o      `+-",
150         "   y/               -/`   -o/",
151         "  .-                  ::/sy+:.",
152         "  /                     `--  /",
153         " `:                          :`",
154         " `:                          :`",
155         "  /                          /",
156         "  .-                        -.",
157         "   --                      -.",
158         "    `:`                  `:`",
159         "      .--             `--.",
160         "         .---.....----."
161 };
162
163 drawer.none = {""};
164
165 drawer.none_shift = {x = 17, y = 0};
166
167 drawer.menu_position = {x = 6, y = 11};
168
169 drawer.box_pos_dim = {x = 3, y = 10, w = 41, h = 11};
170
171 function drawer.drawscreen(menu_opts)
172         -- drawlogo() must go first.
173         -- it determines the positions of other elements
174         drawer.drawlogo();
175         drawer.drawbrand();
176         drawer.drawbox();
177         return drawer.drawmenu(menu_opts);
178 end
179
180 function menu_entry_name(drawing_menu, entry)
181         local name_handler = drawer.menu_name_handlers[entry.entry_type];
182
183         if (name_handler ~= nil) then
184                 return name_handler(drawing_menu, entry);
185         end
186         return entry.name();
187 end
188
189 function drawer.drawmenu(m)
190         x = drawer.menu_position.x;
191         y = drawer.menu_position.y;
192
193         -- print the menu and build the alias table
194         local alias_table = {};
195         local entry_num = 0;
196         local menu_entries = m.entries;
197         if (type(menu_entries) == "function") then
198                 menu_entries = menu_entries();
199         end
200         for line_num, e in ipairs(menu_entries) do
201                 -- Allow menu items to be conditionally visible by specifying
202                 -- a visible function.
203                 if (e.visible ~= nil) and (not e.visible()) then
204                         goto continue;
205                 end
206                 if (e.entry_type ~= core.MENU_SEPARATOR) then
207                         entry_num = entry_num + 1;
208                         screen.setcursor(x, y + line_num);
209
210                         print(entry_num .. ". " .. menu_entry_name(m, e));
211
212                         -- fill the alias table
213                         alias_table[tostring(entry_num)] = e;
214                         if (e.alias ~= nil) then
215                                 for n, a in ipairs(e.alias) do
216                                         alias_table[a] = e;
217                                 end
218                         end
219                 else
220                         screen.setcursor(x, y + line_num);
221                         print(menu_entry_name(m, e));
222                 end
223                 ::continue::
224         end
225         return alias_table;
226 end
227
228
229 function drawer.drawbox()
230         x = drawer.box_pos_dim.x;
231         y = drawer.box_pos_dim.y;
232         w = drawer.box_pos_dim.w;
233         h = drawer.box_pos_dim.h;
234
235         local hl = string.char(0xCD);
236         local vl = string.char(0xBA);
237
238         local tl = string.char(0xC9);
239         local bl = string.char(0xC8);
240         local tr = string.char(0xBB);
241         local br = string.char(0xBC);
242
243         screen.setcursor(x, y); print(tl);
244         screen.setcursor(x, y+h); print(bl);
245         screen.setcursor(x+w, y); print(tr);
246         screen.setcursor(x+w, y+h); print(br);
247
248         for i = 1, w-1 do
249                 screen.setcursor(x+i, y);
250                 print(hl);
251                 screen.setcursor(x+i, y+h);
252                 print(hl);
253         end
254
255         for i = 1, h-1 do
256                 screen.setcursor(x, y+i);
257                 print(vl);
258                 screen.setcursor(x+w, y+i);
259                 print(vl);
260         end
261
262         screen.setcursor(x+(w/2)-9, y);
263         print("Welcome to FreeBSD");
264 end
265
266 function drawer.draw(x, y, logo)
267         for i = 1, #logo do
268                 screen.setcursor(x, y + i);
269                 print(logo[i]);
270         end
271 end
272
273 function drawer.drawbrand()
274         local x = tonumber(loader.getenv("loader_brand_x")) or
275             drawer.brand_position.x;
276         local y = tonumber(loader.getenv("loader_brand_y")) or
277             drawer.brand_position.y;
278
279         local logo = load("return " .. tostring(loader.getenv("loader_brand")))() or
280             drawer.fbsd_logo;
281         drawer.draw(x, y, logo);
282 end
283
284 function drawer.drawlogo()
285         local x = tonumber(loader.getenv("loader_logo_x")) or
286             drawer.logo_position.x;
287         local y = tonumber(loader.getenv("loader_logo_y")) or
288             drawer.logo_position.y;
289
290         local logo = loader.getenv("loader_logo");
291         local s = {x = 0, y = 0};
292         local colored = color.isEnabled();
293
294         if (logo == "beastie") then
295                 if (colored) then
296                         logo = drawer.beastie_color;
297                 end
298         elseif (logo == "beastiebw") then
299                 logo = drawer.beastie;
300         elseif (logo == "fbsdbw") then
301                 logo = drawer.fbsd_logo_v;
302                 s = drawer.fbsd_logo_shift;
303         elseif (logo == "orb") then
304                 if (colored) then
305                         logo = drawer.orb_color;
306                 end
307                 s = drawer.orb_shift;
308         elseif (logo == "orbbw") then
309                 logo = drawer.orb;
310                 s = drawer.orb_shift;
311         elseif (logo == "tribute") then
312                 logo = drawer.fbsd_logo;
313         elseif (logo == "tributebw") then
314                 logo = drawer.fbsd_logo;
315         elseif (logo == "none") then
316                 --centre brand and text if no logo
317                 drawer.brand_position.x = drawer.brand_position.x + drawer.none_shift.x;
318                 drawer.brand_position.y = drawer.brand_position.y + drawer.none_shift.y;
319                 drawer.menu_position.x = drawer.menu_position.x + drawer.none_shift.x;
320                 drawer.menu_position.y = drawer.menu_position.y + drawer.none_shift.y;
321                 drawer.box_pos_dim.x = drawer.box_pos_dim.x + drawer.none_shift.x;
322                 drawer.box_pos_dim.y = drawer.box_pos_dim.y + drawer.none_shift.y;
323                 --prevent redraws from moving menu further
324                 drawer.none_shift.x = 0;
325                 drawer.none_shift.y = 0;
326                 logo = drawer.none;
327         end
328         if (not logo) then
329                 if (colored) then
330                         logo = drawer.orb_color;
331                 else
332                         logo = drawer.orb;
333                 end
334         end
335         drawer.draw(x + s.x, y + s.y, logo);
336 end
337
338 return drawer;