neuroticfox

DCTail

May 27th, 2026 (edited)
89
0
Never
7
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.13 KB | None | 0 0
  1. --[[
  2.   dctail.lua — self-backgrounding direct GPU log viewer for dcmaster
  3.  
  4.   Normal use:
  5.     dctail.lua
  6.     dcmaster
  7.  
  8.   Debug / foreground:
  9.     dctail.lua --fg
  10.     dctail.lua --foreground
  11.  
  12.   Hardware list:
  13.     dctail.lua --list
  14.  
  15.   Optional:
  16.     dctail.lua /home/dcmaster_log.txt
  17.     dctail.lua --screen=<screen address prefix>
  18.     dctail.lua --gpu=<gpu address prefix>
  19.  
  20.   Notes:
  21.     - Do NOT use: dctail.lua &
  22.     - OpenOS does not background jobs that way.
  23.     - This script backgrounds itself using the OpenOS thread library.
  24.     - For true two-screen use, you still need two GPUs.
  25. ]]
  26.  
  27. local component = require("component")
  28. local event     = require("event")
  29. local fs        = require("filesystem")
  30. local computer  = require("computer")
  31.  
  32. local unpack = table.unpack or unpack
  33.  
  34. --------------------------------------------------------------------------------
  35. -- BASIC ARG PARSE
  36. --------------------------------------------------------------------------------
  37.  
  38. local function parseArgs(rawArgs)
  39.   local opts = {
  40.     logFile = "/home/dcmaster_log.txt",
  41.     listOnly = false,
  42.     foreground = false,
  43.     forceScreen = nil,
  44.     forceGpu = nil,
  45.   }
  46.  
  47.   for i = 1, #rawArgs do
  48.     local a = tostring(rawArgs[i] or "")
  49.  
  50.     if a == "--list" then
  51.       opts.listOnly = true
  52.     elseif a == "--fg" or a == "--foreground" then
  53.       opts.foreground = true
  54.     elseif a:sub(1, 9) == "--screen=" then
  55.       opts.forceScreen = a:sub(10)
  56.     elseif a:sub(1, 6) == "--gpu=" then
  57.       opts.forceGpu = a:sub(7)
  58.     elseif a:sub(1, 1) ~= "-" then
  59.       opts.logFile = a
  60.     end
  61.   end
  62.  
  63.   return opts
  64. end
  65.  
  66. local function startsWith(s, p)
  67.   s = tostring(s or "")
  68.   p = tostring(p or "")
  69.   return p == "" or s:sub(1, #p) == p
  70. end
  71.  
  72. local function short(a)
  73.   a = tostring(a or "nil")
  74.   if #a <= 12 then return a end
  75.   return a:sub(1, 8) .. ".." .. a:sub(-4)
  76. end
  77.  
  78. local function keyCount(screenAddr)
  79.   local ok, s = pcall(component.proxy, screenAddr)
  80.   if not ok or not s or not s.getKeyboards then return 0 end
  81.  
  82.   local ok2, kbs = pcall(function()
  83.     return s.getKeyboards()
  84.   end)
  85.  
  86.   if not ok2 or not kbs then return 0 end
  87.   return #kbs
  88. end
  89.  
  90. local function gpuScreen(g)
  91.   local ok, scr = pcall(function()
  92.     return g.getScreen()
  93.   end)
  94.  
  95.   if ok then return scr end
  96.   return nil
  97. end
  98.  
  99. local function listHardware()
  100.   print("DCTAIL SELF-BG LOADED")
  101.   print("")
  102.  
  103.   print("Screens:")
  104.   local anyScreen = false
  105.   for addr in component.list("screen") do
  106.     anyScreen = true
  107.     print("  " .. addr .. "  keyboards=" .. tostring(keyCount(addr)))
  108.   end
  109.   if not anyScreen then print("  none") end
  110.  
  111.   print("")
  112.   print("GPUs:")
  113.   local anyGpu = false
  114.   for addr in component.list("gpu") do
  115.     anyGpu = true
  116.     local ok, g = pcall(component.proxy, addr)
  117.     local bound = "?"
  118.     if ok and g then bound = tostring(gpuScreen(g)) end
  119.     print("  " .. addr .. "  bound=" .. bound)
  120.   end
  121.   if not anyGpu then print("  none") end
  122. end
  123.  
  124. --------------------------------------------------------------------------------
  125. -- REAL TAIL VIEWER
  126. --------------------------------------------------------------------------------
  127.  
  128. local function runTail(rawArgs)
  129.   local opts = parseArgs(rawArgs)
  130.  
  131.   local LOG_FILE = opts.logFile
  132.  
  133.   local primaryGpu = component.isAvailable("gpu") and component.gpu or nil
  134.   local primaryGpuAddr = primaryGpu and primaryGpu.address or nil
  135.   local primaryScreen = primaryGpu and gpuScreen(primaryGpu) or nil
  136.  
  137.   local screenAddr = nil
  138.  
  139.   if opts.forceScreen then
  140.     for addr in component.list("screen") do
  141.       if startsWith(addr, opts.forceScreen) then
  142.         screenAddr = addr
  143.         break
  144.       end
  145.     end
  146.   else
  147.     -- Prefer a non-primary screen with no keyboard.
  148.     for addr in component.list("screen") do
  149.       if addr ~= primaryScreen and keyCount(addr) == 0 then
  150.         screenAddr = addr
  151.         break
  152.       end
  153.     end
  154.  
  155.     -- Fallback: any non-primary screen.
  156.     if not screenAddr then
  157.       for addr in component.list("screen") do
  158.         if addr ~= primaryScreen then
  159.           screenAddr = addr
  160.           break
  161.         end
  162.       end
  163.     end
  164.  
  165.     -- Final fallback: current primary screen.
  166.     if not screenAddr then
  167.       screenAddr = primaryScreen
  168.     end
  169.   end
  170.  
  171.   if not screenAddr then
  172.     error("dctail: no screen found")
  173.   end
  174.  
  175.   local gpu = nil
  176.   local gpuAddr = nil
  177.  
  178.   if opts.forceGpu then
  179.     for addr in component.list("gpu") do
  180.       if startsWith(addr, opts.forceGpu) then
  181.         local ok, g = pcall(component.proxy, addr)
  182.         if ok and g then
  183.           gpu = g
  184.           gpuAddr = addr
  185.           break
  186.         end
  187.       end
  188.     end
  189.   else
  190.     -- Prefer a GPU already bound to the target screen.
  191.     for addr in component.list("gpu") do
  192.       local ok, g = pcall(component.proxy, addr)
  193.       if ok and g and gpuScreen(g) == screenAddr then
  194.         gpu = g
  195.         gpuAddr = addr
  196.         break
  197.       end
  198.     end
  199.  
  200.     -- Then prefer a GPU that is not the primary terminal GPU.
  201.     if not gpu then
  202.       for addr in component.list("gpu") do
  203.         local ok, g = pcall(component.proxy, addr)
  204.         if ok and g and addr ~= primaryGpuAddr then
  205.           gpu = g
  206.           gpuAddr = addr
  207.           break
  208.         end
  209.       end
  210.     end
  211.  
  212.     -- Fallback to primary GPU.
  213.     if not gpu and primaryGpu then
  214.       gpu = primaryGpu
  215.       gpuAddr = primaryGpuAddr
  216.     end
  217.   end
  218.  
  219.   if not gpu then
  220.     error("dctail: no GPU found")
  221.   end
  222.  
  223.   local gpuCount = 0
  224.   for _ in component.list("gpu") do gpuCount = gpuCount + 1 end
  225.  
  226.   if gpuCount < 2 and screenAddr ~= primaryScreen then
  227.     print("DCTAIL SELF-BG LOADED")
  228.     print("Only one GPU detected.")
  229.     print("One GPU cannot run dcmaster on one monitor and dctail on another monitor at the same time.")
  230.     print("Add a second GPU, or run dctail on a second OC computer.")
  231.     print("")
  232.     listHardware()
  233.     return
  234.   end
  235.  
  236.   pcall(function()
  237.     gpu.bind(screenAddr, true)
  238.   end)
  239.  
  240.   local mw, mh = gpu.maxResolution()
  241.   mw = tonumber(mw) or 80
  242.   mh = tonumber(mh) or 25
  243.  
  244.   pcall(function()
  245.     gpu.setResolution(math.min(mw, 160), math.min(mh, 50))
  246.   end)
  247.  
  248.   local W, H = gpu.getResolution()
  249.   W = tonumber(W) or 80
  250.   H = tonumber(H) or 25
  251.  
  252.   local targetKeyboards = {}
  253.   pcall(function()
  254.     local scr = component.proxy(screenAddr)
  255.     if scr and scr.getKeyboards then
  256.       local kbs = scr.getKeyboards()
  257.       if kbs then
  258.         for _, kb in ipairs(kbs) do
  259.           targetKeyboards[kb] = true
  260.         end
  261.       end
  262.     end
  263.   end)
  264.  
  265.   local function isTailKeyboard(addr)
  266.     if not next(targetKeyboards) then return true end
  267.     return targetKeyboards[addr] == true
  268.   end
  269.  
  270.   local C_BG     = 0x000000
  271.   local C_TEXT   = 0xDDDDDD
  272.   local C_HEAD   = 0x4FC3F7
  273.   local C_MUTED  = 0x777777
  274.   local C_RED    = 0xEF5350
  275.   local C_YELLOW = 0xFFD54F
  276.   local C_GREEN  = 0x66BB6A
  277.  
  278.   local lines = {}
  279.   local lastSize = 0
  280.   local dirty = true
  281.   local maxLines = 3000
  282.   local scrollOffset = 0
  283.   local follow = true
  284.   local lastStatus = ""
  285.  
  286.   local function color(fg, bg)
  287.     gpu.setForeground(fg or C_TEXT)
  288.     gpu.setBackground(bg or C_BG)
  289.   end
  290.  
  291.   local function fit(s, w)
  292.     s = tostring(s or "")
  293.     w = tonumber(w) or 0
  294.  
  295.     if w <= 0 then return "" end
  296.  
  297.     if #s > w then
  298.       return s:sub(1, w)
  299.     end
  300.  
  301.     return s .. string.rep(" ", w - #s)
  302.   end
  303.  
  304.   local function clampScroll()
  305.     local usable = math.max(1, H - 3)
  306.     local maxScroll = math.max(0, #lines - usable)
  307.  
  308.     if scrollOffset < 0 then scrollOffset = 0 end
  309.     if scrollOffset > maxScroll then scrollOffset = maxScroll end
  310.  
  311.     follow = scrollOffset == 0
  312.   end
  313.  
  314.   local function push(line)
  315.     line = tostring(line or "")
  316.  
  317.     while #line > W do
  318.       table.insert(lines, line:sub(1, W))
  319.       line = line:sub(W + 1)
  320.     end
  321.  
  322.     table.insert(lines, line)
  323.  
  324.     while #lines > maxLines do
  325.       table.remove(lines, 1)
  326.       if scrollOffset > 0 then scrollOffset = scrollOffset - 1 end
  327.     end
  328.  
  329.     if follow then scrollOffset = 0 end
  330.     clampScroll()
  331.     dirty = true
  332.   end
  333.  
  334.   local function append(chunk)
  335.     if not chunk or #chunk == 0 then return end
  336.  
  337.     chunk = chunk:gsub("\r", "")
  338.  
  339.     for line in (chunk .. "\n"):gmatch("([^\n]*)\n") do
  340.       push(line)
  341.     end
  342.   end
  343.  
  344.   local function draw(status)
  345.     status = status or lastStatus
  346.     lastStatus = status
  347.  
  348.     if not dirty then return end
  349.     dirty = false
  350.     clampScroll()
  351.  
  352.     color(C_TEXT, C_BG)
  353.     gpu.fill(1, 1, W, H, " ")
  354.  
  355.     color(C_BG, C_HEAD)
  356.     gpu.fill(1, 1, W, 1, " ")
  357.     gpu.set(1, 1, fit(
  358.       " dctail scroll-bg  " .. LOG_FILE ..
  359.       "  GPU " .. short(gpuAddr) ..
  360.       "  SCREEN " .. short(screenAddr),
  361.       W
  362.     ))
  363.  
  364.     local mode = follow and "FOLLOW" or ("SCROLL -" .. tostring(scrollOffset))
  365.     local help = "  Up/Dn PgUp/PgDn Home/End  F/Space=follow  C=clear  Q=stop"
  366.     color(C_MUTED, C_BG)
  367.     gpu.set(1, 2, fit((status or "") .. "  [" .. mode .. "]" .. help, W))
  368.  
  369.     local usable = math.max(1, H - 3)
  370.     local start = math.max(1, #lines - usable + 1 - scrollOffset)
  371.     local y = 3
  372.  
  373.     for i = start, #lines do
  374.       if y > H then break end
  375.  
  376.       local line = lines[i]
  377.       local fg = C_TEXT
  378.  
  379.       if line:find("cutoff") or line:find("SCRAM") or line:find("crashed") or line:find("FAILED") then
  380.         fg = C_RED
  381.       elseif line:find("warming") or line:find("charged") or line:find("ETA") then
  382.         fg = C_YELLOW
  383.       elseif line:find("running") or line:find("generation") then
  384.         fg = C_GREEN
  385.       elseif line:find("^tick=") or line:find("^===") then
  386.         fg = C_HEAD
  387.       end
  388.  
  389.       color(fg, C_BG)
  390.       gpu.set(1, y, fit(line, W))
  391.  
  392.       y = y + 1
  393.     end
  394.   end
  395.  
  396.   local function scrollBy(n)
  397.     follow = false
  398.     scrollOffset = scrollOffset + n
  399.     clampScroll()
  400.     dirty = true
  401.   end
  402.  
  403.   local function followBottom()
  404.     scrollOffset = 0
  405.     follow = true
  406.     dirty = true
  407.   end
  408.  
  409.   local function clearLocal()
  410.     lines = {}
  411.     lastSize = 0
  412.     scrollOffset = 0
  413.     follow = true
  414.     push("dctail local view cleared")
  415.     push("Watching " .. LOG_FILE)
  416.     push("")
  417.     dirty = true
  418.   end
  419.  
  420.   local function handleKey(char, code)
  421.     char = tonumber(char) or 0
  422.     code = tonumber(code) or 0
  423.     local c = (char >= 32 and char < 127) and string.lower(string.char(char)) or ""
  424.  
  425.     if c == "q" then
  426.       return false
  427.     elseif c == "f" or c == " " then
  428.       followBottom()
  429.     elseif c == "c" then
  430.       clearLocal()
  431.     elseif code == 200 then
  432.       scrollBy(1)
  433.     elseif code == 208 then
  434.       scrollBy(-1)
  435.     elseif code == 201 then
  436.       scrollBy(math.max(1, H - 4))
  437.     elseif code == 209 then
  438.       scrollBy(-math.max(1, H - 4))
  439.     elseif code == 199 then
  440.       follow = false
  441.       scrollOffset = 999999
  442.       clampScroll()
  443.       dirty = true
  444.     elseif code == 207 then
  445.       followBottom()
  446.     end
  447.  
  448.     return true
  449.   end
  450.  
  451.   push("dctail scroll-bg initialized")
  452.   push("Watching " .. LOG_FILE)
  453.   push("")
  454.   push("Controls: Up/Dn, PgUp/PgDn, Home/End, F or Space to follow, C clear, Q stop")
  455.   push("")
  456.  
  457.   while true do
  458.     local status = ""
  459.  
  460.     if not fs.exists(LOG_FILE) then
  461.       status = "Waiting for log file... uptime=" .. string.format("%.1f", computer.uptime())
  462.       dirty = true
  463.     else
  464.       local size = fs.size(LOG_FILE) or 0
  465.  
  466.       if size < lastSize then
  467.         append("")
  468.         append("--- log reset ---")
  469.         lastSize = 0
  470.       end
  471.  
  472.       if size > lastSize then
  473.         local f = io.open(LOG_FILE, "rb")
  474.  
  475.         if f then
  476.           f:seek("set", lastSize)
  477.           local chunk = f:read("*a")
  478.           f:close()
  479.  
  480.           append(chunk)
  481.           lastSize = size
  482.         end
  483.       end
  484.  
  485.       status = "Watching. size=" .. tostring(size) .. " bytes"
  486.       dirty = true
  487.     end
  488.  
  489.     draw(status)
  490.  
  491.     -- Cooperative yield. OpenOS threads need this to let other scripts run.
  492.     local ev = {event.pull(0.25)}
  493.  
  494.     if ev[1] == "interrupted" then
  495.       -- In foreground mode this lets Ctrl+C stop it.
  496.       -- In detached mode, ignore parent-shell interrupts so dcmaster Ctrl+C does not kill the tail.
  497.       if opts.foreground then break end
  498.     elseif ev[1] == "screen_resized" then
  499.       W, H = gpu.getResolution()
  500.       W = tonumber(W) or 80
  501.       H = tonumber(H) or 25
  502.       clampScroll()
  503.       dirty = true
  504.     elseif ev[1] == "key_down" then
  505.       if isTailKeyboard(ev[2]) then
  506.         if not handleKey(ev[3], ev[4]) then break end
  507.       end
  508.     elseif ev[1] == "touch" or ev[1] == "scroll" then
  509.       if ev[2] == screenAddr then
  510.         -- Some OC setups send mouse wheel as "scroll"; direction is usually ev[5].
  511.         local dir = tonumber(ev[5]) or 0
  512.         if dir > 0 then scrollBy(3)
  513.         elseif dir < 0 then scrollBy(-3) end
  514.       end
  515.     end
  516.   end
  517.  
  518.   color(C_TEXT, C_BG)
  519.   gpu.set(1, H, fit("dctail stopped.", W))
  520. end
  521.  
  522. --------------------------------------------------------------------------------
  523. -- SELF-BACKGROUND LAUNCHER
  524. --------------------------------------------------------------------------------
  525.  
  526. local rawArgs = {...}
  527. local opts = parseArgs(rawArgs)
  528.  
  529. if opts.listOnly then
  530.   listHardware()
  531.   return
  532. end
  533.  
  534. if opts.foreground then
  535.   runTail(rawArgs)
  536.   return
  537. end
  538.  
  539. local okThread, thread = pcall(require, "thread")
  540.  
  541. if not okThread or not thread then
  542.   print("thread library unavailable; running dctail in foreground.")
  543.   print("Use Ctrl+C to stop.")
  544.   runTail(rawArgs)
  545.   return
  546. end
  547.  
  548. local errLog = "/home/dctail_bg_error.txt"
  549. local argsCopy = {}
  550.  
  551. for i = 1, #rawArgs do
  552.   argsCopy[i] = rawArgs[i]
  553. end
  554.  
  555. local function logError(msg)
  556.   local f = io.open(errLog, "a")
  557.   if f then
  558.     f:write("\n--- dctail background error ---\n")
  559.     f:write(tostring(msg))
  560.     f:write("\n")
  561.     f:close()
  562.   end
  563. end
  564.  
  565. local t = thread.create(function()
  566.   local okRun, runtimeErr = xpcall(function()
  567.     runTail(argsCopy)
  568.   end, debug.traceback)
  569.  
  570.   if not okRun then
  571.     logError(runtimeErr)
  572.   end
  573. end)
  574.  
  575. t:detach()
  576.  
  577. print("Started dctail.lua in background.")
  578. print("Rear monitor should update independently.")
  579. print("Do not use dctail.lua & on OpenOS.")
  580. print("Errors, if any: " .. errLog)
Advertisement
Comments
  • 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