can now use named colors

This commit is contained in:
kepler155c@gmail.com 2020-04-21 22:32:12 -06:00
parent e703c7f7b6
commit 5933f8c40f
42 changed files with 308 additions and 400 deletions

View File

@ -1,7 +1,6 @@
local UI = require('opus.ui')
local Util = require('opus.util')
local colors = _G.colors
local help = _G.help
UI:configure('Help', ...)
@ -12,11 +11,11 @@ for _,topic in pairs(help.topics()) do
end
UI:addPage('main', UI.Page {
labelText = UI.Text {
UI.Text {
x = 3, y = 2,
value = 'Search',
},
filter = UI.TextEntry {
UI.TextEntry {
x = 10, y = 2, ex = -3,
limit = 32,
},
@ -38,9 +37,7 @@ UI:addPage('main', UI.Page {
elseif event.type == 'grid_select' then
if self.grid:getSelected() then
local name = self.grid:getSelected().name
UI:setPage('topic', name)
UI:setPage('topic', self.grid:getSelected().name)
end
elseif event.type == 'text_change' then
@ -57,6 +54,7 @@ UI:addPage('main', UI.Page {
self.grid:update()
self.grid:setIndex(1)
self.grid:draw()
else
return UI.Page.eventHandler(self, event)
end
@ -64,13 +62,12 @@ UI:addPage('main', UI.Page {
})
UI:addPage('topic', UI.Page {
backgroundColor = colors.black,
backgroundColor = 'black',
titleBar = UI.TitleBar {
title = 'text',
event = 'back',
},
helpText = UI.TextArea {
backgroundColor = colors.black,
x = 2, ex = -1, y = 3, ey = -2,
},
accelerators = {

View File

@ -58,14 +58,14 @@ local page = UI.Page {
},
[2] = UI.Tab {
tabTitle = 'Output',
backgroundColor = colors.black,
backgroundColor = 'black',
output = UI.Embedded {
y = 2,
maxScroll = 1000,
backgroundColor = colors.black,
backgroundColor = 'black',
},
draw = function(self)
self:write(1, 1, string.rep('\131', self.width), colors.black, UI.colors.primary)
self:write(1, 1, string.rep('\131', self.width), 'black', 'primary')
self:drawChildren()
end,
},
@ -162,7 +162,7 @@ function page:eventHandler(event)
local sz = #value
local pos = self.prompt.entry.pos
self:setPrompt(autocomplete(sandboxEnv, value, self.prompt.entry.pos))
self.prompt:setPosition(pos + #value - sz)
self.prompt:setPosition(pos + #(self.prompt.value or '') - sz)
self.prompt:updateCursor()
elseif event.type == 'device' then
@ -201,7 +201,6 @@ function page:eventHandler(event)
command = nil
self.grid:setValues(t)
self.grid:setIndex(1)
self.grid:adjustWidth()
self:draw()
end
return true
@ -248,7 +247,6 @@ function page:setResult(result)
end
self.grid:setValues(t)
self.grid:setIndex(1)
self.grid:adjustWidth()
self:draw()
end

View File

@ -4,7 +4,6 @@ local Socket = require('opus.socket')
local UI = require('opus.ui')
local Util = require('opus.util')
local colors = _G.colors
local device = _G.device
local network = _G.network
local os = _G.os
@ -58,7 +57,7 @@ local page = UI.Page {
autospace = true,
getRowTextColor = function(self, row, selected)
if not row.active then
return colors.lightGray
return 'lightGray'
end
return UI.Grid.getRowTextColor(self, row, selected)
end,

View File

@ -10,7 +10,6 @@ local Tween = require('opus.ui.tween')
local UI = require('opus.ui')
local Util = require('opus.util')
local colors = _G.colors
local device = _G.device
local fs = _G.fs
local os = _G.os
@ -94,8 +93,8 @@ local page = UI.Page {
tabBar = UI.TabBar {
ey = -2,
width = 8,
selectedBackgroundColor = UI.colors.primary,
backgroundColor = UI.colors.tertiary,
selectedBackgroundColor = 'primary',
backgroundColor = 'tertiary',
layout = function(self)
self.height = nil
UI.TabBar.layout(self)
@ -103,7 +102,7 @@ local page = UI.Page {
},
tray = UI.Window {
y = -1, width = 8,
backgroundColor = UI.colors.tertiary,
backgroundColor = 'tertiary',
newApp = UI.FlatButton {
x = 2,
text = '+', event = 'new',
@ -150,7 +149,7 @@ local page = UI.Page {
text = 'Load', event = 'loadIcon', help = 'Load icon file',
},
image = UI.NftImage {
backgroundColor = colors.black,
backgroundColor = 'black',
y = 6, x = 2, height = 3, width = 8,
},
},
@ -330,9 +329,9 @@ function page.container:setCategory(categoryName, animate)
text = program.title,
centered = false,
backgroundColor = self:getProperty('backgroundColor'),
backgroundFocusColor = colors.gray,
textColor = colors.white,
textFocusColor = colors.white,
backgroundFocusColor = 'gray',
textColor = 'white',
textFocusColor = 'white',
event = 'button',
app = program,
}
@ -349,9 +348,9 @@ function page.container:setCategory(categoryName, animate)
y = 4,
text = title,
backgroundColor = self:getProperty('backgroundColor'),
backgroundFocusColor = colors.gray,
textColor = colors.white,
textFocusColor = colors.white,
backgroundFocusColor = 'gray',
textColor = 'white',
textFocusColor = 'white',
width = #title + 2,
event = 'button',
app = program,
@ -590,7 +589,6 @@ function page.editor:eventHandler(event)
elseif event.type == 'focus_change' then
self.statusBar:setStatus(event.focused.help or '')
self.statusBar:draw()
elseif event.type == 'editIcon' then
local filename = '/tmp/editing.nft'

View File

@ -150,7 +150,6 @@ page = UI.Page {
})
end
self.tabs.properties.grid:setValues(t)
self.tabs.properties.grid:update()
self.tabs.properties.grid:draw()
t = { }
@ -162,7 +161,6 @@ page = UI.Page {
end
end
self.tabs.methodsTab.grid:setValues(t)
self.tabs.methodsTab.grid:update()
self.tabs.methodsTab.grid:draw()
elseif event.type == 'edit_property' then
@ -184,7 +182,7 @@ page = UI.Page {
}
self.ox = math.max(self.ox + sizing[event.ie.code][1], 1)
self.oy = math.max(self.oy + sizing[event.ie.code][2], 1)
UI.term.device.clear()
UI.term:clear()
self:resize()
self:draw()
end

View File

@ -2,8 +2,6 @@ local Array = require('opus.array')
local Config = require('opus.config')
local UI = require('opus.ui')
local colors = _G.colors
local tab = UI.Tab {
tabTitle = 'Preferred',
description = 'Select preferred applications',
@ -25,7 +23,7 @@ local tab = UI.Tab {
},
getRowTextColor = function(self, row)
if row == self.values[1] then
return colors.yellow
return 'yellow'
end
return UI.Grid.getRowTextColor(self, row)
end,

View File

@ -2,8 +2,6 @@ local Ansi = require('opus.ansi')
local Config = require('opus.config')
local UI = require('opus.ui')
local colors = _G.colors
if _G.http.websocket then
local config = Config.load('cloud')
@ -29,8 +27,8 @@ if _G.http.websocket then
},
labelText = UI.TextArea {
x = 2, ex = -2, y = 5, ey = -4,
textColor = colors.yellow,
backgroundColor = colors.black,
textColor = 'yellow',
backgroundColor = 'black',
marginLeft = 1, marginRight = 1, marginTop = 1,
value = string.format(
[[Use a non-changing cloud key. Note that only a single computer can use this session at one time.

View File

@ -2,8 +2,6 @@ local Security = require('opus.security')
local SHA = require('opus.crypto.sha2')
local UI = require('opus.ui')
local colors = _G.colors
return UI.Tab {
tabTitle = 'Password',
description = 'Wireless network password',
@ -26,8 +24,8 @@ return UI.Tab {
},
info = UI.TextArea {
x = 2, ex = -2, y = 5, ey = -4,
backgroundColor = colors.black,
textColor = colors.yellow,
backgroundColor = 'black',
textColor = 'yellow',
inactive = true,
marginLeft = 1, marginRight = 1, marginTop = 1,
value = 'Add a password to enable other computers to connect to this one.',

View File

@ -1,3 +1,4 @@
local Blit = require('opus.ui.blit')
local Config = require('opus.config')
local trace = require('opus.trace')
local Util = require('opus.util')
@ -45,6 +46,7 @@ local config = {
Config.load('multishell', config)
local _colors = parentTerm.isColor() and config.color or config.standard
local palette = parentTerm.isColor() and Blit.colorPalette or Blit.grayscalePalette
local function redrawMenu()
if not tabsDirty then
@ -205,17 +207,11 @@ end)
kernel.hook('multishell_redraw', function()
tabsDirty = false
local function write(x, text, bg, fg)
parentTerm.setBackgroundColor(bg)
parentTerm.setTextColor(fg)
parentTerm.setCursorPos(x, 1)
parentTerm.write(text)
end
local bg = _colors.tabBarBackgroundColor
parentTerm.setBackgroundColor(bg)
parentTerm.setCursorPos(1, 1)
parentTerm.clearLine()
local blit = Blit(w, {
bg = _colors.tabBarBackgroundColor,
fg = _colors.textColor,
palette = palette,
})
local currentTab = kernel.getFocused()
@ -252,21 +248,26 @@ kernel.hook('multishell_redraw', function()
tabX = tabX + tab.width
if tab ~= currentTab then
local textColor = tab.isDead and _colors.errorColor or _colors.textColor
write(tab.sx, tab.title:sub(1, tab.width - 1),
blit:write(tab.sx, tab.title:sub(1, tab.width - 1),
_colors.backgroundColor, textColor)
end
end
end
if currentTab then
write(currentTab.sx - 1,
' ' .. currentTab.title:sub(1, currentTab.width - 1) .. ' ',
_colors.focusBackgroundColor, _colors.focusTextColor)
if currentTab.sx then
blit:write(currentTab.sx - 1,
' ' .. currentTab.title:sub(1, currentTab.width - 1) .. ' ',
_colors.focusBackgroundColor, _colors.focusTextColor)
end
if not currentTab.noTerminate then
write(w, closeInd, _colors.backgroundColor, _colors.focusTextColor)
blit:write(w, closeInd, nil, _colors.focusTextColor)
end
end
parentTerm.setCursorPos(1, 1)
parentTerm.blit(blit.text, blit.fg, blit.bg)
if currentTab and currentTab.window then
currentTab.window.restoreCursor()
end

View File

@ -1,3 +1,5 @@
local Util = require('opus.util')
local Array = { }
function Array.filter(it, f)
@ -19,4 +21,6 @@ function Array.removeByValue(t, e)
end
end
Array.find = Util.find
return Array

View File

@ -1,7 +1,6 @@
local Util = require('opus.util')
local fs = _G.fs
local shell = _ENV.shell
local Config = { }
@ -25,23 +24,6 @@ function Config.load(fname, data)
return data
end
function Config.loadWithCheck(fname, data)
local filename = 'usr/config/' .. fname
if not fs.exists(filename) then
Config.load(fname, data)
print()
print('The configuration file has been created.')
print('The file name is: ' .. filename)
print()
_G.printError('Press enter to configure')
_G.read()
shell.run('edit ' .. filename)
end
return Config.load(fname, data)
end
function Config.update(fname, data)
local filename = 'usr/config/' .. fname
Util.writeTable(filename, data)

View File

@ -24,9 +24,9 @@ function git.list(repository)
local function getContents()
local dataUrl = string.format(TREE_URL, user, repo, branch)
local contents, msg = Util.httpGet(dataUrl,TREE_HEADERS)
local contents, msg = Util.httpGet(dataUrl, TREE_HEADERS)
if not contents then
error(_sformat('Failed to download %s\n%s', dataUrl, msg), 2)
error(string.format('Failed to download %s\n%s', dataUrl, msg), 2)
else
return json.decode(contents)
end

View File

@ -36,7 +36,7 @@ function Terminal.window(parent, sx, sy, w, h, isVisible)
local maxScroll = 100
local cx, cy = 1, 1
local blink = false
local bg, fg = parent.getBackgroundColor(), parent.getTextColor()
local _bg, _fg = parent.getBackgroundColor(), parent.getTextColor()
win.canvas = Canvas({
x = sx,
@ -45,6 +45,8 @@ function Terminal.window(parent, sx, sy, w, h, isVisible)
height = h,
isColor = parent.isColor(),
offy = 0,
bg = _bg,
fg = _fg,
})
local function update()
@ -67,7 +69,7 @@ function Terminal.window(parent, sx, sy, w, h, isVisible)
function win.write(str)
str = tostring(str) or ''
win.canvas:write(cx, cy + win.canvas.offy, str, bg, fg)
win.canvas:write(cx, cy + win.canvas.offy, str, win.canvas.bg, win.canvas.fg)
win.setCursorPos(cx + #str, cy)
update()
end
@ -83,7 +85,7 @@ function Terminal.window(parent, sx, sy, w, h, isVisible)
for i = #win.canvas.lines, win.canvas.height + 1, -1 do
win.canvas.lines[i] = nil
end
win.canvas:clear(bg, fg)
win.canvas:clear()
update()
end
@ -93,7 +95,7 @@ function Terminal.window(parent, sx, sy, w, h, isVisible)
end
function win.clearLine()
win.canvas:clearLine(cy + win.canvas.offy, bg, fg)
win.canvas:clearLine(cy + win.canvas.offy)
win.setCursorPos(cx, cy)
update()
end
@ -126,7 +128,7 @@ function Terminal.window(parent, sx, sy, w, h, isVisible)
win.isColour = win.isColor
function win.setTextColor(c)
fg = c
win.canvas.fg = c
end
win.setTextColour = win.setTextColor
@ -146,7 +148,7 @@ function Terminal.window(parent, sx, sy, w, h, isVisible)
win.setPaletteColour = win.setPaletteColor
function win.setBackgroundColor(c)
bg = c
win.canvas.bg = c
end
win.setBackgroundColour = win.setBackgroundColor
@ -160,7 +162,7 @@ function Terminal.window(parent, sx, sy, w, h, isVisible)
local lines = #win.canvas.lines
for i = 1, n do
win.canvas.lines[lines + i] = { }
win.canvas:clearLine(lines + i, bg, fg)
win.canvas:clearLine(lines + i)
end
while #win.canvas.lines > maxScroll do
table.remove(win.canvas.lines, 1)
@ -172,12 +174,12 @@ function Terminal.window(parent, sx, sy, w, h, isVisible)
end
function win.getTextColor()
return fg
return win.canvas.fg
end
win.getTextColour = win.getTextColor
function win.getBackgroundColor()
return bg
return win.canvas.bg
end
win.getBackgroundColour = win.getBackgroundColor
@ -201,7 +203,7 @@ function Terminal.window(parent, sx, sy, w, h, isVisible)
function win.restoreCursor()
if isVisible then
win.setCursorPos(cx, cy)
win.setTextColor(fg)
win.setTextColor(win.canvas.fg)
win.setCursorBlink(blink)
end
end

View File

@ -1,14 +1,13 @@
local Array = require('opus.array')
local Blit = require('opus.ui.blit')
local Canvas = require('opus.ui.canvas')
local class = require('opus.class')
local Event = require('opus.event')
local Input = require('opus.input')
local Transition = require('opus.ui.transition')
local Util = require('opus.util')
local Canvas = require('opus.ui.canvas')
local _rep = string.rep
local _sub = string.sub
local colors = _G.colors
local device = _G.device
local fs = _G.fs
@ -191,11 +190,7 @@ function UI:configure(appName, ...)
end
if defaults.theme then
for k,v in pairs(defaults.theme) do
if self[k] and self[k].defaults then
Util.merge(self[k].defaults, v)
end
end
Util.deepMerge(self.theme, defaults.theme)
end
end
@ -217,6 +212,9 @@ function UI:generateTheme(filename)
local t = { }
local function getName(d)
if type(d) == 'string' then
return string.format("'%s'", d)
end
for c, n in pairs(colors) do
if n == d then
return 'colors.' .. c
@ -434,7 +432,6 @@ UI.Window.defaults = {
UIElement = 'Window',
x = 1,
y = 1,
-- z = 0, -- eventually...
offx = 0,
offy = 0,
cursorX = 1,
@ -771,11 +768,8 @@ function UI.Window:centeredWrite(y, text, bg, fg)
if #text >= self.width then
self:write(1, y, text, bg, fg)
else
local space = math.floor((self.width-#text) / 2)
local filler = _rep(' ', space + 1)
local str = _sub(filler, 1, space) .. text
str = str .. _sub(filler, self.width - #str + 1)
self:write(1, y, str, bg, fg)
local x = math.floor((self.width-#text) / 2) + 1
self:write(x, y, text, bg, fg)
end
end
@ -1076,6 +1070,9 @@ function UI.Device:sync()
if self:getCursorBlink() then
self.device.setCursorPos(self.cursorX, self.cursorY)
if self.isColor then
self.device.setTextColor(colors.orange)
end
self.device.setCursorBlink(true)
end
end
@ -1117,6 +1114,11 @@ loadComponents()
UI:loadTheme('usr/config/ui.theme')
Util.merge(UI.Window.defaults, UI.theme.Window)
Util.merge(UI.colors, UI.theme.colors)
UI:setDefaultDevice(UI.Device({ device = term.current() }))
UI:setDefaultDevice(UI.Device())
for k,v in pairs(UI.colors) do
Canvas.colorPalette[k] = Canvas.colorPalette[v]
Canvas.grayscalePalette[k] = Canvas.grayscalePalette[v]
end
return UI

View File

@ -1,16 +1,91 @@
local class = require('opus.class')
local colors = _G.colors
local _rep = string.rep
local _sub = string.sub
local Blit = class()
local Blit = { }
function Blit:init(t, cs)
if type(t) == 'string' then
t = Blit.toblit(t, cs or { })
end
self.text = t.text
self.bg = t.bg
self.fg = t.fg
Blit.colorPalette = { }
Blit.grayscalePalette = { }
for n = 1, 16 do
Blit.colorPalette[2 ^ (n - 1)] = _sub("0123456789abcdef", n, n)
Blit.grayscalePalette[2 ^ (n - 1)] = _sub("088888878877787f", n, n)
end
-- default palette
Blit.palette = Blit.colorPalette
function Blit:init(t, args)
if args then
for k,v in pairs(args) do
self[k] = v
end
end
if type(t) == 'string' then
-- create a blit from a string
self.text, self.bg, self.fg = Blit.toblit(t, args or { })
elseif type(t) == 'number' then
-- create a fixed width blit
self.width = t
self.text = _rep(' ', self.width)
self.bg = _rep(self.palette[args.bg], self.width)
self.fg = _rep(self.palette[args.fg], self.width)
else
self.text = t.text
self.bg = t.bg
self.fg = t.fg
end
end
function Blit:write(x, text, bg, fg)
self:insert(x, text,
bg and _rep(self.palette[bg], #text),
fg and _rep(self.palette[fg], #text))
end
function Blit:insert(x, text, bg, fg)
if x <= self.width then
local width = #text
local tx, tex
if x < 1 then
tx = 2 - x
width = width + x - 1
x = 1
end
if x + width - 1 > self.width then
tex = self.width - x + (tx or 1)
width = tex - (tx or 1) + 1
end
if width > 0 then
local function replace(sstr, rstr)
if tx or tex then
rstr = _sub(rstr, tx or 1, tex)
end
if x == 1 and width == self.width then
return rstr
elseif x == 1 then
return rstr .. _sub(sstr, x + width)
elseif x + width > self.width then
return _sub(sstr, 1, x - 1) .. rstr
end
return _sub(sstr, 1, x - 1) .. rstr .. _sub(sstr, x + width)
end
self.text = replace(self.text, text)
if fg then
self.fg = replace(self.fg, fg)
end
if bg then
self.bg = replace(self.bg, bg)
end
end
end
end
function Blit:sub(s, e)
@ -22,7 +97,6 @@ function Blit:sub(s, e)
end
function Blit:wrap(max)
local index = 1
local lines = { }
local data = self
@ -31,7 +105,7 @@ function Blit:wrap(max)
table.insert(lines, data)
break
elseif data.text:sub(max+1, max+1) == ' ' then
table.insert(lines, data:sub(index, max))
table.insert(lines, data:sub(1, max))
data = data:sub(max + 2)
else
local x = data.text:sub(1, max)
@ -56,18 +130,20 @@ function Blit.toblit(str, cs)
if not cs.cbg then
-- reset colors
cs.rbg = cs.palette[cs.bg or colors.black]
cs.rfg = cs.palette[cs.fg or colors.white]
cs.rbg = cs.bg or colors.black
cs.rfg = cs.fg or colors.white
-- current colors
cs.cbg = cs.rbg
cs.cfg = cs.rfg
cs.palette = cs.palette or Blit.palette
end
str = str:gsub('(.-)\027%[([%d;]+)m',
function(k, seq)
text = text .. k
bg = bg .. string.rep(cs.cbg, #k)
fg = fg .. string.rep(cs.cfg, #k)
bg = bg .. string.rep(cs.palette[cs.cbg], #k)
fg = fg .. string.rep(cs.palette[cs.cfg], #k)
for color in string.gmatch(seq, "%d+") do
color = tonumber(color)
if color == 0 then
@ -75,20 +151,24 @@ function Blit.toblit(str, cs)
cs.cfg = cs.rfg
cs.cbg = cs.rbg
elseif color > 20 then
cs.cbg = string.sub("0123456789abcdef", color - 21, color - 21)
cs.cbg = 2 ^ (color - 21)
else
cs.cfg = string.sub("0123456789abcdef", color, color)
cs.cfg = 2 ^ (color - 1)
end
end
return k
end)
local k = str:sub(#text + 1)
return {
text = text .. k,
bg = bg .. string.rep(cs.cbg, #k),
fg = fg .. string.rep(cs.cfg, #k),
}
return text .. k,
bg .. string.rep(cs.palette[cs.cbg], #k),
fg .. string.rep(cs.palette[cs.cfg], #k)
end
return Blit
return setmetatable(Blit, {
__call = function(_, ...)
local obj = setmetatable({ }, { __index = Blit })
obj:init(...)
return obj
end
})

View File

@ -9,16 +9,21 @@ local colors = _G.colors
local Canvas = class()
Canvas.colorPalette = { }
Canvas.darkPalette = { }
Canvas.grayscalePalette = { }
for n = 1, 16 do
Canvas.colorPalette[2 ^ (n - 1)] = _sub("0123456789abcdef", n, n)
Canvas.grayscalePalette[2 ^ (n - 1)] = _sub("088888878877787f", n, n)
Canvas.darkPalette[2 ^ (n - 1)] = _sub("8777777f77fff77f", n, n)
local function genPalette(map)
local t = { }
local rcolors = Util.transpose(colors)
for n = 1, 16 do
local pow = 2 ^ (n - 1)
local ch = _sub(map, n, n)
t[pow] = ch
t[rcolors[pow]] = ch
end
return t
end
Canvas.colorPalette = genPalette('0123456789abcdef')
Canvas.grayscalePalette = genPalette('088888878877787f')
--[[
A canvas can have more lines than canvas.height in order to scroll
@ -119,19 +124,12 @@ function Canvas:copy()
end
function Canvas:addLayer(layer)
local canvas = Canvas({
x = layer.x,
y = layer.y,
width = layer.width,
height = layer.height,
isColor = self.isColor,
})
canvas.parent = self
layer.parent = self
if not self.children then
self.children = { }
end
table.insert(self.children, canvas)
return canvas
table.insert(self.children, 1, layer)
return layer
end
function Canvas:removeLayer()
@ -177,54 +175,42 @@ end
function Canvas:blit(x, y, text, bg, fg)
if y > 0 and y <= #self.lines and x <= self.width then
local width = #text
local tx, tex
-- fix ffs
if x < 1 then
text = _sub(text, 2 - x)
if bg then
bg = _sub(bg, 2 - x)
end
if fg then
fg = _sub(fg, 2 - x)
end
tx = 2 - x
width = width + x - 1
x = 1
end
if x + width - 1 > self.width then
text = _sub(text, 1, self.width - x + 1)
if bg then
bg = _sub(bg, 1, self.width - x + 1)
end
if fg then
fg = _sub(fg, 1, self.width - x + 1)
end
width = #text
tex = self.width - x + (tx or 1)
width = tex - (tx or 1) + 1
end
if width > 0 then
local function replace(sstr, pos, rstr)
if pos == 1 and width == self.width then
return rstr
elseif pos == 1 then
return rstr .. _sub(sstr, pos+width)
elseif pos + width > self.width then
return _sub(sstr, 1, pos-1) .. rstr
local function replace(sstr, rstr)
if tx or tex then
rstr = _sub(rstr, tx or 1, tex)
end
return _sub(sstr, 1, pos-1) .. rstr .. _sub(sstr, pos+width)
if x == 1 and width == self.width then
return rstr
elseif x == 1 then
return rstr .. _sub(sstr, x + width)
elseif x + width > self.width then
return _sub(sstr, 1, x - 1) .. rstr
end
return _sub(sstr, 1, x - 1) .. rstr .. _sub(sstr, x + width)
end
local line = self.lines[y]
if line then
line.dirty = true
line.text = replace(line.text, x, text, width)
if fg then
line.fg = replace(line.fg, x, fg, width)
end
if bg then
line.bg = replace(line.bg, x, bg, width)
end
line.dirty = true
line.text = replace(line.text, text)
if fg then
line.fg = replace(line.fg, fg)
end
if bg then
line.bg = replace(line.bg, bg)
end
end
end
@ -383,9 +369,6 @@ function Canvas:__renderLayers(device, offset, doubleBuffer)
end
function Canvas:__blitRect(device, src, tgt, doubleBuffer)
src = src or { x = 1, y = 1, ex = self.ex - self.x + 1, ey = self.ey - self.y + 1 }
tgt = tgt or self
-- for visualizing updates on the screen
--[[
if Canvas.__visualize or self.visualize then
@ -426,16 +409,4 @@ function Canvas:__blitRect(device, src, tgt, doubleBuffer)
end
end
if not ({ ... })[1] then
local UI = require('opus.ui')
UI:setPage(UI.Page {
button = UI.Button {
x = 5, y = 5,
text = 'abc'
}
})
UI:start()
end
return Canvas

View File

@ -2,17 +2,15 @@ local class = require('opus.class')
local UI = require('opus.ui')
local Util = require('opus.util')
local colors = _G.colors
UI.Button = class(UI.Window)
UI.Button.defaults = {
UIElement = 'Button',
text = 'button',
backgroundColor = colors.lightGray,
backgroundFocusColor = colors.gray,
textFocusColor = colors.white,
textInactiveColor = colors.gray,
textColor = colors.black,
backgroundColor = 'lightGray',
backgroundFocusColor = 'gray',
textFocusColor = 'white',
textInactiveColor = 'gray',
textColor = 'black',
centered = true,
height = 1,
focusIndicator = ' ',
@ -73,7 +71,7 @@ function UI.Button.example()
},
button2 = UI.Button {
x = 2, y = 4,
backgroundColor = colors.green,
backgroundColor = 'green',
event = 'custom_event',
},
button3 = UI.Button {

View File

@ -1,8 +1,6 @@
local class = require('opus.class')
local UI = require('opus.ui')
local colors = _G.colors
UI.Checkbox = class(UI.Window)
UI.Checkbox.defaults = {
UIElement = 'Checkbox',
@ -11,9 +9,9 @@ UI.Checkbox.defaults = {
leftMarker = UI.extChars and '\124' or '[',
rightMarker = UI.extChars and '\124' or ']',
value = false,
textColor = colors.white,
backgroundColor = colors.black,
backgroundFocusColor = colors.lightGray,
textColor = 'white',
backgroundColor = 'black',
backgroundFocusColor = 'lightGray',
height = 1,
width = 3,
accelerators = {

View File

@ -1,8 +1,6 @@
local class = require('opus.class')
local UI = require('opus.ui')
local colors = _G.colors
UI.Dialog = class(UI.SlideOut)
UI.Dialog.defaults = {
UIElement = 'Dialog',
@ -33,7 +31,7 @@ function UI.Dialog.example()
cancelEvent = 'slide_hide',
text = UI.Text {
x = 5, y = 1, width = 20,
textColor = colors.gray,
textColor = 'gray',
},
textEntry = UI.TextEntry {
formKey = 'level',

View File

@ -2,12 +2,10 @@ local class = require('opus.class')
local UI = require('opus.ui')
local Util = require('opus.util')
local colors = _G.colors
UI.DropMenu = class(UI.MenuBar)
UI.DropMenu.defaults = {
UIElement = 'DropMenu',
backgroundColor = colors.white,
backgroundColor = 'white',
buttonClass = 'DropMenuItem',
}
function UI.DropMenu:layout()

View File

@ -1,16 +1,14 @@
local class = require('opus.class')
local UI = require('opus.ui')
local colors = _G.colors
UI.DropMenuItem = class(UI.Button)
UI.DropMenuItem.defaults = {
UIElement = 'DropMenuItem',
textColor = colors.black,
backgroundColor = colors.white,
textFocusColor = colors.white,
textInactiveColor = colors.lightGray,
backgroundFocusColor = colors.lightGray,
textColor = 'black',
backgroundColor = 'white',
textFocusColor = 'white',
textInactiveColor = 'lightGray',
backgroundFocusColor = 'lightGray',
}
function UI.DropMenuItem:eventHandler(event)
if event.type == 'button_activate' then

View File

@ -3,13 +3,11 @@ local Event = require('opus.event')
local Terminal = require('opus.terminal')
local UI = require('opus.ui')
local colors = _G.colors
UI.Embedded = class(UI.Window)
UI.Embedded.defaults = {
UIElement = 'Embedded',
backgroundColor = colors.black,
textColor = colors.white,
backgroundColor = 'black',
textColor = 'white',
maxScroll = 100,
accelerators = {
up = 'scroll_up',

View File

@ -1,13 +1,11 @@
local class = require('opus.class')
local UI = require('opus.ui')
local colors = _G.colors
UI.FlatButton = class(UI.Button)
UI.FlatButton.defaults = {
UIElement = 'FlatButton',
textColor = colors.black,
textFocusColor = colors.white,
textColor = 'black',
textFocusColor = 'white',
noPadding = true,
}
function UI.FlatButton:setParent()

View File

@ -2,8 +2,6 @@ local class = require('opus.class')
local Sound = require('opus.sound')
local UI = require('opus.ui')
local colors = _G.colors
UI.Form = class(UI.Window)
UI.Form.defaults = {
UIElement = 'Form',
@ -68,7 +66,7 @@ function UI.Form:createForm()
table.insert(self.children, UI.Text {
x = self.margin,
y = child.y,
textColor = colors.black,
textColor = 'black',
width = #child.formLabel,
value = child.formLabel,
})

View File

@ -2,10 +2,8 @@ local class = require('opus.class')
local UI = require('opus.ui')
local Util = require('opus.util')
local colors = _G.colors
local os = _G.os
local _rep = string.rep
local _sub = string.sub
local function safeValue(v)
local t = type(v)
@ -23,18 +21,7 @@ function Writer:init(element, y)
end
function Writer:write(s, width, align, bg, fg)
local len = #tostring(s or '')
if len > width then
s = _sub(s, 1, width)
end
local padding = len < width and _rep(' ', width - len)
if padding then
if align == 'right' then
s = padding .. s
else
s = s .. padding
end
end
s = Util.widthify(s, width, align)
self.element:write(self.x, self.y, s, bg, fg)
self.x = self.x + width
end
@ -56,16 +43,16 @@ UI.Grid.defaults = {
disableHeader = false,
headerHeight = 1,
marginRight = 0,
textColor = colors.white,
textSelectedColor = colors.white,
backgroundColor = colors.black,
backgroundSelectedColor = colors.gray,
headerBackgroundColor = UI.colors.primary,
headerTextColor = colors.white,
headerSortColor = colors.yellow,
unfocusedTextSelectedColor = colors.white,
unfocusedBackgroundSelectedColor = colors.gray,
focusIndicator = UI.extChars and '\183' or '>',
textColor = 'white',
textSelectedColor = 'white',
backgroundColor = 'black',
backgroundSelectedColor = 'gray',
headerBackgroundColor = 'primary',
headerTextColor = 'white',
headerSortColor = 'yellow',
unfocusedTextSelectedColor = 'white',
unfocusedBackgroundSelectedColor = 'gray',
focusIndicator = UI.extChars and '\26' or '>',
sortIndicator = ' ',
inverseSortIndicator = UI.extChars and '\24' or '^',
values = { },
@ -522,7 +509,7 @@ function UI.Grid.example()
values = values,
columns = {
{ heading = 'key', key = 'key', width = 6, },
{ heading = 'value', key = 'value', textColor = colors.yellow },
{ heading = 'value', key = 'value', textColor = 'yellow' },
},
},
autospace = UI.Grid {

View File

@ -1,15 +1,13 @@
local class = require('opus.class')
local UI = require('opus.ui')
local colors = _G.colors
UI.MenuBar = class(UI.Window)
UI.MenuBar.defaults = {
UIElement = 'MenuBar',
buttons = { },
height = 1,
backgroundColor = UI.colors.secondary,
textColor = colors.black,
backgroundColor = 'secondary',
textColor = 'black',
spacing = 2,
lastx = 1,
buttonClass = 'MenuItem',

View File

@ -5,5 +5,5 @@ UI.MenuItem = class(UI.FlatButton)
UI.MenuItem.defaults = {
UIElement = 'MenuItem',
noPadding = false,
textInactiveColor = colors.gray,
textInactiveColor = 'gray',
}

View File

@ -5,7 +5,7 @@ UI.MiniSlideOut = class(UI.SlideOut)
UI.MiniSlideOut.defaults = {
UIElement = 'MiniSlideOut',
noFill = true,
backgroundColor = UI.colors.primary,
backgroundColor = 'primary',
height = 1,
}
function UI.MiniSlideOut:postInit()

View File

@ -4,36 +4,34 @@ local Sound = require('opus.sound')
local UI = require('opus.ui')
local Util = require('opus.util')
local colors = _G.colors
UI.Notification = class(UI.Window)
UI.Notification.defaults = {
UIElement = 'Notification',
backgroundColor = colors.gray,
backgroundColor = 'gray',
closeInd = UI.extChars and '\215' or '*',
height = 3,
timeout = 3,
anchor = 'bottom',
}
function UI.Notification:draw()
function UI.Notification.draw()
end
function UI.Notification:enable()
function UI.Notification.enable()
end
function UI.Notification:error(value, timeout)
self.backgroundColor = colors.red
self.backgroundColor = 'red'
Sound.play('entity.villager.no', .5)
self:display(value, timeout)
end
function UI.Notification:info(value, timeout)
self.backgroundColor = colors.lightGray
self.backgroundColor = 'lightGray'
self:display(value, timeout)
end
function UI.Notification:success(value, timeout)
self.backgroundColor = colors.green
self.backgroundColor = 'green'
self:display(value, timeout)
end

View File

@ -2,8 +2,6 @@ local class = require('opus.class')
local UI = require('opus.ui')
local Util = require('opus.util')
local colors = _G.colors
UI.Page = class(UI.Window)
UI.Page.defaults = {
UIElement = 'Page',
@ -16,8 +14,8 @@ UI.Page.defaults = {
up = 'focus_prev',
scroll_up = 'focus_prev',
},
backgroundColor = UI.colors.primary,
textColor = colors.white,
backgroundColor = 'primary',
textColor = 'white',
}
function UI.Page:postInit()
self.parent = self.parent or UI.term

View File

@ -1,35 +1,28 @@
local class = require('opus.class')
local UI = require('opus.ui')
local colors = _G.colors
UI.ProgressBar = class(UI.Window)
UI.ProgressBar.defaults = {
UIElement = 'ProgressBar',
backgroundColor = colors.gray,
backgroundColor = 'gray',
height = 1,
progressColor = colors.lime,
progressColor = 'lime',
progressChar = UI.extChars and '\153' or ' ',
fillChar = ' ',
fillColor = colors.gray,
textColor = colors.green,
fillColor = 'gray',
textColor = 'green',
value = 0,
}
function UI.ProgressBar:draw()
local width = math.ceil(self.value / 100 * self.width)
local filler = string.rep(self.fillChar, self.width)
local progress = string.rep(self.progressChar, width)
for i = 1, self.height do
self:write(1, i, filler, nil, self.fillColor)
self:write(1, i, progress, self.progressColor)
end
self:fillArea(width + 1, 1, self.width - width, self.height, self.fillChar, nil, self.fillColor)
self:fillArea(1, 1, width, self.height, self.progressChar, self.progressColor)
end
function UI.ProgressBar.example()
return UI.ProgressBar {
x = 2, ex = -2, y = 2,
x = 2, ex = -2, y = 2, height = 2,
focus = function() end,
enable = function(self)
require('opus.event').onInterval(.25, function()

View File

@ -15,13 +15,13 @@ function UI.Question:postInit()
self.yes_button = UI.Button {
x = x,
text = 'Yes',
backgroundColor = UI.colors.primary,
backgroundColor = 'primary',
event = 'question_yes',
}
self.no_button = UI.Button {
x = x + 5,
text = 'No',
backgroundColor = UI.colors.primary,
backgroundColor = 'primary',
event = 'question_no',
}
end

View File

@ -30,7 +30,7 @@ end
function UI.SlideOut:draw()
if not self.noFill then
self:fillArea(1, 1, self.width, self.height, string.rep('\127', self.width), colors.black, colors.gray)
self:fillArea(1, 1, self.width, self.height, string.rep('\127', self.width), 'black', 'gray')
end
self:drawChildren()
end

View File

@ -2,17 +2,15 @@ local class = require('opus.class')
local UI = require('opus.ui')
local Util = require('opus.util')
local colors = _G.colors
UI.Slider = class(UI.Window)
UI.Slider.defaults = {
UIElement = 'Slider',
height = 1,
barChar = UI.extChars and '\140' or '-',
barColor = colors.gray,
barColor = 'gray',
sliderChar = UI.extChars and '\143' or '\124',
sliderColor = colors.blue,
sliderFocusColor = colors.lightBlue,
sliderColor = 'blue',
sliderFocusColor = 'lightBlue',
leftBorder = UI.extChars and '\141' or '\124',
rightBorder = UI.extChars and '\142' or '\124',
value = 0,

View File

@ -3,13 +3,11 @@ local Event = require('opus.event')
local UI = require('opus.ui')
local Util = require('opus.util')
local colors = _G.colors
UI.StatusBar = class(UI.Window)
UI.StatusBar.defaults = {
UIElement = 'StatusBar',
backgroundColor = colors.lightGray,
textColor = colors.gray,
backgroundColor = 'lightGray',
textColor = 'gray',
height = 1,
ey = -1,
}

View File

@ -6,9 +6,9 @@ UI.TabBar = class(UI.MenuBar)
UI.TabBar.defaults = {
UIElement = 'TabBar',
buttonClass = 'TabBarMenuItem',
backgroundColor = colors.black,
selectedBackgroundColor = UI.colors.primary,
unselectedBackgroundColor = UI.colors.tertiary,
backgroundColor = 'black',
selectedBackgroundColor = 'primary',
unselectedBackgroundColor = 'tertiary',
}
function UI.TabBar:enable()
UI.MenuBar.enable(self)

View File

@ -5,7 +5,7 @@ UI.TabBarMenuItem = class(UI.Button)
UI.TabBarMenuItem.defaults = {
UIElement = 'TabBarMenuItem',
event = 'tab_select',
textInactiveColor = colors.lightGray,
textInactiveColor = 'lightGray',
}
function UI.TabBarMenuItem:draw()
if self.selected then

View File

@ -14,7 +14,7 @@ function UI.TextArea:setText(text)
self:draw()
end
function UI.TextArea:focus()
function UI.TextArea.focus()
-- allow keyboard scrolling
end
@ -25,6 +25,7 @@ function UI.TextArea:draw()
end
function UI.TextArea.example()
local Ansi = require('opus.ansi')
return UI.Window {
backgroundColor = 2048,
t1 = UI.TextArea {
@ -33,14 +34,16 @@ function UI.TextArea.example()
},
t2 = UI.TextArea {
y = 5,
value = [[1
backgroundColor = 'green',
value = string.format([[now %%is the %stime %sfor%s all good men to come to the aid of their country.
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
3
4
5
6
7
8]]
8]], Ansi.yellow, Ansi.onred, Ansi.reset),
}
}
end

View File

@ -3,7 +3,6 @@ local entry = require('opus.entry')
local UI = require('opus.ui')
local Util = require('opus.util')
local colors = _G.colors
local _rep = string.rep
local function transform(directive)
@ -21,11 +20,11 @@ UI.TextEntry.defaults = {
UIElement = 'TextEntry',
shadowText = '',
focused = false,
textColor = colors.white,
shadowTextColor = colors.gray,
markBackgroundColor = colors.gray,
backgroundColor = colors.black, -- colors.lightGray,
backgroundFocusColor = colors.black, --lightGray,
textColor = 'white',
shadowTextColor = 'gray',
markBackgroundColor = 'gray',
backgroundColor = 'black',
backgroundFocusColor = 'black',
height = 1,
limit = 6,
cursorBlink = true,

View File

@ -1,39 +1,6 @@
local class = require('opus.class')
local UI = require('opus.ui')
local colors = _G.colors
local _rep = string.rep
local _sub = string.sub
-- For manipulating text in a fixed width string
local SB = class()
function SB:init(width)
self.width = width
self.buf = _rep(' ', width)
end
function SB:insert(x, str, width)
if x < 1 then
x = self.width + x + 1
end
width = width or #str
if x + width - 1 > self.width then
width = self.width - x
end
if width > 0 then
self.buf = _sub(self.buf, 1, x - 1) .. _sub(str, 1, width) .. _sub(self.buf, x + width)
end
end
function SB:fill(x, ch, width)
width = width or self.width - x + 1
self:insert(x, _rep(ch, width))
end
function SB:center(str)
self:insert(math.max(1, math.ceil((self.width - #str + 1) / 2)), str)
end
function SB:get()
return self.buf
end
UI.TitleBar = class(UI.Window)
UI.TitleBar.defaults = {
UIElement = 'TitleBar',
@ -43,15 +10,11 @@ UI.TitleBar.defaults = {
closeInd = UI.extChars and '\215' or '*',
}
function UI.TitleBar:draw()
local sb = SB(self.width)
sb:fill(2, self.frameChar, sb.width - 3)
sb:center(string.format(' %s ', self.title))
self:fillArea(2, 1, self.width - 2, 1, self.frameChar)
self:centeredWrite(1, string.format(' %s ', self.title))
if self.previousPage or self.event then
sb:insert(-1, self.closeInd)
else
sb:insert(-2, self.frameChar)
self:write(self.width - 1, 1, ' ' .. self.closeInd)
end
self:write(1, 1, sb:get())
end
function UI.TitleBar:eventHandler(event)
@ -78,7 +41,7 @@ function UI.TitleBar:eventHandler(event)
self.parent:reposition(self.parent.x, self.anchor.oy + event.dy, self.width, self.anchor.h - d)
end
else --if self.moveable then
elseif self.moveable then
local d = event.dy
if self.anchor.oy + d > 0 and self.anchor.oy + d <= self.parent.parent.height then
self.parent:move(self.anchor.ox + event.dx, self.anchor.oy + event.dy)
@ -91,9 +54,9 @@ function UI.TitleBar.example()
return UI.Window {
win1 = UI.Window {
x = 9, y = 2, ex = -7, ey = -3,
backgroundColor = colors.green,
backgroundColor = 'green',
titleBar = UI.TitleBar {
title = 'test', moveable = true,
title = 'A really, really, really long title', moveable = true,
},
button1 = UI.Button {
x = 2, y = 3,
@ -105,9 +68,10 @@ function UI.TitleBar.example()
},
win2 = UI.Window {
x = 7, y = 3, ex = -9, ey = -2,
backgroundColor = colors.orange,
backgroundColor = 'orange',
titleBar = UI.TitleBar {
title = 'test', moveable = true,
event = 'none',
},
button1 = UI.Button {
x = 2, y = 3,

View File

@ -1,13 +1,11 @@
local class = require('opus.class')
local UI = require('opus.ui')
local colors = _G.colors
UI.VerticalMeter = class(UI.Window)
UI.VerticalMeter.defaults = {
UIElement = 'VerticalMeter',
backgroundColor = colors.gray,
meterColor = colors.lime,
backgroundColor = 'gray',
meterColor = 'lime',
width = 1,
value = 0,
}
@ -30,4 +28,4 @@ function UI.VerticalMeter.example()
return UI.VerticalMeter.enable(self)
end
}
end
end

View File

@ -669,42 +669,8 @@ function Util.trimr(s)
end
-- end http://snippets.luacode.org/?p=snippets/trim_whitespace_from_string_76
-- word wrapping based on:
-- https://www.rosettacode.org/wiki/Word_wrap#Lua and
-- http://lua-users.org/wiki/StringRecipes
local function paragraphwrap(text, linewidth, res)
linewidth = linewidth or 75
local spaceleft = linewidth
local line = { }
for word in text:gmatch("%S+") do
local len = #word + 1
--if colorMode then
-- word:gsub('()@([@%d])', function(pos, c) len = len - 2 end)
--end
if len > spaceleft then
table.insert(res, table.concat(line, ' '))
line = { word }
spaceleft = linewidth - len - 1
else
table.insert(line, word)
spaceleft = spaceleft - len
end
end
table.insert(res, table.concat(line, ' '))
return table.concat(res, '\n')
end
-- end word wrapping
--[[
-- better wrapping - needs further testing before replacing the current wrapping
functions
local function wrap(text, max)
local function wrap(text, max, lines)
local index = 1
local lines = { }
repeat
if #text <= max then
table.insert(lines, text)
@ -722,14 +688,12 @@ local function wrap(text, max)
until not text or #text == 0
return lines
end
]]
function Util.wordWrap(str, limit)
local longLines = Util.split(str)
local lines = { }
for _,line in ipairs(longLines) do
paragraphwrap(line, limit, lines)
for _,line in ipairs(Util.split(str)) do
wrap(line, limit, lines)
end
return lines