Lua 协程,无法恢复已经停止的协程

首先,我正在处理的代码不是我的代码,而是一段被遗弃的旧代码,我正在努力修复它。这是一个短脚本,它应该读取一个文本文件并输出一个 ASCII 图像,然后等待一段时间再显示另一个 ASCII 图像,创建一种 ASCII 电影或幻灯片的效果。我对 Lua 不太熟悉,遇到的问题是代码输出了“无法恢复已经停止的协程”。然而,它似乎总是像仍然卡在一个无限循环里一样一直说这个。非常感谢任何帮助或想法。

local component = require("component")
local term = require("term")
local filesystem = require("filesystem")
local gpu = component.gpu

local moviefile = "/usr/movies/1.txt"
local f = filesystem.open(moviefile, "rb")
local filmText = f:read(filesystem.size(moviefile))
f:close()

local function iterator()
    return coroutine.wrap( function()
        for line in string.gmatch( filmText, "([^\n]*)\n") do
            coroutine.yield(line)
        end
        return false
    end )
end

term.clear()
local it = iterator()

local bFinished = false
while not bFinished do
    -- Read the frame header
    local holdLine = it()
    if not holdLine then
        bFinished = true
        break
    end

    -- Get this every frame incase the monitor resizes
    local w,h = gpu.getResolution()
    local startX = math.floor( (w - 65) / 2 )
    local startY = math.floor( (h - 14) / 2 )

    -- Print the frame
    term.clear()
    for n=1,13 do
        local line = it()
        if line then
            term.setCursor(startX, startY + n)
            term.write( line )
        else
            bFinished = true
            break
        end
    end

    -- Hold the frame
    local hold = tonumber(holdLine) or 1
    local delay = (hold * 0.05) - 0.01
    os.sleep( delay )
end
点赞