Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Display help pages, optionally search for key words
- function pup.help(query)
- local allPages = {
- [[===================================================
- SETUP
- ===================================================
- > Load an existing image
- | nfp = pup.loadImage(path)
- | nfp = pup.parseImage(string)
- -- NFP image from file or string; as in original
- > Create a new image
- | nfp = pup.drawFilledBox(x,y,x2,y2,colour[,draw])
- | nfp = pup.drawBox(x,y,x2,y2,colour[,draw])
- -- returns image; if draw = false don't print
- > Inspect image size
- | width, height = nfp:getSize()
- * API must be loaded; e.g. 'pup = require("pup")']],
- [[===================================================
- MANIPULATE: IMAGE EDITING
- ===================================================
- > Resize the image canvas
- | nfp:resizeCanvas(width,height[,bgColour])
- -- crops or pads canvas to specified size
- > Overlay an image on top of this one
- | nfp:overlay(topImage,xPos,yPos[,resize])
- -- default xPos/yPos = 1, resize = true
- > Replace all instances of one colour with another
- | nfp:setColour(oldColour,newColour[,mutate])
- > Replace the colour of a specific pixel
- | nfp:setPixel(x,y,colour[,mutate])
- * If mutate = true then original is edited]],
- [[===================================================
- MANIPULATE: TRANSFORMATION
- ===================================================
- > Flip an image on given axes
- | nfp:mirror("x"|"y"|"xy")
- > Rotate an image 90 degrees clockwise
- | nfp:rotate()
- > Make an image larger (multiplies each pixel)
- | nfp:enlarge([factor])
- -- default factor = 2]],
- [[===================================================
- MANIPULATE: FORMAT EDITING
- ===================================================
- > Convert NFP image into blit image (BIMG) format
- | nfp:blitImage()
- -- 1:1 pixel conversion
- > Upscale image to compressed 2x3 pixel clusters
- | nfp:upscale([bgColour])
- -- default bgColour = current background
- * Upscaling may add pixels to ensure w%2 and h%3]],
- [[===================================================
- OUTPUT
- ===================================================
- > Draw NFP to screen
- | nfp:drawImage(x,y)
- | pup.drawImage(image,x,y)
- > Draw BIMG to screen
- | bimg:drawImage(x,y)
- > Save to file
- | nfp:saveImage(path)
- | bimg:saveImage(path)
- -- BIMG is serialised
- * File paths must not already exist]],
- }
- -- Filter pages if query is provided
- local pages = {}
- if query then
- query = string.lower(query)
- for _, page in ipairs(allPages) do
- if string.find(string.lower(page), query, 1, true) then
- table.insert(pages, page)
- end
- end
- if #pages == 0 then
- term.clear()
- term.setCursorPos(1,1)
- print("No help pages found for: "..query)
- print()
- print("Press any key to exit...")
- os.pullEvent("key")
- return
- end
- else
- pages = allPages
- end
- local oldBg = term.getBackgroundColour()
- local oldFg = term.getTextColour()
- local w,h = term.getSize()
- local currentPage = 1
- local totalPages = #pages
- local function restore()
- term.setBackgroundColour(oldBg)
- term.setTextColour(oldFg)
- term.clear()
- term.setCursorPos(1,1)
- end
- local function drawPage(pageNum)
- term.clear()
- term.setCursorPos(1,1)
- for line in pages[pageNum]:gmatch("[^\n]+") do
- local trimmed = line:match("^%s*(.-)%s*$") -- trim spaces
- if trimmed:sub(1,2) == "--" then
- term.setTextColour(colours.green) -- comment
- elseif trimmed:sub(1,1) == "*" then
- term.setTextColour(colours.red) -- warning
- elseif trimmed:sub(1,1) == "|" then
- term.setTextColour(colours.yellow) -- command
- elseif trimmed:sub(1,1) == ">" then
- term.setTextColour(colours.white) -- subtitle
- else
- term.setTextColour(colours.blue) -- default body
- end
- print(line:sub(1, w))
- end
- -- Footer instructions
- term.setTextColour(colours.lightGrey)
- term.setCursorPos(1, h - 1)
- local footer = "[ < ] Page " .. pageNum .. "/" .. totalPages .. " [ > ]"
- term.write(footer:sub(1, w))
- term.setCursorPos(1, h)
- term.write("Press [ backspace ] to exit")
- end
- drawPage(currentPage)
- while true do
- local event, key = os.pullEvent("key")
- if key == keys.right then
- if currentPage < totalPages then
- currentPage = currentPage + 1
- drawPage(currentPage)
- end
- elseif key == keys.left then
- if currentPage > 1 then
- currentPage = currentPage - 1
- drawPage(currentPage)
- end
- elseif key == keys.backspace then
- break
- end
- end
- restore()
- end
Advertisement
Add Comment
Please, Sign In to add comment