Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- dctail.lua v4 - direct GPU log viewer for dcmaster
- local component = require("component")
- local event = require("event")
- local fs = require("filesystem")
- local computer = require("computer")
- local args = {...}
- local LOG_FILE = "/home/dcmaster_log.txt"
- local listOnly = false
- local forceScreen = nil
- local forceGpu = nil
- for i = 1, #args do
- local a = tostring(args[i] or "")
- if a == "--list" then
- listOnly = true
- elseif a:sub(1, 9) == "--screen=" then
- forceScreen = a:sub(10)
- elseif a:sub(1, 6) == "--gpu=" then
- forceGpu = a:sub(7)
- elseif a:sub(1, 1) ~= "-" then
- LOG_FILE = a
- end
- end
- local function startsWith(s, p)
- s = tostring(s or "")
- p = tostring(p or "")
- return p == "" or s:sub(1, #p) == p
- end
- local function keyCount(screenAddr)
- local ok, s = pcall(component.proxy, screenAddr)
- if not ok or not s or not s.getKeyboards then return 0 end
- local ok2, kbs = pcall(function() return s.getKeyboards() end)
- if not ok2 or not kbs then return 0 end
- return #kbs
- end
- local function gpuScreen(g)
- local ok, scr = pcall(function() return g.getScreen() end)
- if ok then return scr end
- return nil
- end
- local function short(a)
- a = tostring(a or "nil")
- if #a <= 12 then return a end
- return a:sub(1, 8) .. ".." .. a:sub(-4)
- end
- local function listHardware()
- print("DCTAIL V4 LOADED")
- print("")
- print("Screens:")
- local anyScreen = false
- for addr in component.list("screen") do
- anyScreen = true
- print(" " .. addr .. " keyboards=" .. tostring(keyCount(addr)))
- end
- if not anyScreen then print(" none") end
- print("")
- print("GPUs:")
- local anyGpu = false
- for addr in component.list("gpu") do
- anyGpu = true
- local ok, g = pcall(component.proxy, addr)
- local bound = "?"
- if ok and g then bound = tostring(gpuScreen(g)) end
- print(" " .. addr .. " bound=" .. bound)
- end
- if not anyGpu then print(" none") end
- end
- if listOnly then
- listHardware()
- return
- end
- local primaryGpu = component.isAvailable("gpu") and component.gpu or nil
- local primaryGpuAddr = primaryGpu and primaryGpu.address or nil
- local primaryScreen = primaryGpu and gpuScreen(primaryGpu) or nil
- local screenAddr = nil
- if forceScreen then
- for addr in component.list("screen") do
- if startsWith(addr, forceScreen) then screenAddr = addr break end
- end
- else
- for addr in component.list("screen") do
- if addr ~= primaryScreen and keyCount(addr) == 0 then
- screenAddr = addr
- break
- end
- end
- if not screenAddr then
- for addr in component.list("screen") do
- if addr ~= primaryScreen then
- screenAddr = addr
- break
- end
- end
- end
- if not screenAddr then screenAddr = primaryScreen end
- end
- if not screenAddr then
- error("dctail: no screen found")
- end
- local gpu = nil
- local gpuAddr = nil
- if forceGpu then
- for addr in component.list("gpu") do
- if startsWith(addr, forceGpu) then
- local ok, g = pcall(component.proxy, addr)
- if ok and g then gpu = g gpuAddr = addr break end
- end
- end
- else
- -- Prefer a GPU that is not the primary terminal GPU.
- for addr in component.list("gpu") do
- local ok, g = pcall(component.proxy, addr)
- if ok and g and addr ~= primaryGpuAddr then
- gpu = g
- gpuAddr = addr
- break
- end
- end
- -- Fallback to the primary GPU.
- if not gpu and primaryGpu then
- gpu = primaryGpu
- gpuAddr = primaryGpuAddr
- end
- end
- if not gpu then
- error("dctail: no GPU found")
- end
- local gpuCount = 0
- for _ in component.list("gpu") do gpuCount = gpuCount + 1 end
- if gpuCount < 2 and screenAddr ~= primaryScreen then
- print("DCTAIL V4 LOADED")
- print("Only one GPU detected.")
- print("One GPU cannot run dcmaster on one monitor and dctail on another monitor at the same time.")
- print("Add a second GPU, or run dctail on a second OC computer.")
- print("")
- listHardware()
- return
- end
- pcall(function() gpu.bind(screenAddr, true) end)
- local mw, mh = gpu.maxResolution()
- mw = tonumber(mw) or 80
- mh = tonumber(mh) or 25
- pcall(function() gpu.setResolution(math.min(mw, 160), math.min(mh, 50)) end)
- local W, H = gpu.getResolution()
- W = tonumber(W) or 80
- H = tonumber(H) or 25
- local C_BG = 0x000000
- local C_TEXT = 0xDDDDDD
- local C_HEAD = 0x4FC3F7
- local C_MUTED = 0x777777
- local C_RED = 0xEF5350
- local C_YELLOW = 0xFFD54F
- local C_GREEN = 0x66BB6A
- local lines = {}
- local lastSize = 0
- local dirty = true
- local function color(fg, bg)
- gpu.setForeground(fg or C_TEXT)
- gpu.setBackground(bg or C_BG)
- end
- local function fit(s, w)
- s = tostring(s or "")
- if #s > w then return s:sub(1, w) end
- return s .. string.rep(" ", w - #s)
- end
- local function push(line)
- line = tostring(line or "")
- while #line > W do
- table.insert(lines, line:sub(1, W))
- line = line:sub(W + 1)
- end
- table.insert(lines, line)
- while #lines > H - 3 do
- table.remove(lines, 1)
- end
- dirty = true
- end
- local function append(chunk)
- if not chunk or #chunk == 0 then return end
- chunk = chunk:gsub("\r", "")
- for line in (chunk .. "\n"):gmatch("([^\n]*)\n") do
- push(line)
- end
- end
- local function draw(status)
- if not dirty then return end
- dirty = false
- color(C_TEXT, C_BG)
- gpu.fill(1, 1, W, H, " ")
- color(C_BG, C_HEAD)
- gpu.fill(1, 1, W, 1, " ")
- gpu.set(1, 1, fit(" dctail v4 " .. LOG_FILE .. " GPU " .. short(gpuAddr) .. " SCREEN " .. short(screenAddr), W))
- color(C_MUTED, C_BG)
- gpu.set(1, 2, fit(status or "", W))
- local y = 3
- local start = math.max(1, #lines - (H - 3) + 1)
- for i = start, #lines do
- local line = lines[i]
- local fg = C_TEXT
- if line:find("cutoff") or line:find("SCRAM") or line:find("crashed") then
- fg = C_RED
- elseif line:find("warming") or line:find("charged") or line:find("ETA") then
- fg = C_YELLOW
- elseif line:find("running") or line:find("generation") then
- fg = C_GREEN
- elseif line:find("^tick=") or line:find("^===") then
- fg = C_HEAD
- end
- color(fg, C_BG)
- gpu.set(1, y, fit(line, W))
- y = y + 1
- if y > H then break end
- end
- end
- push("dctail v4 initialized")
- push("Watching " .. LOG_FILE)
- push("")
- while true do
- local status = ""
- if not fs.exists(LOG_FILE) then
- status = "Waiting for log file... uptime=" .. string.format("%.1f", computer.uptime())
- dirty = true
- else
- local size = fs.size(LOG_FILE) or 0
- if size < lastSize then
- append("")
- append("--- log reset ---")
- lastSize = 0
- end
- if size > lastSize then
- local f = io.open(LOG_FILE, "rb")
- if f then
- f:seek("set", lastSize)
- local chunk = f:read("*a")
- f:close()
- append(chunk)
- lastSize = size
- end
- end
- status = "Watching. size=" .. tostring(size) .. " bytes Ctrl+C to stop"
- dirty = true
- end
- draw(status)
- local ev = {event.pull(0.25)}
- if ev[1] == "interrupted" then
- break
- elseif ev[1] == "screen_resized" then
- W, H = gpu.getResolution()
- W = tonumber(W) or 80
- H = tonumber(H) or 25
- dirty = true
- end
- end
Advertisement