Advertisement
Guest User

bbar

a guest
Nov 15th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.10 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. local program = coroutine.create(function()
  33.     if fs.exists('/rom/programs/shell.lua') then
  34.         os.run({}, '/rom/programs/shell.lua')
  35.     else
  36.         os.run({}, '/rom/programs/shell')
  37.     end
  38. end)
  39.  
  40. local _, eventfilter = coroutine.resume(program)
  41. local timer = os.startTimer(0)
  42.  
  43. while true do
  44.     if coroutine.status(program) == 'dead' then
  45.         break
  46.     end
  47.    
  48.     local event = {os.pullEventRaw()}
  49.    
  50.     if event[1] == 'timer' and event[2] == timer then
  51.         barCallback(bar)
  52.         container.restoreCursor()
  53.         timer = os.startTimer(0.05)
  54.     elseif event[1] == 'terminate' then
  55.         local _, _eventfilter = coroutine.resume(program, 'terminate')
  56.         eventfilter = _eventfilter
  57.     elseif eventfilter == nil or event[1] == eventfilter then
  58.         local pass = true
  59.        
  60.         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
  61.             if topBar and event[4] <= barHeight or event[4] > h - barHeight then
  62.                 pass = false
  63.             elseif topBar then
  64.                 event[4] = event[4] - barHeight
  65.             end
  66.         end
  67.        
  68.         if pass == true then
  69.             local _, _eventfilter = coroutine.resume(program, table.unpack(event))
  70.             eventfilter = _eventfilter
  71.         end
  72.     end
  73. end
  74.  
  75. term.redirect(orig_term)
  76. local cx, cy = term.getCursorPos()
  77. bar.clear()
  78. term.setCursorPos(cx, cy)
  79.  
  80. print('Ended')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement