Advertisement
DOGGYWOOF

tabs manager

May 19th, 2024 (edited)
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. -- Tab Manager Program
  2. local function listTabs()
  3. local currentTab = multishell.getCurrent()
  4. local tabs = {}
  5.  
  6. for i = 1, multishell.getCount() do
  7. local title = multishell.getTitle(i)
  8. table.insert(tabs, {id = i, title = title})
  9. end
  10.  
  11. return currentTab, tabs
  12. end
  13.  
  14. local function drawTabs(currentTab, tabs)
  15. term.clear()
  16. term.setCursorPos(1, 1)
  17. print("Tab Manager - Select a tab to switch to it")
  18. print("========================================")
  19.  
  20. for _, tab in ipairs(tabs) do
  21. if tab.id == currentTab then
  22. print("-> [" .. tab.id .. "] " .. tab.title .. " (current)")
  23. else
  24. print(" [" .. tab.id .. "] " .. tab.title)
  25. end
  26. end
  27. end
  28.  
  29. local function getUserSelection(tabs)
  30. print("\nEnter the tab number to switch to it (or 'q' to quit): ")
  31. local input = read()
  32.  
  33. if input == 'q' or input == 'Q' then
  34. return nil
  35. end
  36.  
  37. local tabNumber = tonumber(input)
  38. for _, tab in ipairs(tabs) do
  39. if tab.id == tabNumber then
  40. return tabNumber
  41. end
  42. end
  43.  
  44. return nil
  45. end
  46.  
  47. local function main()
  48. while true do
  49. local currentTab, tabs = listTabs()
  50. drawTabs(currentTab, tabs)
  51. local selection = getUserSelection(tabs)
  52.  
  53. if selection then
  54. multishell.setFocus(selection)
  55. else
  56. break
  57. end
  58. end
  59.  
  60. term.clear()
  61. term.setCursorPos(1, 1)
  62. end
  63.  
  64. main()
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement