neuroticfox

Draconic Control Redux Tail

May 27th, 2026 (edited)
102
0
Never
8
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.13 KB | None | 0 0
  1. -- dctail.lua v4 - direct GPU log viewer for dcmaster
  2.  
  3. local component = require("component")
  4. local event = require("event")
  5. local fs = require("filesystem")
  6. local computer = require("computer")
  7.  
  8. local args = {...}
  9. local LOG_FILE = "/home/dcmaster_log.txt"
  10. local listOnly = false
  11. local forceScreen = nil
  12. local forceGpu = nil
  13.  
  14. for i = 1, #args do
  15.   local a = tostring(args[i] or "")
  16.   if a == "--list" then
  17.     listOnly = true
  18.   elseif a:sub(1, 9) == "--screen=" then
  19.     forceScreen = a:sub(10)
  20.   elseif a:sub(1, 6) == "--gpu=" then
  21.     forceGpu = a:sub(7)
  22.   elseif a:sub(1, 1) ~= "-" then
  23.     LOG_FILE = a
  24.   end
  25. end
  26.  
  27. local function startsWith(s, p)
  28.   s = tostring(s or "")
  29.   p = tostring(p or "")
  30.   return p == "" or s:sub(1, #p) == p
  31. end
  32.  
  33. local function keyCount(screenAddr)
  34.   local ok, s = pcall(component.proxy, screenAddr)
  35.   if not ok or not s or not s.getKeyboards then return 0 end
  36.   local ok2, kbs = pcall(function() return s.getKeyboards() end)
  37.   if not ok2 or not kbs then return 0 end
  38.   return #kbs
  39. end
  40.  
  41. local function gpuScreen(g)
  42.   local ok, scr = pcall(function() return g.getScreen() end)
  43.   if ok then return scr end
  44.   return nil
  45. end
  46.  
  47. local function short(a)
  48.   a = tostring(a or "nil")
  49.   if #a <= 12 then return a end
  50.   return a:sub(1, 8) .. ".." .. a:sub(-4)
  51. end
  52.  
  53. local function listHardware()
  54.   print("DCTAIL V4 LOADED")
  55.   print("")
  56.  
  57.   print("Screens:")
  58.   local anyScreen = false
  59.   for addr in component.list("screen") do
  60.     anyScreen = true
  61.     print("  " .. addr .. "  keyboards=" .. tostring(keyCount(addr)))
  62.   end
  63.   if not anyScreen then print("  none") end
  64.  
  65.   print("")
  66.   print("GPUs:")
  67.   local anyGpu = false
  68.   for addr in component.list("gpu") do
  69.     anyGpu = true
  70.     local ok, g = pcall(component.proxy, addr)
  71.     local bound = "?"
  72.     if ok and g then bound = tostring(gpuScreen(g)) end
  73.     print("  " .. addr .. "  bound=" .. bound)
  74.   end
  75.   if not anyGpu then print("  none") end
  76. end
  77.  
  78. if listOnly then
  79.   listHardware()
  80.   return
  81. end
  82.  
  83. local primaryGpu = component.isAvailable("gpu") and component.gpu or nil
  84. local primaryGpuAddr = primaryGpu and primaryGpu.address or nil
  85. local primaryScreen = primaryGpu and gpuScreen(primaryGpu) or nil
  86.  
  87. local screenAddr = nil
  88. if forceScreen then
  89.   for addr in component.list("screen") do
  90.     if startsWith(addr, forceScreen) then screenAddr = addr break end
  91.   end
  92. else
  93.   for addr in component.list("screen") do
  94.     if addr ~= primaryScreen and keyCount(addr) == 0 then
  95.       screenAddr = addr
  96.       break
  97.     end
  98.   end
  99.   if not screenAddr then
  100.     for addr in component.list("screen") do
  101.       if addr ~= primaryScreen then
  102.         screenAddr = addr
  103.         break
  104.       end
  105.     end
  106.   end
  107.   if not screenAddr then screenAddr = primaryScreen end
  108. end
  109.  
  110. if not screenAddr then
  111.   error("dctail: no screen found")
  112. end
  113.  
  114. local gpu = nil
  115. local gpuAddr = nil
  116.  
  117. if forceGpu then
  118.   for addr in component.list("gpu") do
  119.     if startsWith(addr, forceGpu) then
  120.       local ok, g = pcall(component.proxy, addr)
  121.       if ok and g then gpu = g gpuAddr = addr break end
  122.     end
  123.   end
  124. else
  125.   -- Prefer a GPU that is not the primary terminal GPU.
  126.   for addr in component.list("gpu") do
  127.     local ok, g = pcall(component.proxy, addr)
  128.     if ok and g and addr ~= primaryGpuAddr then
  129.       gpu = g
  130.       gpuAddr = addr
  131.       break
  132.     end
  133.   end
  134.  
  135.   -- Fallback to the primary GPU.
  136.   if not gpu and primaryGpu then
  137.     gpu = primaryGpu
  138.     gpuAddr = primaryGpuAddr
  139.   end
  140. end
  141.  
  142. if not gpu then
  143.   error("dctail: no GPU found")
  144. end
  145.  
  146. local gpuCount = 0
  147. for _ in component.list("gpu") do gpuCount = gpuCount + 1 end
  148.  
  149. if gpuCount < 2 and screenAddr ~= primaryScreen then
  150.   print("DCTAIL V4 LOADED")
  151.   print("Only one GPU detected.")
  152.   print("One GPU cannot run dcmaster on one monitor and dctail on another monitor at the same time.")
  153.   print("Add a second GPU, or run dctail on a second OC computer.")
  154.   print("")
  155.   listHardware()
  156.   return
  157. end
  158.  
  159. pcall(function() gpu.bind(screenAddr, true) end)
  160.  
  161. local mw, mh = gpu.maxResolution()
  162. mw = tonumber(mw) or 80
  163. mh = tonumber(mh) or 25
  164. pcall(function() gpu.setResolution(math.min(mw, 160), math.min(mh, 50)) end)
  165.  
  166. local W, H = gpu.getResolution()
  167. W = tonumber(W) or 80
  168. H = tonumber(H) or 25
  169.  
  170. local C_BG = 0x000000
  171. local C_TEXT = 0xDDDDDD
  172. local C_HEAD = 0x4FC3F7
  173. local C_MUTED = 0x777777
  174. local C_RED = 0xEF5350
  175. local C_YELLOW = 0xFFD54F
  176. local C_GREEN = 0x66BB6A
  177.  
  178. local lines = {}
  179. local lastSize = 0
  180. local dirty = true
  181.  
  182. local function color(fg, bg)
  183.   gpu.setForeground(fg or C_TEXT)
  184.   gpu.setBackground(bg or C_BG)
  185. end
  186.  
  187. local function fit(s, w)
  188.   s = tostring(s or "")
  189.   if #s > w then return s:sub(1, w) end
  190.   return s .. string.rep(" ", w - #s)
  191. end
  192.  
  193. local function push(line)
  194.   line = tostring(line or "")
  195.   while #line > W do
  196.     table.insert(lines, line:sub(1, W))
  197.     line = line:sub(W + 1)
  198.   end
  199.   table.insert(lines, line)
  200.   while #lines > H - 3 do
  201.     table.remove(lines, 1)
  202.   end
  203.   dirty = true
  204. end
  205.  
  206. local function append(chunk)
  207.   if not chunk or #chunk == 0 then return end
  208.   chunk = chunk:gsub("\r", "")
  209.   for line in (chunk .. "\n"):gmatch("([^\n]*)\n") do
  210.     push(line)
  211.   end
  212. end
  213.  
  214. local function draw(status)
  215.   if not dirty then return end
  216.   dirty = false
  217.  
  218.   color(C_TEXT, C_BG)
  219.   gpu.fill(1, 1, W, H, " ")
  220.  
  221.   color(C_BG, C_HEAD)
  222.   gpu.fill(1, 1, W, 1, " ")
  223.   gpu.set(1, 1, fit(" dctail v4  " .. LOG_FILE .. "  GPU " .. short(gpuAddr) .. "  SCREEN " .. short(screenAddr), W))
  224.  
  225.   color(C_MUTED, C_BG)
  226.   gpu.set(1, 2, fit(status or "", W))
  227.  
  228.   local y = 3
  229.   local start = math.max(1, #lines - (H - 3) + 1)
  230.  
  231.   for i = start, #lines do
  232.     local line = lines[i]
  233.     local fg = C_TEXT
  234.  
  235.     if line:find("cutoff") or line:find("SCRAM") or line:find("crashed") then
  236.       fg = C_RED
  237.     elseif line:find("warming") or line:find("charged") or line:find("ETA") then
  238.       fg = C_YELLOW
  239.     elseif line:find("running") or line:find("generation") then
  240.       fg = C_GREEN
  241.     elseif line:find("^tick=") or line:find("^===") then
  242.       fg = C_HEAD
  243.     end
  244.  
  245.     color(fg, C_BG)
  246.     gpu.set(1, y, fit(line, W))
  247.     y = y + 1
  248.     if y > H then break end
  249.   end
  250. end
  251.  
  252. push("dctail v4 initialized")
  253. push("Watching " .. LOG_FILE)
  254. push("")
  255.  
  256. while true do
  257.   local status = ""
  258.  
  259.   if not fs.exists(LOG_FILE) then
  260.     status = "Waiting for log file... uptime=" .. string.format("%.1f", computer.uptime())
  261.     dirty = true
  262.   else
  263.     local size = fs.size(LOG_FILE) or 0
  264.  
  265.     if size < lastSize then
  266.       append("")
  267.       append("--- log reset ---")
  268.       lastSize = 0
  269.     end
  270.  
  271.     if size > lastSize then
  272.       local f = io.open(LOG_FILE, "rb")
  273.       if f then
  274.         f:seek("set", lastSize)
  275.         local chunk = f:read("*a")
  276.         f:close()
  277.         append(chunk)
  278.         lastSize = size
  279.       end
  280.     end
  281.  
  282.     status = "Watching. size=" .. tostring(size) .. " bytes  Ctrl+C to stop"
  283.     dirty = true
  284.   end
  285.  
  286.   draw(status)
  287.  
  288.   local ev = {event.pull(0.25)}
  289.   if ev[1] == "interrupted" then
  290.     break
  291.   elseif ev[1] == "screen_resized" then
  292.     W, H = gpu.getResolution()
  293.     W = tonumber(W) or 80
  294.     H = tonumber(H) or 25
  295.     dirty = true
  296.   end
  297. end
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
Add Comment
Please, Sign In to add comment