xref: /freebsd/stand/lua/screen.lua (revision 9f71d421c89dde4e5a642f4555bcd20558fd91b0)
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
29aedd6be5SKyle Evanslocal color = require("color")
30aedd6be5SKyle Evanslocal core = require("core")
31088b4f5fSWarner Losh
32aedd6be5SKyle Evanslocal screen = {}
331f5696c7SKyle Evans
34901d96e3SKyle Evans-- XXX TODO: This should be fixed in the interpreter to not print decimals
35e15abd1fSKyle Evanslocal intstring = function(num)
36aedd6be5SKyle Evans	local str = tostring(num)
37aedd6be5SKyle Evans	local decimal = str:find("%.")
38901d96e3SKyle Evans
39*9f71d421SKyle Evans	if decimal then
40aedd6be5SKyle Evans		return str:sub(1, decimal - 1)
41901d96e3SKyle Evans	end
42aedd6be5SKyle Evans	return str
43901d96e3SKyle Evansend
44901d96e3SKyle Evans
45b5746545SKyle Evans-- Module exports
46088b4f5fSWarner Loshfunction screen.clear()
47*9f71d421SKyle Evans	if core.isSerialBoot() then
48aedd6be5SKyle Evans		return
49088b4f5fSWarner Losh	end
50aedd6be5SKyle Evans	loader.printc("\027[H\027[J")
51088b4f5fSWarner Loshend
52088b4f5fSWarner Losh
53088b4f5fSWarner Loshfunction screen.setcursor(x, y)
54*9f71d421SKyle Evans	if core.isSerialBoot() then
55aedd6be5SKyle Evans		return
56088b4f5fSWarner Losh	end
57901d96e3SKyle Evans
58aedd6be5SKyle Evans	loader.printc("\027[" .. intstring(y) .. ";" .. intstring(x) .. "H")
59088b4f5fSWarner Loshend
60088b4f5fSWarner Losh
61088b4f5fSWarner Loshfunction screen.setforeground(c)
62*9f71d421SKyle Evans	if color.disabled then
63aedd6be5SKyle Evans		return c
64088b4f5fSWarner Losh	end
65aedd6be5SKyle Evans	loader.printc("\027[3" .. c .. "m")
66088b4f5fSWarner Loshend
67088b4f5fSWarner Losh
68088b4f5fSWarner Loshfunction screen.setbackground(c)
69*9f71d421SKyle Evans	if color.disabled then
70aedd6be5SKyle Evans		return c
71088b4f5fSWarner Losh	end
72aedd6be5SKyle Evans	loader.printc("\027[4" .. c .. "m")
73088b4f5fSWarner Loshend
74088b4f5fSWarner Losh
75088b4f5fSWarner Loshfunction screen.defcolor()
76aedd6be5SKyle Evans	loader.printc(color.default())
77088b4f5fSWarner Loshend
78088b4f5fSWarner Losh
79088b4f5fSWarner Loshfunction screen.defcursor()
80*9f71d421SKyle Evans	if core.isSerialBoot() then
81aedd6be5SKyle Evans		return
82088b4f5fSWarner Losh	end
83aedd6be5SKyle Evans	loader.printc("\027[25;0H")
84088b4f5fSWarner Loshend
85088b4f5fSWarner Losh
86aedd6be5SKyle Evansreturn screen
87