From 3c62599d8d4c4b770086c7b47679dad5933291b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Thu, 15 Jan 2015 15:13:09 +0000 Subject: [PATCH] Added match check to term.read It can be ethier string or function. If it's string user can confirm only when entered text matches the string. If thes param is function, the function gets called when user clicks enter key, first parameter for it is entered text, it should return a value that decides if given text should is accepted(true), or not(false/nil) --- .../opencomputers/loot/OpenOS/lib/term.lua | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/main/resources/assets/opencomputers/loot/OpenOS/lib/term.lua b/src/main/resources/assets/opencomputers/loot/OpenOS/lib/term.lua index 03a53556e..07408ad51 100644 --- a/src/main/resources/assets/opencomputers/loot/OpenOS/lib/term.lua +++ b/src/main/resources/assets/opencomputers/loot/OpenOS/lib/term.lua @@ -108,10 +108,11 @@ function term.isAvailable() return component.isAvailable("gpu") and component.isAvailable("screen") end -function term.read(history, dobreak, hint, pwchar) +function term.read(history, dobreak, hint, pwchar, match) checkArg(1, history, "table", "nil") checkArg(3, hint, "function", "table", "nil") checkArg(4, pwchar, "string", "nil") + checkArg(5, match, "string", "function", "nil") history = history or {} table.insert(history, "") local offset = term.getCursor() - 1 @@ -327,12 +328,16 @@ function term.read(history, dobreak, hint, pwchar) elseif code == keyboard.keys.tab and hint then tab(keyboard.isShiftDown() and -1 or 1) elseif code == keyboard.keys.enter then - local cbx, cby = getCursor() - if cby ~= #history then -- bring entry to front - history[#history] = line() - table.remove(history, cby) + if not match or (type(match) == "string" and match:match(line() or "")) or (type(match) == "function" and match(line() or "")) then + local cbx, cby = getCursor() + if cby ~= #history then -- bring entry to front + history[#history] = line() + table.remove(history, cby) + end + return true, history[#history] .. "\n" + else + computer.beep(2000, 0.1) end - return true, history[#history] .. "\n" elseif keyboard.isControlDown() and code == keyboard.keys.d then if line() == "" then history[#history] = ""