--[[ bind_monitor.lua - 一键绑定显示器 运行后会自动备份原有 startup.lua,并生成新的 startup.lua 将终端输出重定向到找到的第一个显示器。 运行后重启生效。 ]] local function main() -- 1. 查找显示器 local mon = peripheral.find("monitor") if not mon then print("未找到显示器!请先连接显示器。") return end -- 2. 备份原有 startup.lua(如果存在) if fs.exists("startup.lua") then local backupName = "startup_backup_" .. os.time() .. ".lua" fs.copy("startup.lua", backupName) print("已备份原 startup.lua 为 " .. backupName) end -- 3. 生成新的 startup.lua local file = fs.open("startup.lua", "w") file.write([[ -- 自动生成的 startup.lua - 将终端输出重定向到显示器 local mon = peripheral.find("monitor") if mon then -- 设置缩放(可选,0.5 可显示更多内容) mon.setTextScale(0.5) -- 重定向终端到显示器 term.redirect(mon) mon.clear() mon.setCursorPos(1,1) mon.write("Terminal redirected to monitor.") mon.setCursorPos(1,2) mon.write("Type commands on the computer.") else print("No monitor found.") end -- 如果还想在电脑自身的屏幕上看到内容,可以在此添加 term.native() 输出, -- 但为简化,这里仅重定向。 ]]) file.close() print("已生成新的 startup.lua,重启后生效。") -- 4. 提示重启 print("输入 reboot 重启电脑。") end -- 安全执行 local ok, err = pcall(main) if not ok then print("错误: " .. tostring(err)) end