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

View File

@ -3,7 +3,7 @@
local fmt local fmt
fmt = { fmt = {
pad = function (t, len, centre, cut) pad = function (t, len, centre, cut, ra)
local l = unicode.len(t) local l = unicode.len(t)
local add = len - l local add = len - l
if add > 0 then if add > 0 then
@ -14,7 +14,11 @@ fmt = {
end end
end end
if cut then 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 end
return t return t
end, end,

View File

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

View File

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

View File

@ -78,10 +78,7 @@ local fstatSwap = false
local workingOnBox = nil local workingOnBox = nil
local function runField(tx, l, r) local function runField(tx, l, r)
local fieldContent = unicode.safeTextFormat(tx) return l .. fmttext.pad(unicode.safeTextFormat(tx), 31, false, true, true) .. r
fieldContent = fmttext.pad(fieldContent, 31, false, false)
fieldContent = unicode.sub(fieldContent, math.max(1, unicode.len(fieldContent) - 30))
return l .. fieldContent .. r
end end
local function actField(tx, ka, kc) local function actField(tx, ka, kc)
if kc == 211 or ka == 8 then 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. into a column of a given width.
It has two functions for this task: It has two functions for this task:
pad(text, len[, centre[, cut]]): pad(text, len[, centre
[, cut[, ra]]]):
Pads the given safeTextFormat'd Pads the given safeTextFormat'd
string to a given width, returning string to a given width, returning
the resulting string. the resulting string.
@ -33,6 +34,11 @@ It has two functions for this task:
be reduced in size if necessary. be reduced in size if necessary.
Otherwise, text that is too long is Otherwise, text that is too long is
not reduced. 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): fmtText(text, width):
Formats the given safeTextFormat'd Formats the given safeTextFormat'd