EWICS

Untitled

Nov 6th, 2025
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. -- showimg <nfp_url> [scale]
  2. -- Downloads a .nfp (paintutils image) and displays it centered on the first connected monitor.
  3.  
  4. local args = { ... }
  5. if not args[1] then
  6. print("Usage: showimg <nfp_url> [scale]")
  7. return
  8. end
  9.  
  10. local url = args[1]
  11. local scale = tonumber(args[2]) or 0.5
  12.  
  13. local mon = peripheral.find("monitor")
  14. if not mon then
  15. print("No monitor found. Place the computer next to a monitor.")
  16. return
  17. end
  18.  
  19. if scale < 0.5 then scale = 0.5 end
  20. if scale > 5 then scale = 5 end
  21. mon.setTextScale(scale)
  22.  
  23. if not http then
  24. print("HTTP API unavailable. Enable http in CC config/server config.")
  25. return
  26. end
  27.  
  28. local function fetch(u)
  29. local r = http.get(u, { ["Cache-Control"] = "no-cache" })
  30. if not r then return nil, "HTTP request failed" end
  31. local body = r.readAll()
  32. r.close()
  33. return body
  34. end
  35.  
  36. local body, err = fetch(url)
  37. if not body then
  38. print("Download error: " .. (err or "unknown"))
  39. return
  40. end
  41.  
  42. local tmp = "/.showimg.nfp"
  43. local fh = fs.open(tmp, "w")
  44. fh.write(body)
  45. fh.close()
  46.  
  47. local img = paintutils.loadImage(tmp)
  48. if not img then
  49. print("Invalid .nfp content. Make sure the URL is the RAW .nfp file.")
  50. return
  51. end
  52.  
  53. -- determine image width/height in characters
  54. local ih = #img
  55. local iw = 0
  56. for y = 1, ih do
  57. iw = math.max(iw, #img[y])
  58. end
  59.  
  60. local ow, oh = mon.getSize()
  61. local ox = math.max(1, math.floor((ow - iw) / 2) + 1)
  62. local oy = math.max(1, math.floor((oh - ih) / 2) + 1)
  63.  
  64. local old = term.redirect(mon)
  65. paintutils.drawFilledBox(1, 1, ow, oh, colors.black)
  66. paintutils.drawImage(img, ox, oy)
  67. term.redirect(old)
  68.  
  69. print(("Done: %dx%d image at scale %.1f"):format(iw, ih, scale))
  70.  
Advertisement
Add Comment
Please, Sign In to add comment