Advertisement
Guest User

bbar

a guest
Nov 15th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.35 KB | None | 0 0
  1. local args = {...}
  2.  
  3. if #args ~= 1 and #args ~= 2 then
  4.     print('Usage: bbar <bar> [t/top/b/bottom]')
  5.     return
  6. end
  7.  
  8. local barCallback = dofile(args[1])
  9.  
  10. if type(barCallback) ~= 'function' then
  11.     print('Invalid bar file (not a function)')
  12.     return
  13. end
  14.  
  15. if #args == 2 then
  16.     local arg2 = args[2]
  17.    
  18.     if arg2 ~= 't' and arg2 ~= 'top' and arg2 ~= 'b' and arg2 ~= 'bottom' then
  19.         print('Invalid argument #2 (expected t/top/b/bottom): ' .. arg2)
  20.     end
  21. end
  22.  
  23. local barHeight = 1
  24. local topBar = #args == 2 and args[2]:sub(1, 1) == 't'
  25.  
  26. local w, h = term.getSize()
  27. local container = window.create(term.current(), 1, (topBar and barHeight or 0) + 1, w, h - barHeight)
  28. local orig_term = term.redirect(container)
  29.  
  30. local bar = window.create(orig_term, 1, (topBar and 1 or h + 1 - barHeight), w, barHeight)
  31.  
  32. --[[
  33. parallel.waitForAny(
  34.     function()
  35.         shell.run('shell')
  36.     end,
  37.     function()
  38.         while running do
  39.             barCallback(bar)
  40.             container.restoreCursor()
  41.            
  42.             local timer = os.startTimer(0.05)
  43.            
  44.             while true do
  45.                 local name, id = os.pullEventRaw()
  46.                
  47.                 if name == 'timer' and id == timer then
  48.                     break
  49.                 end
  50.             end
  51.         end
  52.     end
  53. )
  54. ]]
  55.  
  56. local program = coroutine.create(function()
  57.     os.run({}, '/rom/programs/shell')
  58. end)
  59.  
  60. local _, eventfilter = coroutine.resume(program)
  61. local timer = os.startTimer(0)
  62.  
  63. while true do
  64.     if coroutine.status(program) == 'dead' then
  65.         break
  66.     end
  67.    
  68.     local event = {os.pullEventRaw()}
  69.    
  70.     if event[1] == 'timer' and event[2] == timer then
  71.         barCallback(bar)
  72.         container.restoreCursor()
  73.         timer = os.startTimer(0.05)
  74.     elseif event[1] == 'terminate' then
  75.         local _, _eventfilter = coroutine.resume(program, 'terminate')
  76.         eventfilter = _eventfilter
  77.     elseif eventfilter == nil or event[1] == eventfilter then
  78.         local pass = true
  79.        
  80.         if event[1] == 'mouse_click' or event[1] == 'mouse_up' or event[1] == 'mouse_scroll' or event[1] == 'mouse_drag' or event[1] == 'monitor_touch' then
  81.             if topBar and event[4] <= barHeight or event[4] > h - barHeight then
  82.                 pass = false
  83.             elseif topBar then
  84.                 event[4] = event[4] - barHeight
  85.             end
  86.         end
  87.        
  88.         if pass == true then
  89.             local _, _eventfilter = coroutine.resume(program, table.unpack(event))
  90.             eventfilter = _eventfilter
  91.         end
  92.     end
  93. end
  94.  
  95. term.redirect(orig_term)
  96. local cx, cy = term.getCursorPos()
  97. bar.clear()
  98. term.setCursorPos(cx, cy)
  99.  
  100. print('Ended')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement