Advertisement
9551

sky's bar test

Aug 15th, 2021
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. --- progress bar is an api for drawing & updating progress bars.
  2. -- @module[kind=misc] progressbar
  3.  
  4. local expect = require("cc.expect").expect
  5.  
  6. local function save()
  7. local x,y = term.getCursorPos()
  8. local tbl = {
  9. fg = term.getTextColour(),
  10. bg = term.getBackgroundColour(),
  11. x = x,
  12. y = y,
  13. }
  14. return tbl
  15. end
  16.  
  17. local function restore(tbl)
  18. term.setCursorPos(tbl.x,tbl.y)
  19. term.setTextColour(tbl.fg)
  20. term.setBackgroundColour(tbl.bg)
  21. end
  22.  
  23. -- Soort coordinates. ex: 34,2 -> 21,34. (Sorts the x/y coordinates)
  24. -- This function is written w/ paintutils' one in mind, so it acts similar.
  25. local function sort(x,y,w,h)
  26. local lowX,highX,lowY,highY
  27. -- If the width is smaller than x, swap em around
  28. if w <= x then
  29. lowX = w
  30. highX = x
  31. else -- Leave them be
  32. lowX = x
  33. highX = w
  34. end
  35. -- Do the same, but for height
  36. if h <= y then
  37. lowY = h
  38. highY = y
  39. else -- Leave them be
  40. lowY = y
  41. highY = h
  42. end
  43. return lowX,highX,lowY,highY
  44. end
  45.  
  46. -- draw filled box
  47. local function dfb(x,y,w,h,col,tOutput)
  48. local tbl = save()
  49. x,w,y,h = sort(x,y,w,h)
  50. local width = w - x + 1
  51. -- Pretty simple, just fills in the space
  52. for o = y,h do
  53. tOutput.setCursorPos(x,o)
  54. tOutput.blit((" "):rep(width),("f"):rep(width),colours.toBlit(col):rep(width))
  55. end
  56. restore(tbl)
  57. end
  58.  
  59. local function update(bar,percent)
  60. expect(1,bar,"table")
  61. expect(2,percent,"number")
  62. -- Calculate pixel requirements
  63. if percent > 100 then percent = 100 end
  64. local pixels = math.floor(percent / (100 / bar.w) + 0.5) -- The math.floor + 0.5 acts as a rounding function.
  65. -- percent / (100 / barWidth) calculates how many pixels should be filled in the bar
  66. dfb(bar.x,bar.y,bar.x+bar.w-1,bar.y+bar.h-1,bar.bg,bar.terminal)
  67. if pixels ~= 0 then
  68. dfb(bar.x,bar.y,bar.x+pixels-1,bar.y+bar.h-1,bar.fg,bar.terminal)
  69. end
  70. return percent
  71. end
  72.  
  73. --- The progress bar object itself. Returns by @{create}
  74. local bar = {} --- @type bar
  75. local mt = {
  76. __index = bar,
  77. }
  78.  
  79. --- Update the bar to a percentage from 0 to 100.
  80. -- @tparam number percent Percentage of how full the bar is.
  81. function bar:update(percent)
  82. expect(1,percent,"number")
  83. self.fill = update(self,percent)
  84. end
  85.  
  86. --- Redraw the bar, putting it overtop of whatever has been drawn since.
  87. function bar:redraw()
  88. update(self,self.fill)
  89. end
  90.  
  91. --- Create a bar object
  92. -- @tparam number x X coordinate of the bar.
  93. -- @tparam number y Y coordinate of the bar.
  94. -- @tparam number w Width of the bar.
  95. -- @tparam number h Height of the bar.
  96. -- @tparam number fg The colour of the filled in bar.
  97. -- @tparam number bg The colour of the background of the bar.
  98. -- @tparam[opt] number fill The pre filled portion of the bar. Defaults to 0.
  99. -- @tparam[opt] table terminal The terminal to draw the bar on. Defaults to `term.current()`.
  100. local function create(x,y,w,h,fg,bg,fill,terminal)
  101. expect(1,x,"number")
  102. expect(2,y,"number")
  103. expect(3,w,"number")
  104. expect(4,h,"number")
  105. expect(5,fg,"number")
  106. expect(6,bg,"number")
  107. expect(7,fill,"number","nil")
  108. expect(8,terminal,"table","nil")
  109. fill = fill or 0
  110. terminal = terminal or term.current()
  111. local bar = {
  112. x = x,
  113. y = y,
  114. w = w,
  115. h = h,
  116. fg = fg,
  117. bg = bg,
  118. fill = fill,
  119. terminal = terminal,
  120. }
  121. -- draw the background
  122. dfb(x,y,x+w-1,y+h-1,bg,terminal)
  123. if fill ~= 0 then update(bar,fill) end
  124. return setmetatable(bar,mt)
  125. end
  126.  
  127. return setmetatable({
  128. create = create,
  129. }, {__call = function(_,...) return create(...) end})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement