1
0
mirror of https://github.com/20kdc/OC-KittenOS.git synced 2024-11-23 10:58:06 +11:00

Fix wget yielder bug, make sys-filedialog fit in with NeoUX, add 'ra' parameter to fmttext

This commit is contained in:
20kdc 2018-04-25 19:01:32 +01:00
parent 26be65e1bb
commit 6be34ec7e6
6 changed files with 88 additions and 64 deletions

View File

@ -8,55 +8,75 @@ local primaryINet = neo.requireAccess("c.internet", "internet access").list()()
-- Enter URL dialog
local running = true
local sRunning = true
-- useful to perform a system update
local url = "http://20kdc.duckdns.org/neo/inst.lua"
local w = neoux.create(25, 3, nil, neoux.tcwindow(25, 3, {
neoux.tcrawview(1, 1, {"URL to download?"}),
neoux.tcfield(1, 2, 25, function (t)
url = t or url
return url
end),
neoux.tcbutton(16, 3, "Confirm", function (w)
local nurl = url
local fd = neoux.fileDialog(true)
if not fd then return end
-- download!
local req, err = primaryINet.request(nurl)
if not req then
neoux.startDialog("failed request:\n" .. tostring(err))
end
-- OpenComputers#535
req.finishConnect()
while true do
local n, n2 = req.read(neo.readBufSize)
if not n then
req.close()
fd.close()
if n2 then
neoux.startDialog("failed download:\n" .. tostring(n2))
return
else
break
end
else
if n == "" then
yielder()
else
local o, r = fd.write(n)
if not o then
req.close()
fd.close()
neoux.startDialog("failed write:\n" .. tostring(r))
local function doWorking()
return 25, 1, nil, neoux.tcwindow(25, 1, {
neoux.tcrawview(1, 1, {"Downloading now..."}),
}, function (w)
sRunning = false
end, 0xFFFFFF, 0)
end
local function doMainWin()
return 25, 3, nil, neoux.tcwindow(25, 3, {
neoux.tcrawview(1, 1, {"URL to download?"}),
neoux.tcfield(1, 2, 25, function (t)
url = t or url
return url
end),
neoux.tcbutton(16, 3, "Confirm", function (w)
sRunning = true
w.reset(doWorking())
local nurl = url
local fd = neoux.fileDialog(true)
if not fd then return end
-- download!
local req, err = primaryINet.request(nurl)
if not req then
neoux.startDialog("failed request:\n" .. tostring(err))
w.reset(doMainWin())
return
end
-- OpenComputers#535
req.finishConnect()
while sRunning do
local n, n2 = req.read(neo.readBufSize)
if not n then
req.close()
fd.close()
if n2 then
neoux.startDialog("failed download:\n" .. tostring(n2))
w.reset(doMainWin())
return
else
break
end
else
if n == "" then
event.sleepTo(os.uptime() + 0.05)
else
local o, r = fd.write(n)
if not o then
req.close()
fd.close()
neoux.startDialog("failed write:\n" .. tostring(r))
w.reset(doMainWin())
return
end
end
end
end
end
end)
}, function (w)
w.close()
running = false
end, 0xFFFFFF, 0))
pcall(req.close)
pcall(fd.close)
w.reset(doMainWin())
end)
}, function (w)
w.close()
running = false
end, 0xFFFFFF, 0)
end
local w = neoux.create(doMainWin)
while running do
event.pull()

View File

@ -3,7 +3,7 @@
local fmt
fmt = {
pad = function (t, len, centre, cut)
pad = function (t, len, centre, cut, ra)
local l = unicode.len(t)
local add = len - l
if add > 0 then
@ -14,7 +14,11 @@ fmt = {
end
end
if cut then
t = unicode.sub(t, 1, len)
local i = 0
if ra then
i = -math.min(0, add)
end
t = unicode.sub(t, i + 1, len + i)
end
return t
end,

View File

@ -378,9 +378,7 @@ newNeoux = function (event, neo)
bg = fg1
end
local text = unicode.safeTextFormat(textprop())
local txl = unicode.len(text)
local start = math.max(1, (txl - (w - 2)) + 1)
text = "[" .. neoux.pad(unicode.sub(text, start, start + (w - 3)), w - 2, false, true) .. "]"
text = "[" .. neoux.pad(text, w - 2, false, true, true) .. "]"
window.span(x, y, text, bg, fg)
end
}

View File

@ -45,27 +45,26 @@ local function prepareNodeI(node)
--
local function format(a)
if a <= 1 then
return true, true, node.name
return false, fmt.pad(unicode.safeTextFormat(node.name), w, true, true)
end
local camY = math.max(1, selection - 3)
local idx = a + camY - 2
if node.unknownAvailable then
if idx == #l + 1 then
return selection == #l + 1, false, ":" .. unknownTx
end
local utx = (" "):rep(w)
if node.unknownAvailable and idx == #l + 1 then
utx = "<OK>[" .. fmt.pad(unicode.safeTextFormat(unknownTx), w - 6, false, true, true) .. "]"
end
if l[idx] then
return selection == idx, false, l[idx][1]
utx = "<" .. fmt.pad(unicode.safeTextFormat(l[idx][1]), w - 2, false, true) .. ">"
end
return true, true, "~~~"
return selection == idx, utx
end
local function updateLine(wnd, a)
local colA, colB = 0xFFFFFF, 0
local sel, cen, text = format(a)
local sel, text = format(a)
if sel then
colB, colA = 0xFFFFFF, 0
end
wnd.span(1, a, fmt.pad(unicode.safeTextFormat(text), w, cen, true), colA, colB)
wnd.span(1, a, text, colA, colB)
end
local function flush(wnd)
for i = 1, h do
@ -84,7 +83,7 @@ local function prepareNodeI(node)
elseif kc == 208 then
h = h + 1
elseif kc == 203 then
w = math.max(1, w - 1)
w = math.max(6, w - 1)
elseif kc == 205 then
w = w + 1
else
@ -156,7 +155,7 @@ local function prepareNodeI(node)
if node.unknownAvailable then
max = max + 1
end
if ns == selection and selection ~= #l + 1 then
if ns == selection and ((selection ~= #l + 1) or (a <= 4)) then
key(wnd, 13, 0, true)
else
selection = math.min(math.max(1, ns), max)

View File

@ -78,10 +78,7 @@ local fstatSwap = false
local workingOnBox = nil
local function runField(tx, l, r)
local fieldContent = unicode.safeTextFormat(tx)
fieldContent = fmttext.pad(fieldContent, 31, false, false)
fieldContent = unicode.sub(fieldContent, math.max(1, unicode.len(fieldContent) - 30))
return l .. fieldContent .. r
return l .. fmttext.pad(unicode.safeTextFormat(tx), 31, false, true, true) .. r
end
local function actField(tx, ka, kc)
if kc == 211 or ka == 8 then

View File

@ -20,7 +20,8 @@ Essentially, it's job is to take some
into a column of a given width.
It has two functions for this task:
pad(text, len[, centre[, cut]]):
pad(text, len[, centre
[, cut[, ra]]]):
Pads the given safeTextFormat'd
string to a given width, returning
the resulting string.
@ -33,6 +34,11 @@ It has two functions for this task:
be reduced in size if necessary.
Otherwise, text that is too long is
not reduced.
If ra is true, the text is "semi-
right-aligned" - it's left-aligned
as normal *if it can be*, but if
it exceeds the width, it becomes
right-aligned.
fmtText(text, width):
Formats the given safeTextFormat'd