Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -----------------------
- -- API ui
- -- by SukaiPoppuGo
- --
- -----------------------
- -- initTerm
- -- Return current terminal as default output
- -- param: object terminal (optional)
- -- return: object terminal
- local function initTerm(_term)
- return _term or term.current()
- end
- -----------------------
- -- screenCutString
- -- Keep last part of a string (screen constraint)
- -- param: string str Full line
- -- param: object terminal (optional)
- -- return: string End of line
- function screenCutString(str, _term)
- _term = initTerm(_term)
- local screenW, screenH = _term.getSize()
- return string.len(str) >= screenW - 1 and string.sub(str, string.len(str) - screenW +2) or str
- end
- -----------------------
- -- output
- -- Write on chosen line, (and restore cursor)
- -- param: string str Text to write
- -- param: number ln Chosen line
- -- param: object terminal (optional)
- function output(str, ln, _term)
- _term = initTerm(_term)
- local x,y = _term.getCursorPos()
- _term.setCursorPos(1, ln)
- _term.clearLine()
- _term.write(str)
- _term.setCursorPos(x,y)
- end
- -----------------------
- -- outputEndScreen
- -- Write on the last line, (and restore cursor)
- -- param: string str Text to write
- -- param: object terminal (optional)
- function outputEndScreen(str, _term)
- _term = initTerm(_term)
- local screenW, screenH = _term.getSize()
- output(string.rep(" ", screenW - string.len(str))..str, screenH, _term)
- end
- -----------------------
- -- clearLines
- -- Clear multiple line, (and restore cursor)
- -- param: number startY First line
- -- param: number endY Last line
- -- param: object terminal (optional)
- function clearLines(startY, endY, _term)
- _term = initTerm(_term)
- local x,y = _term.getCursorPos()
- for i=startY,endY,1 do
- _term.setCursorPos(1,i)
- _term.clearLine()
- end
- _term.setCursorPos(x,y)
- end
- -----------------------
- -- blit
- -- Custom blit function, lenght up to screen width
- -- -- do not require each three strings to be of the same length
- -- -- text_colors and background_colour could be repeated as patterns
- -- param: string text
- -- param: string text_colors
- -- param: string background_colour
- -- param: object terminal (optional)
- function blit(text, textColor, backgroundColor, _term)
- _term = initTerm(_term)
- local screenW, screenH = _term.getSize()
- text = string.sub(text..string.rep(" ", screenW), 0, screenW)
- textColor = string.sub(string.rep(textColor, screenW), 0, screenW)
- backgroundColor = string.sub(string.rep(backgroundColor, screenW), 0, screenW)
- _term.blit(text, textColor, backgroundColor)
- end
- -----------------------
- -- cblit (Constraint blit)
- -- Custom blit function, lenght up to max width of each 3 strings
- -- -- do not require each three strings to be of the same length
- -- -- text_colors and background_colour could be repeated as patterns
- -- param: string text
- -- param: string text_colors
- -- param: string background_colour
- -- param: object terminal (optional)
- function cblit(text, textColor, backgroundColor, _term)
- _term = initTerm(_term)
- local screenW, screenH = _term.getSize()
- local width = math.min(screenW, math.max(string.len(text), string.len(textColor), string.len(backgroundColor)))
- text = string.sub(text..string.rep(" ", width), 0, width)
- textColor = string.sub(string.rep(textColor, width), 0, width)
- backgroundColor = string.sub(string.rep(backgroundColor, width), 0, width)
- _term.blit(text, textColor, backgroundColor)
- end
- -----------------------
- -- btn
- -- Enhanced button with a background colors
- -- param: string str Text display inside button
- -- param: boolean enabled Change colors, as clicked effect
- -- param: object terminal (optional)
- function btn(str, enabled, _term)
- _term = initTerm(_term)
- local C, L = string.char(149), string.len(str)
- local SF, S8, S0 = string.rep("f", L), string.rep("8", L), string.rep("0", L)
- if enabled then
- _term.blit(C..str..C, "f"..SF.."8", "8"..S8.."f")
- else
- _term.blit(" "..str.." ", "0"..S0.."0", "f"..SF.."f")
- end
- end
- -----------------------
- -- waitPressAnyKey
- -- Handle complete "any" key event, from key press to key up
- function waitPressAnyKey()
- os.pullEvent("key")
- os.pullEvent("key_up")
- end
- -----------------------
- -- box
- -- Draw boxes with outline
- -- param: mixed msg Box centents, could be a string, or table of strings for multiple lines
- -- param: number minWidth Extends box width (optional)
- -- param: number title Add title on top bar
- -- param: object terminal (optional)
- -- param: boolean _debug Debug trace
- function box(msg, minWidth, title, _term, _debug)
- _term = initTerm(_term)
- local screenW, screenH = _term.getSize()
- _debug = _debug or false
- minWidth = minWidth or 0
- msg = (type(msg) == "string") and {msg} or msg
- local x,y = _term.getCursorPos()
- local txtColor, bgColor = "0", "7"
- local maxLen = minWidth
- for k,v in pairs(msg) do
- maxLen = (maxLen < string.len(tostring(v))) and string.len(tostring(v)) or maxLen
- end
- maxLen = math.min(maxLen, screenW - 2)
- local pad = string.rep(" ", screenW)
- local borderColor = "8"
- local txtColorLine, bgColorLine = string.rep(txtColor, maxLen), string.rep(bgColor, maxLen)
- _term.blit(
- string.char(151)..string.rep(string.char(131), maxLen)..string.char(148),
- string.rep(borderColor, maxLen + 1)..bgColor,
- bgColor..bgColorLine..borderColor
- )
- for k,v in pairs(msg) do
- _term.setCursorPos(x, y + k)
- local line1 = string.char(149)..string.sub(tostring(v)..pad, 0, maxLen)..string.char(149)
- local line2 = borderColor..txtColorLine..bgColor
- local line3 = bgColor..bgColorLine..borderColor
- if _debug then
- print("line1="..string.len(line1))
- print("line2="..string.len(line2))
- print("line3="..string.len(line3))
- sleep(.1)
- os.pullEvent("none")
- end
- _term.blit( line1, line2, line3)
- end
- _term.setCursorPos(x, y + #msg + 1)
- _term.blit(
- string.char(138)..string.rep(string.char(143), maxLen)..string.char(133),
- bgColor..bgColorLine..bgColor,
- string.rep(borderColor, maxLen + 2)
- )
- if title then
- _term.setCursorPos(x, y)
- _term.blit(
- string.sub(title, 0, maxLen + 2),
- string.sub(string.rep(txtColor, #title), 0, maxLen + 2),
- string.sub(string.rep(bgColor, #title), 0, maxLen + 2)
- )
- end
- _term.setCursorPos(x, y + #msg + 2)
- end
- -----------------------
- -- inputFilename
- -- Act as read() input for filename
- -- -- Prevent overwriting, ask to confirm
- -- param: string msg Message prefixe to input
- -- param: number line Line where display the input, requires a free line below for warnings
- -- param: string filename Default filename (optional)
- -- param: object terminal (optional)
- function inputFilename(msg, line, filename, _term)
- _term = initTerm(_term)
- msg = msg or ""
- filename = filename or ""
- while true do
- output(msg..filename, line, _term)
- _term.setCursorPos(string.len(msg..filename) + 1, line)
- _term.setCursorBlink(true)
- local e, p = os.pullEvent()
- if e == "key" and p == keys.backspace then
- filename = string.sub(filename, 0, math.max(0, string.len(filename) - 1))
- elseif e == "key_up" and p == keys.enter and string.len(filename) > 0 then
- _term.setCursorBlink(false)
- if fs.exists(filename) then
- output("File already exists. Overwrite ? Y/N", line+1, _term)
- while true do
- local e2, p2 = os.pullEvent("key")
- if p2 == keys.y then
- return filename
- elseif p2 == keys.n then
- output("", line+1, _term)
- break
- end
- end
- else
- return filename
- end
- elseif e == "char" then
- filename = filename..p
- end
- end
- end
- -----------------------
- -- drawTurtleInventory
- -- Display a grid of 16 boxes, shape turtle inventory slots (4x4)
- -- -- Display a selected slot regardless the current selected slot of turtle
- -- -- (UI purpose, up to you to match the two if it's the desired result)
- -- -- Enhance non-empty slots
- -- param: number selection Enhance one slot (refer to slot number)
- -- param: object terminal (optional)
- function drawTurtleInventory(selection, _term)
- _term = initTerm(_term)
- selection = selection or 0
- local padX, padY = 2, 3
- local txtLine, txtColor, bgColor = "","","ffffff"
- for i = 0, 15 do
- local sLen = (i % 2 == 0) and 1 or 2
- local sBox = (sLen == 1) and string.char(143) or string.char(138)..string.char(133)
- txtLine = txtLine..sBox
- local boxColor = (turtle.getItemCount(i + 1) > 0) and "8" or "7"
- boxColor = (i == selection) and "0" or boxColor
- txtColor = txtColor..string.rep(boxColor, sLen)
- if i%4 == 3 then
- _term.setCursorPos(padX, padY + math.floor(i / 4))
- _term.blit(txtLine, txtColor, bgColor)
- txtLine, txtColor, bgColor = "","","ffffff"
- end
- end
- _term.setTextColor(colors.white)
- _term.setBackgroundColor(colors.black)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement