1
0
mirror of https://github.com/20kdc/OC-KittenOS.git synced 2024-11-23 19:08:05 +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,15 +8,26 @@ 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()
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.tcrawview(1, 1, {"URL to download?"}),
neoux.tcfield(1, 2, 25, function (t) neoux.tcfield(1, 2, 25, function (t)
url = t or url url = t or url
return url return url
end), end),
neoux.tcbutton(16, 3, "Confirm", function (w) neoux.tcbutton(16, 3, "Confirm", function (w)
sRunning = true
w.reset(doWorking())
local nurl = url local nurl = url
local fd = neoux.fileDialog(true) local fd = neoux.fileDialog(true)
if not fd then return end if not fd then return end
@ -24,39 +35,48 @@ local w = neoux.create(25, 3, nil, neoux.tcwindow(25, 3, {
local req, err = primaryINet.request(nurl) local req, err = primaryINet.request(nurl)
if not req then if not req then
neoux.startDialog("failed request:\n" .. tostring(err)) neoux.startDialog("failed request:\n" .. tostring(err))
w.reset(doMainWin())
return
end end
-- OpenComputers#535 -- OpenComputers#535
req.finishConnect() req.finishConnect()
while true do while sRunning do
local n, n2 = req.read(neo.readBufSize) local n, n2 = req.read(neo.readBufSize)
if not n then if not n then
req.close() req.close()
fd.close() fd.close()
if n2 then if n2 then
neoux.startDialog("failed download:\n" .. tostring(n2)) neoux.startDialog("failed download:\n" .. tostring(n2))
w.reset(doMainWin())
return return
else else
break break
end end
else else
if n == "" then if n == "" then
yielder() event.sleepTo(os.uptime() + 0.05)
else else
local o, r = fd.write(n) local o, r = fd.write(n)
if not o then if not o then
req.close() req.close()
fd.close() fd.close()
neoux.startDialog("failed write:\n" .. tostring(r)) neoux.startDialog("failed write:\n" .. tostring(r))
w.reset(doMainWin())
return return
end end
end end
end end
end end
pcall(req.close)
pcall(fd.close)
w.reset(doMainWin())
end) end)
}, function (w) }, function (w)
w.close() w.close()
running = false running = false
end, 0xFFFFFF, 0)) 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