From 686c6a4c44838277da4df0fdb50b06bc224c6e4e Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Sun, 1 Aug 2021 08:43:37 +0100 Subject: [PATCH] Use cc.strings.wrap inside slowWrite Means we don't end up re-wrapping text and producing incorrect results. Fixes #865 --- .../computercraft/lua/rom/apis/textutils.lua | 29 +++++++++---------- .../lua/rom/modules/main/cc/strings.lua | 2 +- .../test-rom/spec/apis/textutils_spec.lua | 15 ++++++++++ 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/main/resources/data/computercraft/lua/rom/apis/textutils.lua b/src/main/resources/data/computercraft/lua/rom/apis/textutils.lua index 4e340f4be..855e893c5 100644 --- a/src/main/resources/data/computercraft/lua/rom/apis/textutils.lua +++ b/src/main/resources/data/computercraft/lua/rom/apis/textutils.lua @@ -5,35 +5,32 @@ local expect = dofile("rom/modules/main/cc/expect.lua") local expect, field = expect.expect, expect.field +local wrap = dofile("rom/modules/main/cc/strings.lua").wrap --- Slowly writes string text at current cursor position, -- character-by-character. -- -- Like @{_G.write}, this does not insert a newline at the end. -- --- @tparam string sText The the text to write to the screen --- @tparam[opt] number nRate The number of characters to write each second, +-- @tparam string text The the text to write to the screen +-- @tparam[opt] number rate The number of characters to write each second, -- Defaults to 20. -- @usage textutils.slowWrite("Hello, world!") -- @usage textutils.slowWrite("Hello, world!", 5) -function slowWrite(sText, nRate) - expect(2, nRate, "number", "nil") - nRate = nRate or 20 - if nRate < 0 then +function slowWrite(text, rate) + expect(2, rate, "number", "nil") + rate = rate or 20 + if rate < 0 then error("Rate must be positive", 2) end - local nSleep = 1 / nRate + local to_sleep = 1 / rate - sText = tostring(sText) - local x, y = term.getCursorPos() - local len = #sText + local wrapped_lines = wrap(tostring(text), (term.getSize())) + local wrapped_str = table.concat(wrapped_lines, "\n") - for n = 1, len do - term.setCursorPos(x, y) - sleep(nSleep) - local nLines = write(string.sub(sText, 1, n)) - local _, newY = term.getCursorPos() - y = newY - nLines + for n = 1, #wrapped_str do + sleep(to_sleep) + write(wrapped_str:sub(n, n)) end end diff --git a/src/main/resources/data/computercraft/lua/rom/modules/main/cc/strings.lua b/src/main/resources/data/computercraft/lua/rom/modules/main/cc/strings.lua index 89d6e475c..fa149733d 100644 --- a/src/main/resources/data/computercraft/lua/rom/modules/main/cc/strings.lua +++ b/src/main/resources/data/computercraft/lua/rom/modules/main/cc/strings.lua @@ -3,7 +3,7 @@ -- @module cc.strings -- @see textutils For additional string related utilities. -local expect = require "cc.expect".expect +local expect = (require and require("cc.expect") or dofile("rom/modules/main/cc/expect.lua")).expect --[[- Wraps a block of text, so that each line fits within the given width. diff --git a/src/test/resources/test-rom/spec/apis/textutils_spec.lua b/src/test/resources/test-rom/spec/apis/textutils_spec.lua index 1ce8ddc13..b8137414a 100644 --- a/src/test/resources/test-rom/spec/apis/textutils_spec.lua +++ b/src/test/resources/test-rom/spec/apis/textutils_spec.lua @@ -1,8 +1,23 @@ +local helpers = require "test_helpers" + describe("The textutils library", function() describe("textutils.slowWrite", function() it("validates arguments", function() expect.error(textutils.slowWrite, nil, false):eq("bad argument #2 (expected number, got boolean)") end) + + it("wraps text correctly", function() + local count = 0 + stub(_G, "sleep", function() count = count + 1 end) + local w = helpers.with_window(20, 3, function() + textutils.slowWrite("This is a long string which one would hope wraps.") + end) + + expect(w.getLine(1)):eq "This is a long " + expect(w.getLine(2)):eq "string which one " + expect(w.getLine(3)):eq "would hope wraps. " + expect(count):eq(51) + end) end) describe("textutils.formatTime", function()