From 5b0c085c2d12a82ea1773e7e54985f5451c340dc Mon Sep 17 00:00:00 2001 From: SquidDev Date: Wed, 22 Nov 2017 19:19:33 +0000 Subject: [PATCH] Miscellaneous tweaks to OpenOS (#2636) * Parse Lua REPL inputs with an implicit "return " If an input does not start with a leading "=", this will parse the input with "return " appended and, if that fails, will parse as a normal statement. This allows for normal expressions to be entered into the repl (such as `2 + 2`) but does mean the parse errors for malformed inputs are confusing. For instance, `3 + ` will error at '3' rather than ''. * Do not insert into history if a duplicate This mimics the behaviour of shells such as bash or zsh, where whitespace-only lines are not entered into history, nor are ones equal to the previous input. This makes history navigation slightly easier. --- .../assets/opencomputers/loot/openos/lib/core/lua_shell.lua | 5 ++++- .../resources/assets/opencomputers/loot/openos/lib/tty.lua | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/resources/assets/opencomputers/loot/openos/lib/core/lua_shell.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/core/lua_shell.lua index 48bdeeacd..bcff783ca 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/lib/core/lua_shell.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/lib/core/lua_shell.lua @@ -101,7 +101,10 @@ while tty.isAvailable() do if string.sub(command, 1, 1) == "=" then code, reason = load("return " .. string.sub(command, 2), "=stdin", "t", env) else - code, reason = load(command, "=stdin", "t", env) + code, reason = load("return " .. command, "=stdin", "t", env) + if not code then + code, reason = load(command, "=stdin", "t", env) + end end if code then local result = table.pack(xpcall(code, debug.traceback)) diff --git a/src/main/resources/assets/opencomputers/loot/openos/lib/tty.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/tty.lua index bdfca7144..820585a1f 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/lib/tty.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/lib/tty.lua @@ -32,11 +32,11 @@ function tty.key_down_handler(handler, cursor, char, code) elseif code == keys.enter or code == keys.numpadenter then cursor:move(math.huge) cursor:draw("\n") - if #data > 0 then + if data:find("%S") and data ~= handler[1] then table.insert(handler, 1, data) handler[(tonumber(os.getenv("HISTSIZE")) or 10)+1]=nil - handler[0]=nil end + handler[0]=nil return nil, data .. "\n" elseif code == keys.up or code == keys.down then local ni = handler.index + (code == keys.up and 1 or -1)