awefs/files/startup.lua
2025-02-02 05:30:04 -05:00

155 lines
3.7 KiB
Lua

local bsh = {}
local component = require("component")
local computer = require("computer")
local event = {}
function event.pull(name, timeout)
local rawSignal = {}
local signals_recieved_in_mean_time = {}
local timeouts = 0
if not timeout then
timeout = 99999999999
end
while rawSignal[1] ~= name do
-- pull any signal
rawSignal = table.pack(computer.pullSignal(0.1))
-- if no signal then add to time timeout
if rawSignal[1] == nil then
timeouts = timeouts + 0.1
else
if rawSignal[1] ~= name then
-- not the signal, push to the signals we got in the mean time
table.insert(signals_recieved_in_mean_time,rawSignal)
else
-- got the signal
break
end
end
if timeouts >= timeout then
-- we never got the signal :(
return nil
end
end
-- rethrow all signals... they weren't what we wanted
if #signals_recieved_in_mean_time > 0 then
for i in ipairs(signals_recieved_in_mean_time) do
computer.pushSignal(table.unpack(signals_recieved_in_mean_time[i]))
end
end
return table.unpack(rawSignal)
end
-- BUG: when in /bin directory, running something tries to run /binhello.lua
-- FEAT: autofill slashes, and .lua
-- FEAT: handle ..
bsh.running = true
local function keyin()
while true do
local _,_,letterAsciiCode,_,_ = event.pull("key_down")
return letterAsciiCode
end
end
function Align(String,Number,append)
if not append then
append = " "
end
if #String > Number then
while #String > Number do
String = String:sub(1, -2)
end
String = String:sub(1, -5) .. "..."
end
if #String < Number then
while #String < Number do
String = String .. append
end
end
return String
end
function bsh.command(buffer)
local command = Split(buffer)
if command[1] == "ls" then
for i = 1, #Filetable do
print(
string.format("%s off %sb size%s(*512)\r\n",
Align(Filetable[i]["name"],16),
Align(tostring(Filetable[i]["offset"]),8),
Align(tostring(Filetable[i]["size"]),2)))
end
elseif command[1] == "clear" then
clearscr()
screen_x = 2
screen_y = 1
elseif command[1] == "exit" then
bsh.running = false
else
local file, err = loadfile(command[1])
if file == nil then
print("error while loading" .. tostring(err))
return
end
local success, result = pcall(file, command)
if not success then
print(result.."\r\n")
end
end
end
local function prompt()
print(cwd .." #> ")
end
if _G.cwd == nil then
_G.cwd = "/"
end
print("bsh v2.1\r\n")
local buffer = ""
prompt()
while bsh.running do
local nextkeycode = keyin()
if nextkeycode == nil then
nextkeycode = 0
end
local nextkey = string.char(nextkeycode)
if nextkeycode == 13 then
print("\r\n")
local success, reason = pcall(bsh.command, buffer)
if not success then
print(reason)
end
buffer = ""
nextkeycode = nil
print("\r\n")
if _G.PROCESS_RUNNER then
coroutine.yield()
end
prompt()
elseif nextkeycode == 8 then
if #buffer ~= 0 then
buffer = buffer:sub(1, -2)
screen_x = screen_x - 1
print(" ")
screen_x = screen_x - 1
end
elseif nextkeycode == 16 then
-- ignore.
else
buffer = buffer .. nextkey
print(nextkey)
end
end