diff --git a/patchwork.md b/patchwork.md index 26cb59266..10364757c 100644 --- a/patchwork.md +++ b/patchwork.md @@ -168,3 +168,12 @@ Update configuration to match latest illuaminate Ooooooh, it's all fancy now. Well, that or horrifically broken. ``` + +``` +c334423d42ba3b653ac3a8c27bce7970457f8f96 +Add function to get window visibility + +Closes #562 + +Co-authored-by: devomaa +``` diff --git a/src/main/resources/data/computercraft/lua/rom/apis/window.lua b/src/main/resources/data/computercraft/lua/rom/apis/window.lua index ec112a991..684d54f9c 100644 --- a/src/main/resources/data/computercraft/lua/rom/apis/window.lua +++ b/src/main/resources/data/computercraft/lua/rom/apis/window.lua @@ -125,7 +125,7 @@ function create(parent, nX, nY, nWidth, nHeight, bStartVisible) -- Helper functions local function updateCursorPos() if nCursorX >= 1 and nCursorY >= 1 and - nCursorX <= nWidth and nCursorY <= nHeight then + nCursorX <= nWidth and nCursorY <= nHeight then parent.setCursorPos(nX + nCursorX - 1, nY + nCursorY - 1) else parent.setCursorPos(0, 0) @@ -474,6 +474,14 @@ function create(parent, nX, nY, nWidth, nHeight, bStartVisible) end end + --- Get whether this window is visible. Invisible windows will not be + -- drawn to the screen until they are made visible again. + -- + -- @treturn boolean Whether this window is visible. + -- @see Window:setVisible + function window.isVisible() + return bVisible + end --- Draw this window. This does nothing if the window is not visible. -- -- @see Window:setVisible 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..d1861eef2 100644 --- a/src/test/resources/test-rom/spec/apis/window_spec.lua +++ b/src/test/resources/test-rom/spec/apis/window_spec.lua @@ -118,6 +118,56 @@ describe("The window library", function() expect.error(w.reposition, 1, 1, false, 1):eq("bad argument #3 (expected number, got boolean)") expect.error(w.reposition, 1, 1, nil, 1):eq("bad argument #3 (expected number, got nil)") expect.error(w.reposition, 1, 1, 1, nil):eq("bad argument #4 (expected number, got nil)") + expect.error(w.reposition, 1, 1, 1, 1, true):eq("bad argument #5 (expected table, got boolean)") + end) + + it("can change the buffer", function() + local a, b = mk(), mk() + local target = window.create(a, 1, 1, a.getSize()) + + target.write("Test") + expect((a.getLine(1))):equal("Test ") + expect({ a.getCursorPos() }):same { 5, 1 } + + target.reposition(1, 1, nil, nil, b) + + target.redraw() + expect((a.getLine(1))):equal("Test ") + expect({ a.getCursorPos() }):same { 5, 1 } + + target.setCursorPos(1, 1) target.write("More") + expect((a.getLine(1))):equal("Test ") + expect((b.getLine(1))):equal("More ") + 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) + + it("provides a line's contents", function() + local w = mk() + w.blit("test", "aaaa", "4444") + expect({ w.getLine(1) }):same { "test ", "aaaa0", "4444f" } + end) + end) + describe("Window.setVisible", function() + it("validates arguments", function() + local w = mk() + expect.error(w.setVisible, nil):eq("bad argument #1 (expected boolean, got nil)") + end) + end) + describe("Window.isVisible", function() + it("gets window visibility", function() + local w = mk() + w.setVisible(false) + expect(w.isVisible()):same(false) end) end) end)