Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local serialization = require("serialization")
- local component = require("component")
- local function load_config()
- local file = io.open("/etc/sc", "r")
- if not file then
- return {}
- end
- local config = serialization.unserialize(file:read("*all"))
- file:close()
- return config
- end
- local function save_config(config)
- local file = io.open("/etc/sc", "w")
- if not file then
- return
- end
- file:write(serialization.serialize(config, math.huge))
- file:close()
- end
- local config = load_config()
- local gpu = component.gpu
- local args = require("shell").parse(...)
- if args[1] == "scan" then
- local primary = component.getPrimary("screen").address
- if not config.primary then
- config.primary = primary
- end
- if not config.map then
- config.map = {}
- config.umap = {}
- end
- local index = config.index or 1
- for k, v in pairs(component.list("screen")) do
- if config.umap[k] == nil then
- config.map["s" .. tostring(index)] = k
- config.umap[k] = "s" .. tostring(index)
- index = index + 1
- end
- if k ~= primary then
- gpu.bind(k, false)
- local x, y = component.invoke(k, "getAspectRatio")
- gpu.setResolution(x * 10, y * 5)
- gpu.setBackground(0x000000)
- gpu.setForeground(0xffffff)
- gpu.fill(1, 1, x * 10, y * 5, " ")
- gpu.set(1, 1, "Screen " .. config.umap[k])
- gpu.set(1, 2, "Address: " .. k)
- gpu.set(1, 3, "Name: " .. config.umap[k])
- gpu.set(1, 4, "Primary: " .. (k == primary and "Yes" or "No"))
- gpu.set(1, 5, "Aspect Ratio: " .. x .. ":" .. y)
- end
- end
- gpu.bind(primary, false)
- config.index = index
- save_config(config)
- print("Done.")
- end
- if args[1] == "rename" then
- local name = args[2]
- local newName = args[3]
- if not config.map[name] then
- print("Screen not found")
- return
- end
- local screen = config.map[name]
- config.map[name] = nil
- config.map[newName] = screen
- config.umap[screen] = newName
- save_config(config)
- print("Done.")
- end
- if args[1] == "primary" then
- local name = args[2]
- if not config.map[name] then
- print("Screen not found")
- return
- end
- config.primary = config.map[name]
- save_config(config)
- print("Done.")
- end
- local function switchTo(address)
- gpu.bind(address, false)
- gpu.setResolution(gpu.maxResolution())
- require("tty").clear()
- end
- if args[1] == "switch" then
- local name = args[2]
- if not config.map[name] then
- print("Screen not found")
- return
- end
- local screen = config.map[name]
- switchTo(screen)
- end
- if args[1] == "reset" then
- local primary = config.primary
- if not primary then
- print("Primary screen not found")
- return
- end
- switchTo(primary)
- end
- if args[1] == "with" then
- local name = args[2]
- if not config.map[name] then
- print("Screen not found")
- return
- end
- local screen = config.map[name]
- switchTo(screen)
- local left = table.concat({select(3, ...)}, " ")
- os.execute(left)
- switchTo(config.primary)
- end
Advertisement
Add Comment
Please, Sign In to add comment