monitor manager

This commit is contained in:
kepler155c
2017-09-24 00:35:15 -04:00
parent 75952c1580
commit 09c1285132

View File

@@ -9,12 +9,12 @@ local function syntax()
error() error()
end end
local args = { ... } local args = { ... }
local UID = 0 local UID = 0
local processes = { } local processes = { }
local parentTerm = term.current() local parentTerm = term.current()
local configFile = args[1] or syntax() local configFile = args[1] or syntax()
local monitor = peripheral.find(args[2] or 'monitor') or syntax() local monitor = peripheral.find(args[2] or 'monitor') or syntax()
local defaultEnv = Util.shallowCopy(getfenv(1)) local defaultEnv = Util.shallowCopy(getfenv(1))
monitor.setTextScale(.5) monitor.setTextScale(.5)
@@ -24,6 +24,11 @@ local monDim, termDim = { }, { }
monDim.width, monDim.height = monitor.getSize() monDim.width, monDim.height = monitor.getSize()
termDim.width, termDim.height = parentTerm.getSize() termDim.width, termDim.height = parentTerm.getSize()
local function nextUID()
UID = UID + 1
return UID
end
local function saveConfig() local function saveConfig()
local t = { } local t = { }
for _,process in pairs(processes) do for _,process in pairs(processes) do
@@ -56,11 +61,12 @@ end
local function focusProcess(process) local function focusProcess(process)
if #processes > 0 then if #processes > 0 then
processes[#processes]:focus(false) local lastFocused = processes[#processes]
lastFocused:focus(false)
end end
for k,v in pairs(processes) do for k,v in pairs(processes) do
if v == self then if v == process then
table.remove(processes, k) table.remove(processes, k)
break break
end end
@@ -70,68 +76,37 @@ local function focusProcess(process)
process:focus(true) process:focus(true)
end end
local function getProcessAt(x, y)
for k = #processes, 1, -1 do
local process = processes[k]
if x >= process.x and
y >= process.y and
x <= process.x + process.width - 1 and
y <= process.y + process.height - 1 then
return k, process
end
end
end
--[[ A runnable process ]]--
local Process = { } local Process = { }
function Process:focus(focused)
if focused then
self.titleBar.setBackgroundColor(colors.green)
else
self.titleBar.setBackgroundColor(colors.gray)
end
self.titleBar.clear()
self.titleBar.setTextColor(colors.black)
write(self.titleBar, 2, 1, self.title)
write(self.titleBar, self.width - 3, 1, '*')
if focused then
self.window.restoreCursor()
end
end
function Process:drawSizers()
self.container.setBackgroundColor(colors.black)
self.container.setTextColor(colors.white)
if self.showSizers then
write(self.container, 1, 1, '\135')
write(self.container, self.width, 1, '\139')
write(self.container, 1, self.height, '\141')
write(self.container, self.width, self.height, '\142')
self.container.setTextColor(colors.yellow)
write(self.container, 1, 3, '+')
write(self.container, 1, 5, '-')
write(self.container, 3, 1, '+')
write(self.container, 5, 1, '-')
local str = string.format('%d x %d', self.width - 2, self.height - 3)
write(self.container, (self.width - #str) / 2, 1, str)
else
write(self.container, 1, 1, string.rep(' ', self.width))
write(self.container, self.width, 1, ' ')
write(self.container, 1, self.height, ' ')
write(self.container, self.width, self.height, ' ')
write(self.container, 1, 3, ' ')
write(self.container, 1, 5, ' ')
end
end
function Process:new(args) function Process:new(args)
args.env = args.env or Util.shallowCopy(defaultEnv) args.env = args.env or Util.shallowCopy(defaultEnv)
args.width = args.width or termDim.width args.width = args.width or termDim.width
args.height = args.height or termDim.height args.height = args.height or termDim.height
UID = UID + 1 local self = setmetatable({
self.uid = UID uid = nextUID(),
x = args.x or 1,
self.x = args.x or 1 y = args.y or 1,
self.y = args.y or 1 width = args.width + 2,
self.width = args.width + 2 height = args.height + 3,
self.height = args.height + 3 path = args.path,
self.path = args.path args = args.args or { },
self.args = args.args or { } title = args.title or 'shell',
self.title = args.title or 'shell' }, { __index = Process })
self:adjustDimensions() self:adjustDimensions()
@@ -163,15 +138,71 @@ function Process:new(args)
break break
end end
end end
--saveConfig() saveConfig()
redraw() redraw()
end) end)
if #processes > 0 then
processes[#processes]:focus(false)
end
table.insert(processes, self)
self:focus(true)
local previousTerm = term.current() local previousTerm = term.current()
self:resume() self:resume()
term.redirect(previousTerm) term.redirect(previousTerm)
return tab return self
end
function Process:focus(focused)
if focused then
self.titleBar.setBackgroundColor(colors.green)
else
self.titleBar.setBackgroundColor(colors.gray)
end
self.titleBar.clear()
self.titleBar.setTextColor(colors.black)
write(self.titleBar, 2, 1, self.title)
write(self.titleBar, self.width - 3, 1, '*')
if focused then
self.window.restoreCursor()
elseif self.showSizers then
self:drawSizers(false)
end
end
function Process:drawSizers(showSizers)
self.showSizers = showSizers
self.container.setBackgroundColor(colors.black)
self.container.setTextColor(colors.white)
if self.showSizers then
write(self.container, 1, 1, '\135')
write(self.container, self.width, 1, '\139')
write(self.container, 1, self.height, '\141')
write(self.container, self.width, self.height, '\142')
self.container.setTextColor(colors.yellow)
write(self.container, 1, 3, '+')
write(self.container, 1, 5, '-')
write(self.container, 3, 1, '+')
write(self.container, 5, 1, '-')
local str = string.format('%d x %d', self.width - 2, self.height - 3)
write(self.container, (self.width - #str) / 2, 1, str)
else
write(self.container, 1, 1, string.rep(' ', self.width))
write(self.container, self.width, 1, ' ')
write(self.container, 1, self.height, ' ')
write(self.container, self.width, self.height, ' ')
write(self.container, 1, 3, ' ')
write(self.container, 1, 5, ' ')
end
end end
function Process:adjustDimensions() function Process:adjustDimensions()
@@ -235,18 +266,7 @@ function Process:resume(event, ...)
end end
end end
function getProcessAt(x, y) --[[ Install a multishell manager for the monitor ]]--
for k = #processes, 1, -1 do
local process = processes[k]
if x >= process.x and
y >= process.y and
x <= process.x + process.width - 1 and
y <= process.y + process.height - 1 then
return k, process
end
end
end
defaultEnv.multishell = { } defaultEnv.multishell = { }
function defaultEnv.multishell.getFocus() function defaultEnv.multishell.getFocus()
@@ -281,7 +301,9 @@ function defaultEnv.multishell.setTitle(uid, title)
end end
function defaultEnv.multishell.getCurrent() function defaultEnv.multishell.getCurrent()
return processes[#processes].uid if #processes > 0 then
return processes[#processes].uid
end
end end
function defaultEnv.multishell.getCount() function defaultEnv.multishell.getCount()
@@ -298,43 +320,22 @@ function defaultEnv.multishell.launch(env, file, ...)
end end
function defaultEnv.multishell.openTab(tabInfo) function defaultEnv.multishell.openTab(tabInfo)
local process = setmetatable({ }, { __index = Process }) local process = Process:new(tabInfo)
table.insert(processes, process)
process:new(tabInfo)
focusProcess(process)
saveConfig() saveConfig()
return process.uid return process.uid
end end
if fs.exists(configFile) then
local config = Util.readTable(configFile)
if config then
for _,v in pairs(config) do
local process = setmetatable({ }, { __index = Process })
table.insert(processes, process)
process:new(v)
process:focus(false)
end
end
end
local function addShell() local function addShell()
UID = UID + 1
local process = setmetatable({ local process = setmetatable({
x = monDim.width - 8, x = monDim.width - 8,
y = monDim.height, y = monDim.height,
width = 9, width = 9,
height = 1, height = 1,
isShell = true, isShell = true,
uid = UID, uid = nextUID(),
}, { __index = Process }) }, { __index = Process })
table.insert(processes, 1, process)
function process:focus(focused) function process:focus(focused)
self.window.setVisible(focused) self.window.setVisible(focused)
if focused then if focused then
@@ -366,7 +367,9 @@ local function addShell()
end end
end) end)
process:focus(false) table.insert(processes, process)
process:focus(true)
local previousTerm = term.current() local previousTerm = term.current()
process:resume() process:resume()
term.redirect(previousTerm) term.redirect(previousTerm)
@@ -374,7 +377,14 @@ end
addShell() addShell()
processes[#processes]:focus(true) if fs.exists(configFile) then
local config = Util.readTable(configFile)
if config then
for _,v in pairs(config) do
Process:new(v)
end
end
end
while true do while true do
@@ -400,8 +410,7 @@ while true do
if x == process.width - 2 then if x == process.width - 2 then
process:resume('terminate') process:resume('terminate')
else else
process.showSizers = not process.showSizers process:drawSizers(not process.showSizers)
process:drawSizers()
end end
elseif x == 1 or y == 1 then -- sizers elseif x == 1 or y == 1 then -- sizers
@@ -409,8 +418,7 @@ while true do
elseif x > 1 and x < process.width then elseif x > 1 and x < process.width then
if process.showSizers then if process.showSizers then
process.showSizers = false process:drawSizers(false)
process:drawSizers()
end end
process:resume('mouse_click', 1, x - 1, y - 2) process:resume('mouse_click', 1, x - 1, y - 2)
process:resume('mouse_up', 1, x - 1, y - 2) process:resume('mouse_up', 1, x - 1, y - 2)
@@ -421,7 +429,7 @@ while true do
process.x = math.floor(x - (process.width) / 2) process.x = math.floor(x - (process.width) / 2)
process.y = y process.y = y
process:reposition() process:reposition()
process:drawSizers() process:drawSizers(true)
saveConfig() saveConfig()
end end
end end
@@ -445,3 +453,6 @@ while true do
end end
end end
end end
parentTerm.clear()
parentTerm.setCursorPos(1, 1)