Make terminal.read() use a table for options

Also change all apps to work with this
This commit is contained in:
WahPlus
2025-10-30 13:29:13 +02:00
parent ca5d1114d2
commit be2ddf1dd4
5 changed files with 58 additions and 51 deletions
+11 -11
View File
@@ -107,7 +107,7 @@ local function doChecks(package)
if agcfg[package].dependencies then if agcfg[package].dependencies then
for _, dependency in ipairs(agcfg[package].dependencies) do for _, dependency in ipairs(agcfg[package].dependencies) do
if not agReg[dependency] and not agcfg[dependency] then if not agReg[dependency] and not agcfg[dependency] then
local response = terminal.read(nil, "\27[91mPackage " .. package .. " requires dependency " .. dependency .. " that does not exist.\n[A - Abort/s - Skip]") local response = terminal.read({prefix = "\27[91mPackage " .. package .. " requires dependency " .. dependency .. " that does not exist.\n[A - Abort/s - Skip]"})
if response:lower() ~= "s" then if response:lower() ~= "s" then
fs.remove("/argentum/store/" .. package) fs.remove("/argentum/store/" .. package)
return false return false
@@ -166,7 +166,7 @@ local function installPackage(package, overwriteFlag)
if agcfg[package].dependencies then if agcfg[package].dependencies then
for _, dependency in ipairs(agcfg[package].dependencies) do for _, dependency in ipairs(agcfg[package].dependencies) do
if not agReg[dependency] and not agcfg[dependency] then if not agReg[dependency] and not agcfg[dependency] then
local response = terminal.read(nil, "\27[91mPackage " .. package .. " requires dependency " .. dependency .. " that does not exist.\n[A - Abort/s - Skip]") local response = terminal.read({prefix = "\27[91mPackage " .. package .. " requires dependency " .. dependency .. " that does not exist.\n[A - Abort/s - Skip]"})
if response:lower() ~= "s" then if response:lower() ~= "s" then
fs.remove("/argentum/store/" .. package) fs.remove("/argentum/store/" .. package)
return false return false
@@ -199,7 +199,7 @@ local function installPackage(package, overwriteFlag)
local data, errorMessage = getFile(source .. agcfg[package].maindir .. file) local data, errorMessage = getFile(source .. agcfg[package].maindir .. file)
if not data then if not data then
clearProgress() clearProgress()
local response = terminal.read(nil, "\27[91mCould not fetch " .. file .. ": " .. errorMessage .. "\n\27[0m[a - Abort/R - Retry/s - Skip]") local response = terminal.read({prefix = "\27[91mCould not fetch " .. file .. ": " .. errorMessage .. "\n\27[0m[a - Abort/R - Retry/s - Skip]"})
if response:lower() == "a" then if response:lower() == "a" then
fs.remove("/argentum/store/" .. package) fs.remove("/argentum/store/" .. package)
return false return false
@@ -258,7 +258,7 @@ local function removePackage(package)
local result, errorMessage = fs.remove(line:sub(2)) local result, errorMessage = fs.remove(line:sub(2))
if not result then if not result then
clearProgress() clearProgress()
local response = terminal.read(nil, "\27[91mFailed to remove " .. line:sub(2) .. ": " .. errorMessage .. "\n\27[0m[a - Abort/r - Retry/S - Skip]") local response = terminal.read({prefix = "\27[91mFailed to remove " .. line:sub(2) .. ": " .. errorMessage .. "\n\27[0m[a - Abort/r - Retry/S - Skip]"})
if response:lower() == "a" then if response:lower() == "a" then
return false return false
elseif response:lower() == "r" then elseif response:lower() == "r" then
@@ -274,7 +274,7 @@ local function removePackage(package)
local handle, data, tmpdata = fs.open("/argentum/store/" .. package .. "/files/" .. line:sub(2), "r"), "", nil local handle, data, tmpdata = fs.open("/argentum/store/" .. package .. "/files/" .. line:sub(2), "r"), "", nil
if not handle then if not handle then
clearProgress() clearProgress()
local response = terminal.read(nil, "\27[91mFailed to revert " .. line:sub(2) .. ": " .. data .. "\n\27[0m[a - Abort/R - Retry/s - Skip]") -- this is pretty stupid but i think the error message would get pushed to data local response = terminal.read({prefix = "\27[91mFailed to revert " .. line:sub(2) .. ": " .. data .. "\n\27[0m[a - Abort/R - Retry/s - Skip]"}) -- this is pretty stupid but i think the error message would get pushed to data
if response:lower() == "a" then if response:lower() == "a" then
return false return false
elseif response:lower() == "s" then elseif response:lower() == "s" then
@@ -360,7 +360,7 @@ if command == "install" then
local answer local answer
if #fails == 0 then if #fails == 0 then
print("Packages that will be installed: " .. table.concat(packageList, ", ")) print("Packages that will be installed: " .. table.concat(packageList, ", "))
if terminal.read(nil, "Would you like to proceed? [Y/n] "):lower() == "n" then if terminal.read({prefix = "Would you like to proceed? [Y/n] "}):lower() == "n" then
return return
end end
elseif #packageList == 0 then elseif #packageList == 0 then
@@ -370,7 +370,7 @@ if command == "install" then
print("Some packages cannot be installed.") print("Some packages cannot be installed.")
print("Packages that will be installed: " .. table.concat(packageList, ", ")) print("Packages that will be installed: " .. table.concat(packageList, ", "))
print("Packages that cannot be installed: " .. table.concat(fails, ", ")) print("Packages that cannot be installed: " .. table.concat(fails, ", "))
if terminal.read(nil, "Would you like to proceed? [y/N] "):lower() ~= "y" then if terminal.read({prefix = "Would you like to proceed? [y/N] "}):lower() ~= "y" then
return return
end end
end end
@@ -452,7 +452,7 @@ elseif command == "remove" then
local answer local answer
if #fails == 0 then if #fails == 0 then
print("Packages that will be removed: " .. table.concat(packageList, ", ")) print("Packages that will be removed: " .. table.concat(packageList, ", "))
if terminal.read(nil, "Would you like to proceed? [Y/n] "):lower() == "n" then if terminal.read({prefix = "Would you like to proceed? [Y/n] "}):lower() == "n" then
return return
end end
elseif #packageList == 0 then elseif #packageList == 0 then
@@ -462,7 +462,7 @@ elseif command == "remove" then
print("Some packages cannot be removed.") print("Some packages cannot be removed.")
print("Packages that will be removed: " .. table.concat(packageList, ", ")) print("Packages that will be removed: " .. table.concat(packageList, ", "))
print("Packages that cannot be removed: " .. table.concat(fails, ", ")) print("Packages that cannot be removed: " .. table.concat(fails, ", "))
if terminal.read(nil, "Would you like to proceed? [y/N] "):lower() ~= "y" then if terminal.read({prefix = "Would you like to proceed? [y/N] "}):lower() ~= "y" then
return return
end end
end end
@@ -560,14 +560,14 @@ elseif command == "update" then
end end
elseif #fails == 0 then elseif #fails == 0 then
print("Packages that will be updated: " .. table.concat(packageList, ", ")) print("Packages that will be updated: " .. table.concat(packageList, ", "))
if terminal.read(nil, "Would you like to proceed? [Y/n] "):lower() == "n" then if terminal.read({prefix = "Would you like to proceed? [Y/n] "}):lower() == "n" then
return return
end end
else else
print("Some packages cannot be updated.") print("Some packages cannot be updated.")
print("Packages that will be updated: " .. table.concat(packageList, ", ")) print("Packages that will be updated: " .. table.concat(packageList, ", "))
print("Packages that cannot be updated: " .. table.concat(fails, ", ")) print("Packages that cannot be updated: " .. table.concat(fails, ", "))
if terminal.read(nil, "Would you like to proceed? [y/N] "):lower() ~= "y" then if terminal.read({prefix = "Would you like to proceed? [y/N] "}):lower() ~= "y" then
return return
end end
end end
+1 -1
View File
@@ -43,7 +43,7 @@ repeat
if fs.isDirectory(saveLocation) then if fs.isDirectory(saveLocation) then
print("\27[91mThe specified location is a directory.") print("\27[91mThe specified location is a directory.")
elseif fs.exists(saveLocation) then elseif fs.exists(saveLocation) then
local answer = read(nil, "\27[91mThere is already a file at the specified directory. Overwrite it? [Y/n]") local answer = terminal.read({prefix = "\27[91mThere is already a file at the specified directory. Overwrite it? [Y/n]"})
if answer:lower() ~= "n" then if answer:lower() ~= "n" then
saveLocationOK = true saveLocationOK = true
end end
+3 -3
View File
@@ -262,12 +262,12 @@ local function save()
gpu.setForeground(0) gpu.setForeground(0)
gpu.set(1, height - 1, string.rep(" ", width)) gpu.set(1, height - 1, string.rep(" ", width))
terminal.setCursorPos(1, height - 1) terminal.setCursorPos(1, height - 1)
local savepath = terminal.read(nil, "\27[107m\27[30mSave location: ", filepath) local savepath = terminal.read({prefix = "\27[107m\27[30mSave location: ", defaultText = filepath})
gpu.setBackground(0xFFFFFF) gpu.setBackground(0xFFFFFF)
gpu.setForeground(0) gpu.setForeground(0)
if fs.exists(savepath) then if fs.exists(savepath) then
gpu.set(1, height - 1, string.rep(" ", width)) gpu.set(1, height - 1, string.rep(" ", width))
local answer = terminal.read(nil, "\27[107m\27[30mFile already exists. Overwrite it? [Y/n] ") local answer = terminal.read({prefix = "\27[107m\27[30mFile already exists. Overwrite it? [Y/n] "})
if answer:lower() == "n" then if answer:lower() == "n" then
gpu.setBackground(0xFFFFFF) gpu.setBackground(0xFFFFFF)
gpu.setForeground(0) gpu.setForeground(0)
@@ -301,7 +301,7 @@ while true do
if specialKey == "x" then if specialKey == "x" then
if changesMade then if changesMade then
terminal.setCursorPos(1, height - 1) terminal.setCursorPos(1, height - 1)
local response = terminal.read(nil, "\27[107m\27[30mWould you like to save changes? [Y/n] ") local response = terminal.read({prefix = "\27[107m\27[30mWould you like to save changes? [Y/n] "})
if response:lower() ~= "n" then if response:lower() ~= "n" then
save() save()
end end
+1 -1
View File
@@ -46,7 +46,7 @@ print(string.format("\27[44m%s\27[0m shell", _VERSION))
print('Type "exit" to exit.') print('Type "exit" to exit.')
while true do while true do
local command = terminal.read("lua", "\27[44mlua>\27[0m ") local command = terminal.read({readHistoryType = "lua", prefix = "\27[44mlua>\27[0m "})
if command == "exit" then if command == "exit" then
coroutine.yield() coroutine.yield()
return return
+42 -35
View File
@@ -254,36 +254,43 @@ function module.init()
cursorPosX, cursorPosY = 1, 1 cursorPosX, cursorPosY = 1, 1
end end
function _G._PUBLIC.terminal.read(readHistoryType, prefix, defaultText, maxChars) function _G._PUBLIC.terminal.read(options)
checkArg(1, readHistoryType, "string", "nil") checkArg(1, options, "table", "nil")
checkArg(2, prefix, "string", "nil") local function checkOption(name, value, neededType)
checkArg(3, defaultText, "string", "nil") assert(type(value) == "string" or not value, ("%s option must be %s, %s provided"):format(name, neededType, type(value)))
checkArg(4, maxChars, "number", "nil") end
maxChars = maxChars or math.huge if options then
checkOption("readHistoryType", options.readHistoryType, "string")
checkOption("prefix", options.prefix, "string")
checkOption("maxChars", options.maxChars, "number")
checkOption("defaultText", options.defaultText, "string")
end
local text = defaultText or "" options.maxChars = options.maxChars or math.huge
local text = options.defaultText or ""
local historyIdx local historyIdx
if readHistoryType then if options.readHistoryType then
if not readHistory[readHistoryType] then if not readHistory[options.readHistoryType] then
readHistory[readHistoryType] = {text} readHistory[options.readHistoryType] = {text}
elseif readHistory[readHistoryType][#readHistory[readHistoryType] ] ~= text then elseif readHistory[options.readHistoryType][#readHistory[options.readHistoryType] ] ~= text then
table.insert(readHistory[readHistoryType], text) table.insert(readHistory[options.readHistoryType], text)
end end
historyIdx = #readHistory[readHistoryType] historyIdx = #readHistory[options.readHistoryType]
end end
local function updateHistory() local function updateHistory()
if not readHistoryType then return end if not options.readHistoryType then return end
readHistory[readHistoryType][historyIdx]=text readHistory[options.readHistoryType][historyIdx]=text
end end
local cur = unicode.len(text)+1 local cur = unicode.len(text)+1
if prefix then _PUBLIC.terminal.write(prefix) end if options.prefix then _PUBLIC.terminal.write(options.prefix) end
local startX, startY = cursorPosX, cursorPosY local startX, startY = cursorPosX, cursorPosY
local fg, bg = gpu.getForeground(), gpu.getBackground() local fg, bg = gpu.getForeground(), gpu.getBackground()
local cursorBlink = true local cursorBlink = true
local function get(idx) local function get(idx) -- FIXME: Why is this here if it's unused?
idx=startX+idx-1 idx=startX+idx-1
return gpu.get(idx%width,startY+(idx//width)) return gpu.get(idx%width,startY+(idx//width))
end end
@@ -294,7 +301,7 @@ function module.init()
end end
return math.min(y,height) return math.min(y,height)
end end
local function set(idx,chr,rev) local function set(idx,chr,rev) -- FIXME: Very clear variable naming... I would fix it but I don't even know what they mean.
if chr==nil or chr=="" then return end if chr==nil or chr=="" then return end
if rev then if rev then
gpu.setForeground(bg) gpu.setForeground(bg)
@@ -321,13 +328,13 @@ function module.init()
end end
local function add(chr) local function add(chr)
if type(chr)~="string" or #chr==0 then return end if type(chr)~="string" or #chr==0 then return end
if unicode.len(text)>=maxChars then return end if unicode.len(text)>=options.maxChars then return end
if maxChars<math.huge then if options.maxChars<math.huge then
chr=unicode.sub(chr,1,maxChars-unicode.len(text)) chr=unicode.sub(chr,1,options.maxChars-unicode.len(text))
end end
text=unicode.sub(text,1,cur-1)..chr..unicode.sub(text,cur) text=unicode.sub(text,1,cur-1)..chr..unicode.sub(text,cur)
set(curPos(cur),chr,false) set(curPos(cur),chr,false)
cur=math.min(cur+unicode.len(chr),maxChars+1) cur=math.min(cur+unicode.len(chr),options.maxChars+1)
set(curPos(cur),strDef(unicode.sub(text,cur,cur)," "),true) set(curPos(cur),strDef(unicode.sub(text,cur,cur)," "),true)
cursorBlink = true cursorBlink = true
set(curPos(cur+1),unicode.sub(text,cur+1),false) set(curPos(cur+1),unicode.sub(text,cur+1),false)
@@ -402,12 +409,12 @@ function module.init()
local ctrlDown = _PUBLIC.keyboard.getCtrlDown() local ctrlDown = _PUBLIC.keyboard.getCtrlDown()
if args and args[1] == "key_down" and args[4] then if args and args[1] == "key_down" and args[4] then
local key = _PUBLIC.keyboard.keys[args[4]] local key = _PUBLIC.keyboard.keys[args[4]]
if key=="up" and readHistoryType then if key=="up" and options.readHistoryType then
historyIdx=math.max(historyIdx-1,1) historyIdx=math.max(historyIdx-1,1)
reprint(readHistory[readHistoryType][historyIdx]) reprint(readHistory[options.readHistoryType][historyIdx])
elseif key=="down" and readHistoryType then elseif key=="down" and options.readHistoryType then
historyIdx=math.min(historyIdx+1,#readHistory[readHistoryType]) historyIdx=math.min(historyIdx+1,#readHistory[options.readHistoryType])
reprint(readHistory[readHistoryType][historyIdx]) reprint(readHistory[options.readHistoryType][historyIdx])
elseif key=="left" and ctrlDown then elseif key=="left" and ctrlDown then
moveWord(-1) moveWord(-1)
elseif key=="right" and ctrlDown then elseif key=="right" and ctrlDown then
@@ -460,16 +467,16 @@ function module.init()
::continue:: ::continue::
end end
if readHistoryType then if options.readHistoryType then
if readHistory[readHistoryType][#readHistory[readHistoryType]]=="" then if readHistory[options.readHistoryType][#readHistory[options.readHistoryType]]=="" then
table.remove(readHistory[readHistoryType],#readHistory[readHistoryType]) table.remove(readHistory[options.readHistoryType],#readHistory[options.readHistoryType])
end end
if historyIdx<#readHistory[readHistoryType] then if historyIdx<#readHistory[options.readHistoryType] then
table.remove(readHistory[readHistoryType],historyIdx) table.remove(readHistory[options.readHistoryType],historyIdx)
table.insert(readHistory[readHistoryType],text) table.insert(readHistory[options.readHistoryType],text)
end end
while #readHistory[readHistoryType] > 50 do while #readHistory[options.readHistoryType] > 50 do
table.remove(readHistory[readHistoryType], 1) table.remove(readHistory[options.readHistoryType], 1)
end end
end end