1
0
mirror of https://github.com/kepler155c/opus synced 2025-01-06 13:50:27 +00:00

alignment update

This commit is contained in:
kepler155c@gmail.com 2019-03-06 11:48:09 -05:00
parent 7a57d74e68
commit ce9ffa6c3a
4 changed files with 53 additions and 35 deletions

View File

@ -16,10 +16,5 @@ function UI.Text:setParent()
end end
function UI.Text:draw() function UI.Text:draw()
if self.align and self.align == 'center' then self:write(1, 1, Util.widthify(self.value, self.width, self.align))
self:clear()
self:centeredWrite(1, self.value or '')
else
self:write(1, 1, Util.widthify(self.value or '', self.width))
end
end end

View File

@ -6,6 +6,10 @@ local os = _G.os
local term = _G.term local term = _G.term
local textutils = _G.textutils local textutils = _G.textutils
local _sformat = string.format
local _srep = string.rep
local _ssub = string.sub
function Util.tryTimed(timeout, f, ...) function Util.tryTimed(timeout, f, ...)
local c = os.clock() local c = os.clock()
repeat repeat
@ -27,13 +31,15 @@ function Util.tryTimes(attempts, f, ...)
return unpack(result) return unpack(result)
end end
function Util.Timer() function Util.timer()
local ct = os.clock() local ct = os.clock()
return function() return function()
return os.clock() - ct return os.clock() - ct
end end
end end
Util.Timer = Util.timer -- deprecate
function Util.throttle(fn) function Util.throttle(fn)
local ts = os.clock() local ts = os.clock()
local timeout = .095 local timeout = .095
@ -56,11 +62,11 @@ function Util.tostring(pattern, ...)
for k, v in pairs(tbl) do for k, v in pairs(tbl) do
local value local value
if type(v) == 'table' then if type(v) == 'table' then
value = string.format('table: %d', Util.size(v)) value = _sformat('table: %d', Util.size(v))
else else
value = tostring(v) value = tostring(v)
end end
str = str .. string.format(' %s: %s\n', k, value) str = str .. _sformat(' %s: %s\n', k, value)
end end
--if #str < width then --if #str < width then
--str = str:gsub('\n', '') .. ' }' --str = str:gsub('\n', '') .. ' }'
@ -71,7 +77,7 @@ function Util.tostring(pattern, ...)
end end
if type(pattern) == 'string' then if type(pattern) == 'string' then
return string.format(pattern, ...) return _sformat(pattern, ...)
elseif type(pattern) == 'table' then elseif type(pattern) == 'table' then
return serialize(pattern, term.current().getSize()) return serialize(pattern, term.current().getSize())
end end
@ -473,7 +479,7 @@ end
function Util.download(url, filename) function Util.download(url, filename)
local contents, msg = Util.httpGet(url) local contents, msg = Util.httpGet(url)
if not contents then if not contents then
error(string.format('Failed to download %s\n%s', url, msg), 2) error(_sformat('Failed to download %s\n%s', url, msg), 2)
end end
if filename then if filename then
@ -519,11 +525,11 @@ end
function Util.toBytes(n) function Util.toBytes(n)
if not tonumber(n) then error('Util.toBytes: n must be a number', 2) end if not tonumber(n) then error('Util.toBytes: n must be a number', 2) end
if n >= 1000000 or n <= -1000000 then if n >= 1000000 or n <= -1000000 then
return string.format('%sM', math.floor(n/1000000 * 10) / 10) return _sformat('%sM', math.floor(n/1000000 * 10) / 10)
elseif n >= 10000 or n <= -10000 then elseif n >= 10000 or n <= -10000 then
return string.format('%sK', math.floor(n/1000)) return _sformat('%sK', math.floor(n/1000))
elseif n >= 1000 or n <= -1000 then elseif n >= 1000 or n <= -1000 then
return string.format('%sK', math.floor(n/1000 * 10) / 10) return _sformat('%sK', math.floor(n/1000 * 10) / 10)
end end
return tostring(n) return tostring(n)
end end
@ -551,17 +557,33 @@ function Util.matches(str, pattern)
end end
function Util.startsWith(s, match) function Util.startsWith(s, match)
return string.sub(s, 1, #match) == match return _ssub(s, 1, #match) == match
end end
function Util.widthify(s, len) -- return a fixed length string using specified alignment
function Util.widthify(s, len, align)
s = s or '' s = s or ''
local slen = #s local slen = #s
if slen < len then
s = s .. string.rep(' ', len - #s) if slen > len then
elseif slen > len then return _ssub(s, 1, len)
s = s:sub(1, len) elseif slen == len then
return s
end end
if align == 'center' then
local space = math.floor((len-slen) / 2)
local filler = _srep(' ', space + 1)
s = _ssub(filler, 1, space) .. s
s = s .. _srep(' ', len - #s)
elseif align == 'right' then
s = _srep(' ', len - #s) .. s
else -- 'left'
s = s .. _srep(' ', len - #s)
end
return s return s
end end
@ -628,8 +650,8 @@ function Util.args(arg)
local k = 1 local k = 1
while k <= #arg do while k <= #arg do
local v = arg[k] local v = arg[k]
if string.sub(v, 1, 1) == '-' then if _ssub(v, 1, 1) == '-' then
local opt = string.sub(v, 2) local opt = _ssub(v, 2)
options[opt] = arg[k + 1] options[opt] = arg[k + 1]
k = k + 1 k = k + 1
else else
@ -645,20 +667,20 @@ local function getopt( arg, options )
local tab = {} local tab = {}
for k, v in ipairs(arg) do for k, v in ipairs(arg) do
if type(v) == 'string' then if type(v) == 'string' then
if string.sub( v, 1, 2) == "--" then if _ssub( v, 1, 2) == "--" then
local x = string.find( v, "=", 1, true ) local x = string.find( v, "=", 1, true )
if x then tab[ string.sub( v, 3, x-1 ) ] = string.sub( v, x+1 ) if x then tab[ _ssub( v, 3, x-1 ) ] = _ssub( v, x+1 )
else tab[ string.sub( v, 3 ) ] = true else tab[ _ssub( v, 3 ) ] = true
end end
elseif string.sub( v, 1, 1 ) == "-" then elseif _ssub( v, 1, 1 ) == "-" then
local y = 2 local y = 2
local l = string.len(v) local l = string.len(v)
local jopt local jopt
while ( y <= l ) do while ( y <= l ) do
jopt = string.sub( v, y, y ) jopt = _ssub( v, y, y )
if string.find( options, jopt, 1, true ) then if string.find( options, jopt, 1, true ) then
if y < l then if y < l then
tab[ jopt ] = string.sub( v, y+1 ) tab[ jopt ] = _ssub( v, y+1 )
y = l y = l
else else
tab[ jopt ] = arg[ k + 1 ] tab[ jopt ] = arg[ k + 1 ]
@ -677,7 +699,7 @@ end
function Util.showOptions(options) function Util.showOptions(options)
print('Arguments: ') print('Arguments: ')
for _, v in pairs(options) do for _, v in pairs(options) do
print(string.format('-%s %s', v.arg, v.desc)) print(_sformat('-%s %s', v.arg, v.desc))
end end
end end

View File

@ -18,12 +18,12 @@ for name in pairs(Packages:installed()) do
local packageDir = fs.combine('packages', name) local packageDir = fs.combine('packages', name)
table.insert(appPaths, 1, packageDir) table.insert(appPaths, 1, packageDir)
local apiPath = fs.combine(fs.combine('packages', name), 'apis') local apiPath = fs.combine(packageDir, 'apis')
if fs.exists(apiPath) then if fs.exists(apiPath) then
fs.mount(fs.combine('sys/apis', name), 'linkfs', apiPath) fs.mount(fs.combine('sys/apis', name), 'linkfs', apiPath)
end end
local helpPath = '/' .. fs.combine(fs.combine('packages', name), 'help') local helpPath = '/' .. fs.combine(packageDir, 'help')
if fs.exists(helpPath) then if fs.exists(helpPath) then
table.insert(helpPaths, helpPath) table.insert(helpPaths, helpPath)
end end

View File

@ -150,9 +150,10 @@ local function sendInfo()
end end
if device.neuralInterface then if device.neuralInterface then
info.status = device.neuralInterface.status info.status = device.neuralInterface.status
pcall(function() if not info.status and device.neuralInterface.getMetaOwner then
if not info.status and device.neuralInterface.getMetaOwner then pcall(function()
local meta = device.neuralInterface.getMetaOwner() local meta = device.neuralInterface.getMetaOwner()
if meta.isWet then if meta.isWet then
info.status = 'Swimming' info.status = 'Swimming'
elseif meta.isElytraFlying then elseif meta.isElytraFlying then
@ -174,8 +175,8 @@ local function sendInfo()
math.floor(meta.health / math.floor(meta.health /
meta.maxHealth * 100) meta.maxHealth * 100)
end end
end end)
end) end
end end
device.wireless_modem.transmit(999, os.getComputerID(), info) device.wireless_modem.transmit(999, os.getComputerID(), info)
end end