From 85b740f4845d5c976cf5a90cba8130b3da913e6c Mon Sep 17 00:00:00 2001 From: liquid Date: Fri, 12 Jul 2019 22:54:37 -0500 Subject: [PATCH] Added term.getLine and window.getLine --- .../dan200/computercraft/core/apis/TermAPI.java | 16 ++++++++++++++++ .../assets/computercraft/lua/rom/apis/window.lua | 10 ++++++++++ .../resources/test-rom/spec/apis/term_spec.lua | 9 +++++++++ .../resources/test-rom/spec/apis/window_spec.lua | 11 +++++++++++ 4 files changed, 46 insertions(+) diff --git a/src/main/java/dan200/computercraft/core/apis/TermAPI.java b/src/main/java/dan200/computercraft/core/apis/TermAPI.java index 382ae4069..4a26d36e2 100644 --- a/src/main/java/dan200/computercraft/core/apis/TermAPI.java +++ b/src/main/java/dan200/computercraft/core/apis/TermAPI.java @@ -67,6 +67,7 @@ public String[] getMethodNames() "nativePaletteColour", "nativePaletteColor", "getCursorBlink", + "getLine" }; } @@ -279,6 +280,21 @@ public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull O case 25: // getCursorBlink return new Object[] { m_terminal.getCursorBlink() }; + case 26: + // getLine + int y = getInt( args, 0 ) - 1; + if ( y < 0 || y >= m_terminal.getHeight() ) + { + throw new LuaException( "Line is out of range." ); + } + String line, lineTextColour, lineBackgroundColour; + synchronized (m_terminal) + { + line = m_terminal.getLine( y ).read(); + lineTextColour = m_terminal.getTextColourLine( y ).read(); + lineBackgroundColour = m_terminal.getBackgroundColourLine( y ).read(); + } + return new Object[] { line, lineTextColour, lineBackgroundColour }; default: return null; } diff --git a/src/main/resources/assets/computercraft/lua/rom/apis/window.lua b/src/main/resources/assets/computercraft/lua/rom/apis/window.lua index d3ecf0915..8ac9674dd 100644 --- a/src/main/resources/assets/computercraft/lua/rom/apis/window.lua +++ b/src/main/resources/assets/computercraft/lua/rom/apis/window.lua @@ -388,6 +388,16 @@ function create( parent, nX, nY, nWidth, nHeight, bStartVisible ) return nBackgroundColor end + function window.getLine( nY ) + if type(nY) ~= "number" then expect(1, nY, "number") end + + if nY < 1 or nY > nHeight then + error( "Line is out of range.", 2 ) + end + + return tLines[nY].text, tLines[nY].textColor, tLines[nY].backgroundColor + end + -- Other functions function window.setVisible( bVis ) if type(bVis) ~= "boolean" then expect(1, bVis, "boolean") end diff --git a/src/test/resources/test-rom/spec/apis/term_spec.lua b/src/test/resources/test-rom/spec/apis/term_spec.lua index 769fcb867..2bc4f85ac 100644 --- a/src/test/resources/test-rom/spec/apis/term_spec.lua +++ b/src/test/resources/test-rom/spec/apis/term_spec.lua @@ -9,4 +9,13 @@ describe("The term library", function() :eq("term is not a recommended redirect target, try term.current() instead") end) end) + + describe("term.getLine", function() + it("validates arguments", function() + local _, y = term.getSize() + expect.error(term.getLine, nil):eq("bad argument #1 (expected number, got nil)") + expect.error(term.getLine, 0):eq("Line is out of range.") + expect.error(term.getLine, y + 1):eq("Line is out of range.") + end) + end) end) diff --git a/src/test/resources/test-rom/spec/apis/window_spec.lua b/src/test/resources/test-rom/spec/apis/window_spec.lua index 16fe229e1..b6102e841 100644 --- a/src/test/resources/test-rom/spec/apis/window_spec.lua +++ b/src/test/resources/test-rom/spec/apis/window_spec.lua @@ -120,4 +120,15 @@ describe("The window library", function() expect.error(w.reposition, 1, 1, 1, nil):eq("bad argument #4 (expected number, got nil)") end) end) + + describe("Window.getLine", function() + it("validates arguments", function() + local w = mk() + w.getLine(1) + local _, y = w.getSize() + expect.error(w.getLine, nil):eq("bad argument #1 (expected number, got nil)") + expect.error(w.getLine, 0):eq("Line is out of range.") + expect.error(w.getLine, y + 1):eq("Line is out of range.") + end) + end) end)