Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local mon = peripheral.wrap("right")
- mon.setTextScale(0.5)
- mon.setBackgroundColor(colors.black)
- mon.setTextColor(colors.white)
- function label(x, y, text)
- mon.setCursorPos(x, y)
- mon.write(text)
- end
- function formatPercent(str, fp)
- str = tostring(str)
- local dec = string.find(str, "%.")
- if fp > 4 then
- fp = 4
- end
- if dec then
- local wNum = string.sub(str,1,dec-1)
- local fNum = string.sub(str,dec+1,#str)
- if #fNum >= fp then
- return (wNum .. "." .. string.sub(fNum, 1, fp) .. "%")
- else
- return (wNum .. "." .. fNum .. "%")
- end
- else
- return (str.."%")
- end
- end
- function float(str, fp)
- str = tostring(str)
- local dec = string.find(str, "%.")
- if fp > 4 then
- fp = 4
- end
- if dec then
- local wNum = string.sub(str,1,dec-1)
- local fNum = string.sub(str,dec+1,#str)
- if #fNum >= fp then
- return tonumber(wNum .. "." .. string.sub(fNum, 1, fp))
- else
- return tonumber(wNum .. "." .. fNum)
- end
- else
- return tonumber(str)
- end
- end
- function drawMeter(x,y,width,height,clr, label,iNum,maxNum)
- --
- -- drawMeter(x pos, y pos, width, height, border color, meter label, current energy, max energy)
- --
- local oldBGColor = mon.getBackgroundColor()
- local label_start = x+3
- fpct = float(((iNum/maxNum) * 100),4)
- label = label .. " - " .. formatPercent(fpct,4)
- local label_end = label_start+#label+2
- --Height must be 3 or greater
- --Set to 3 instead of failing the render
- if height < 3 then
- height = 3
- end
- height=height-1
- mon.setBackgroundColor(clr)
- for i=x, x+width do
- if i >= label_start and i < label_end then
- -- Moved out of the loop
- --mon.setBackgroundColor(oldBGColor)
- --mon.setCursorPos(label_start,y)
- --mon.write(" " .. label .. " ")
- else
- mon.setBackgroundColor(clr)
- mon.setCursorPos(i,y)
- mon.write(" ")
- end
- mon.setBackgroundColor(clr)
- mon.setCursorPos(i,y+height)
- mon.write(" ")
- end
- for i=y, (y+(height)) do
- mon.setCursorPos(x,i)
- mon.write(" ")
- mon.setCursorPos(x+width, i)
- mon.write(" ")
- end
- --
- -- Write the label
- --
- mon.setBackgroundColor(oldBGColor)
- mon.setCursorPos(label_start,y)
- mon.write(" " .. label .. " ")
- -- Adjusted info for progress bar
- fx1 = x+1
- fy1 = y+1
- fw = width - 2
- fx2 = fx1+fw
- fy2 = fy1+(height-2)
- -- Counter and Percent for pixel rendering
- fc = 0
- fp = 0
- for i = fx1, fx2 do
- fp = float( (fc / fw)*100 , 4)
- fc = fc+1
- fbl = 1
- -- break loop after writing a few 'empty' bars
- -- removes artifacts and decreases time in the loop
- if fbl == 3 then
- break
- end
- for j = fy1, fy2 do
- if (fp > fpct and fp ~= 1) or fpct == 0 then
- mon.setBackgroundColor(colors.black)
- mon.setCursorPos(i,j)
- mon.write(" ")
- fbl = fbl+1
- else
- -- Progress bar colors
- if fpct <= 5 then
- mon.setBackgroundColor(colors.red)
- mon.setTextColor(colors.red)
- elseif fpct <= 10 and fpct > 5 then
- mon.setBackgroundColor(colors.red)
- mon.setTextColor(colors.black)
- elseif fpct <= 17.5 and fpct > 10 then
- mon.setBackgroundColor(colors.red)
- mon.setTextColor(colors.orange)
- elseif fpct <= 25 and fpct > 17.5 then
- mon.setBackgroundColor(colors.red)
- mon.setTextColor(colors.yellow)
- elseif fpct <= 37.5 and fpct > 25 then
- mon.setBackgroundColor(colors.orange)
- mon.setTextColor(colors.red)
- elseif fpct <= 50 and fpct > 37.5 then
- mon.setBackgroundColor(colors.orange)
- mon.setTextColor(colors.yellow)
- elseif fpct <= 67.5 and fpct > 50 then
- mon.setBackgroundColor(colors.yellow)
- mon.setTextColor(colors.orange)
- elseif fpct <= 75 and fpct > 67.5 then
- mon.setBackgroundColor(colors.yellow)
- mon.setTextColor(colors.lime)
- elseif fpct <= 87.5 and fpct > 75 then
- mon.setBackgroundColor(colors.lime)
- mon.setTextColor(colors.yellow)
- elseif fpct < 100 and fpct > 87.5 then
- mon.setBackgroundColor(colors.lime)
- mon.setTextColor(colors.white)
- elseif fpct == 100 then
- mon.setBackgroundColor(colors.lime)
- mon.setTextColor(colors.lime)
- end
- -- End Progress bar colors
- mon.setCursorPos(i,j)
- --mon.write(" ")
- --Random Chars because I could
- --Serves no real purpose
- mwn = math.random(1,4)
- if mwn == 1 then
- mon.write("*")
- elseif mwn == 2 then
- mon.write("+")
- elseif mwn == 3 then
- mon.write("\\")
- elseif mwn == 4 then
- mon.write("/")
- end
- --End random chars
- end
- end
- end
- mon.setTextColor(colors.white)
- mon.setBackgroundColor(colors.black)
- end -- drawMeter
Advertisement
Add Comment
Please, Sign In to add comment