Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- showimg <nfp_url> [scale]
- -- Downloads a .nfp (paintutils image) and displays it centered on the first connected monitor.
- local args = { ... }
- if not args[1] then
- print("Usage: showimg <nfp_url> [scale]")
- return
- end
- local url = args[1]
- local scale = tonumber(args[2]) or 0.5
- local mon = peripheral.find("monitor")
- if not mon then
- print("No monitor found. Place the computer next to a monitor.")
- return
- end
- if scale < 0.5 then scale = 0.5 end
- if scale > 5 then scale = 5 end
- mon.setTextScale(scale)
- if not http then
- print("HTTP API unavailable. Enable http in CC config/server config.")
- return
- end
- local function fetch(u)
- local r = http.get(u, { ["Cache-Control"] = "no-cache" })
- if not r then return nil, "HTTP request failed" end
- local body = r.readAll()
- r.close()
- return body
- end
- local body, err = fetch(url)
- if not body then
- print("Download error: " .. (err or "unknown"))
- return
- end
- local tmp = "/.showimg.nfp"
- local fh = fs.open(tmp, "w")
- fh.write(body)
- fh.close()
- local img = paintutils.loadImage(tmp)
- if not img then
- print("Invalid .nfp content. Make sure the URL is the RAW .nfp file.")
- return
- end
- -- determine image width/height in characters
- local ih = #img
- local iw = 0
- for y = 1, ih do
- iw = math.max(iw, #img[y])
- end
- local ow, oh = mon.getSize()
- local ox = math.max(1, math.floor((ow - iw) / 2) + 1)
- local oy = math.max(1, math.floor((oh - ih) / 2) + 1)
- local old = term.redirect(mon)
- paintutils.drawFilledBox(1, 1, ow, oh, colors.black)
- paintutils.drawImage(img, ox, oy)
- term.redirect(old)
- print(("Done: %dx%d image at scale %.1f"):format(iw, ih, scale))
Advertisement
Add Comment
Please, Sign In to add comment