From acd5603844f9e89c526db02d1814f2eeab10d7e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Mon, 7 Oct 2013 05:05:51 +0200 Subject: [PATCH] re-added clipboard pasting (now for term.read()) and fixed clipboard being sent twice (only on key down again now) --- .../opencomputers/lua/drivers/filesystem.lua | 2 +- assets/opencomputers/lua/drivers/keyboard.lua | 4 +- assets/opencomputers/lua/rom/api/term.lua | 50 +++++++++++-------- li/cil/oc/client/gui/Screen.scala | 6 ++- li/cil/oc/server/component/Computer.scala | 6 ++- 5 files changed, 42 insertions(+), 26 deletions(-) diff --git a/assets/opencomputers/lua/drivers/filesystem.lua b/assets/opencomputers/lua/drivers/filesystem.lua index a86c00e77..6572e5297 100644 --- a/assets/opencomputers/lua/drivers/filesystem.lua +++ b/assets/opencomputers/lua/drivers/filesystem.lua @@ -612,7 +612,7 @@ end function stdinStream:read(n) local result = term.read(stdinHistory) - if #stdinHistory > 10 then + while #stdinHistory > 10 do table.remove(stdinHistory, 1) end return result diff --git a/assets/opencomputers/lua/drivers/keyboard.lua b/assets/opencomputers/lua/drivers/keyboard.lua index c600882ad..f8c0928db 100644 --- a/assets/opencomputers/lua/drivers/keyboard.lua +++ b/assets/opencomputers/lua/drivers/keyboard.lua @@ -136,5 +136,5 @@ for k, v in pairs(driver.keyboard.keys) do end function driver.keyboard.keys.isControl(char) - return char < 0x20 or (char >= 0x7F and char <= 0x9F) -end \ No newline at end of file + return type(char) == "number" and (char < 0x20 or (char >= 0x7F and char <= 0x9F)) +end diff --git a/assets/opencomputers/lua/rom/api/term.lua b/assets/opencomputers/lua/rom/api/term.lua index 9e3a4627a..de5481f24 100644 --- a/assets/opencomputers/lua/rom/api/term.lua +++ b/assets/opencomputers/lua/rom/api/term.lua @@ -171,12 +171,23 @@ function term.read(history) driver.gpu.set(term.gpu(), w, y, char) end end + local function scrollEnd() + local w = term.size() + cursor = history[current]:len() + 1 + scroll = math.max(0, cursor - (w - (start - 1))) + render() + end local function copyIfNecessary() if current ~= #history then history[#history] = history[current] current = #history end end + local function updateCursor() + term.cursor(start - 1 + cursor - scroll, y) + term.cursorBlink(cursor <= history[current]:len() and + history[current]:sub(cursor, cursor) or " ") + end local function handleKeyPress(char, code) local w, h = term.size() local cancel = false @@ -224,24 +235,18 @@ function term.read(history) end elseif code == keys["end"] then if cursor < history[current]:len() + 1 then - cursor = history[current]:len() + 1 - scroll = math.max(0, cursor - (w - (start - 1))) - render() + scrollEnd() end elseif code == keys.up then if current > 1 then current = current - 1 - cursor = history[current]:len() + 1 - scroll = math.max(0, cursor - (w - (start - 1))) - render() + scrollEnd() end cancel = current == 1 elseif code == keys.down then if current < #history then current = current + 1 - cursor = history[current]:len() + 1 - scroll = math.max(0, cursor - (w - (start - 1))) - render() + scrollEnd() end cancel = current == #history elseif code == keys.enter then @@ -266,9 +271,7 @@ function term.read(history) scrollRight() end end - term.cursor(start - 1 + cursor - scroll, y) - term.cursorBlink(cursor <= history[current]:len() and - history[current]:sub(cursor, cursor) or " ") + updateCursor() return cancel end local function onKeyDown(_, address, char, code) @@ -295,19 +298,25 @@ function term.read(history) keyRepeat = event.cancel(keyRepeat) end end - --[[local function onClipboard(_, address, value) - if address ~= term.keyboard then + local function onClipboard(_, address, value) + if address ~= term.keyboard() then return end - if current ~= #history then - history[#history] = history[current] - current = #history + copyIfNecessary() + term.cursorBlink(false) + local l = value:find("\n", 1, true) + if l then + history[current] = history[current] .. value:sub(1, l - 1) + result = history[current] .. "\n" + else + history[current] = history[current] .. value + scrollEnd() + updateCursor() end - history[current] = history[current] .. value - done = done or value:find("\n", 1, true) >= 0 - end]] + end event.listen("key_down", onKeyDown) event.listen("key_up", onKeyUp) + event.listen("clipboard", onClipboard) term.cursorBlink(true) while not result do coroutine.sleep() @@ -317,6 +326,7 @@ function term.read(history) end event.ignore("key_down", onKeyDown) event.ignore("key_up", onKeyUp) + event.ignore("clipboard", onClipboard) term.cursorBlink(false) print() return result diff --git a/li/cil/oc/client/gui/Screen.scala b/li/cil/oc/client/gui/Screen.scala index 7c8a367c0..d13cbc63c 100644 --- a/li/cil/oc/client/gui/Screen.scala +++ b/li/cil/oc/client/gui/Screen.scala @@ -56,8 +56,10 @@ class Screen(val tileEntity: tileentity.Screen) extends MCGuiScreen { val char = Keyboard.getEventCharacter if (code != Keyboard.KEY_ESCAPE && code != Keyboard.KEY_F11) - if (code == Keyboard.KEY_INSERT && MCGuiScreen.isShiftKeyDown) - PacketSender.sendClipboard(tileEntity, MCGuiScreen.getClipboardString) + if (code == Keyboard.KEY_INSERT && MCGuiScreen.isShiftKeyDown) { + if (Keyboard.getEventKeyState) + PacketSender.sendClipboard(tileEntity, MCGuiScreen.getClipboardString) + } else if (Keyboard.getEventKeyState) { PacketSender.sendKeyDown(tileEntity, char, code) } diff --git a/li/cil/oc/server/component/Computer.scala b/li/cil/oc/server/component/Computer.scala index 83fccd864..05ccf3a38 100644 --- a/li/cil/oc/server/component/Computer.scala +++ b/li/cil/oc/server/component/Computer.scala @@ -783,7 +783,11 @@ class Computer(val owner: Computer.Environment) extends Persistable with Runnabl } else { lua.setTotalMemory(Int.MaxValue) - message = Some(lua.toString(3)) + val error = lua.toString(3) + if (error != null) + message = Some(error) + else + message = Some("unknown error") } close() })