Files
LeonOS/data/computercraft/lua/rom/programs/shell.lua
Leonmmcoset 8c2b73b845 refactor(shell): 使用textutils.coloredPrint优化提示符显示
将shell提示符的拼接方式改为使用textutils.coloredPrint函数,提高代码可读性和维护性
2025-08-31 19:56:01 +08:00

81 lines
1.8 KiB
Lua

-- rc.shell
local rc = require("rc")
local fs = require("fs")
local term = require("term")
local shell = require("shell")
local colors = require("colors")
local thread = require("rc.thread")
local textutils = require("textutils")
if os.version then
textutils.coloredPrint(colors.yellow, os.version(), colors.white)
else
textutils.coloredPrint(colors.yellow, rc.version(), colors.white)
end
thread.vars().parentShell = thread.id()
shell.init(_ENV)
if not shell.__has_run_startup then
shell.__has_run_startup = true
if fs.exists("/startup.lua") then
local ok, err = pcall(dofile, "/startup.lua")
if not ok and err then
io.stderr:write(err, "\n")
end
end
if fs.exists("/startup") and fs.isDir("/startup") then
local files = fs.list("/startup/")
table.sort(files)
for f=1, #files, 1 do
local ok, err = pcall(dofile, "/startup/"..files[f])
if not ok and err then
io.stderr:write(err, "\n")
end
end
end
end
local aliases = {
background = "bg",
clr = "clear",
cp = "copy",
dir = "list",
foreground = "fg",
mv = "move",
rm = "delete",
rs = "redstone",
sh = "shell",
ps = "threads",
restart = "reboot"
}
for k, v in pairs(aliases) do
shell.setAlias(k, v)
end
local completions = "/rc/completions"
for _, prog in ipairs(fs.list(completions)) do
dofile(fs.combine(completions, prog))
end
local history = {}
while true do
term.setTextColor(colors.yellow)
term.setBackgroundColor(colors.black)
rc.write(textutils.coloredPrint(colors.yellow, "$"..shell.dir(), colors.green, " >>> "))
term.setTextColor(colors.white)
local text = term.read(nil, history, shell.complete)
if #text > 0 then
history[#history+1] = text
local ok, err = shell.run(text)
if not ok and err then
io.stderr:write(err, "\n")
end
end
end