xref: /freebsd/stand/lua/screen.lua (revision 1f5696c7fd92009f7687b8c8eaf89c09eab72daa)
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");
30088b4f5fSWarner Loshlocal core = require("core");
31088b4f5fSWarner Losh
32*1f5696c7SKyle Evanslocal screen = {};
33*1f5696c7SKyle Evans
34901d96e3SKyle Evans-- XXX TODO: This should be fixed in the interpreter to not print decimals
35901d96e3SKyle Evansfunction intstring(num)
3624a1bd54SKyle Evans	local str = tostring(num);
3724a1bd54SKyle Evans	local decimal = str:find("%.");
38901d96e3SKyle Evans
3924a1bd54SKyle Evans	if (decimal) then
4024a1bd54SKyle Evans		return str:sub(1, decimal - 1);
41901d96e3SKyle Evans	end
4224a1bd54SKyle Evans	return str;
43901d96e3SKyle Evansend
44901d96e3SKyle Evans
45088b4f5fSWarner Loshfunction screen.clear()
46b140d14bSKyle Evans	if (core.isSerialBoot()) then
47088b4f5fSWarner Losh		return;
48088b4f5fSWarner Losh	end
49088b4f5fSWarner Losh	loader.printc("\027[H\027[J");
50088b4f5fSWarner Loshend
51088b4f5fSWarner Losh
52088b4f5fSWarner Loshfunction screen.setcursor(x, y)
53b140d14bSKyle Evans	if (core.isSerialBoot()) then
54088b4f5fSWarner Losh		return;
55088b4f5fSWarner Losh	end
56901d96e3SKyle Evans
57901d96e3SKyle Evans	loader.printc("\027[" .. intstring(y) .. ";" .. intstring(x) .. "H");
58088b4f5fSWarner Loshend
59088b4f5fSWarner Losh
60088b4f5fSWarner Loshfunction screen.setforeground(c)
6124a1bd54SKyle Evans	if (color.disabled) then
62088b4f5fSWarner Losh		return c;
63088b4f5fSWarner Losh	end
64088b4f5fSWarner Losh	loader.printc("\027[3" .. c .. "m");
65088b4f5fSWarner Loshend
66088b4f5fSWarner Losh
67088b4f5fSWarner Loshfunction screen.setbackground(c)
6824a1bd54SKyle Evans	if (color.disabled) then
69088b4f5fSWarner Losh		return c;
70088b4f5fSWarner Losh	end
71088b4f5fSWarner Losh	loader.printc("\027[4" .. c .. "m");
72088b4f5fSWarner Loshend
73088b4f5fSWarner Losh
74088b4f5fSWarner Loshfunction screen.defcolor()
75088b4f5fSWarner Losh	loader.printc(color.default());
76088b4f5fSWarner Loshend
77088b4f5fSWarner Losh
78088b4f5fSWarner Loshfunction screen.defcursor()
79b140d14bSKyle Evans	if (core.isSerialBoot()) then
80088b4f5fSWarner Losh		return;
81088b4f5fSWarner Losh	end
82088b4f5fSWarner Losh	loader.printc("\027[25;0H");
83088b4f5fSWarner Loshend
84088b4f5fSWarner Losh
8524a1bd54SKyle Evansreturn screen;
86