1
0
mirror of https://github.com/20kdc/OC-KittenOS.git synced 2024-11-08 11:38:07 +11:00
OC-KittenOS/code/libs/fmttext.lua
20kdc 7bde8fee55 Finish lowering memory use, R1
Since this is after the technical "release", version numbers have been bumped to 1.

Changes before this commit for R1:
 Kernel memory usage reduction schemes, with some security fixes.
 Still need to deal w/ proxies (see later)
Changes in this commit:
 Some various little things in apps
 CLAW inet actually works now on 192K
 sys-icecap no longer uses the event/neoux combination,
  and now handles Everest disappearance as a mass-close,
  but still handles Everest not being around on window create.
 So it still handles every situation that matters.
 neoux no longer handles everest crash protection.
 Security policy and filedialog obviously don't use neoux anymore.
 Kernel now only guarantees parsing, not event-loop, by executeAsync
 This is safer and allows app-launcher to get rid of NeoUX by
  any means necessary.
 wrapMeta cache now exists, and proxies get wrapMeta'd to deal with
  various low-priority security shenanigans.
 This is a *stopgap* until I work out how to force OCEmu to give me
  totally accurate boot-time memory figures, so I can create the
  ultimate lowmem proxy. I'm calling it "puppet". FG knows why.
2018-03-30 13:37:02 +01:00

68 lines
1.6 KiB
Lua

local fmt
fmt = {
pad = function (t, len, centre, cut)
local l = unicode.len(t)
local add = len - l
if add > 0 then
if centre then
t = (" "):rep(math.floor(add / 2)) .. t .. (" "):rep(math.ceil(add / 2))
else
t = t .. (" "):rep(add)
end
end
if cut then
t = unicode.sub(t, 1, len)
end
return t
end,
fmtText = function (text, w)
local nl = text:find("\n")
if nl then
local base = text:sub(1, nl - 1)
local ext = text:sub(nl + 1)
local baseT = fmt.fmtText(base, w)
local extT = fmt.fmtText(ext, w)
for _, v in ipairs(extT) do
table.insert(baseT, v)
end
return baseT
end
if unicode.len(text) > w then
local lastSpace
for i = 1, w do
if unicode.sub(text, i, i) == " " then
-- Check this isn't an inserted space (unicode safe text format)
local ok = true
if i > 1 then
if unicode.charWidth(unicode.sub(text, i - 1, i - 1)) ~= 1 then
ok = false
end
end
if ok then
lastSpace = i
end
end
end
local baseText, extText
if not lastSpace then
-- Break at a 1-earlier boundary
local wEffect = w
if unicode.charWidth(unicode.sub(text, w, w)) ~= 1 then
-- Guaranteed to be safe, so
wEffect = wEffect - 1
end
baseText = unicode.sub(text, 1, wEffect)
extText = unicode.sub(text, wEffect + 1)
else
baseText = unicode.sub(text, 1, lastSpace - 1)
extText = unicode.sub(text, lastSpace + 1)
end
local full = fmt.fmtText(extText, w)
table.insert(full, 1, fmt.pad(baseText, w))
return full
end
return {fmt.pad(text, w)}
end
}
return neo.wrapMeta(fmt)