aawwaaa

sc.lua

Aug 10th, 2025
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. local serialization = require("serialization")
  2. local component = require("component")
  3.  
  4. local function load_config()
  5. local file = io.open("/etc/sc", "r")
  6. if not file then
  7. return {}
  8. end
  9. local config = serialization.unserialize(file:read("*all"))
  10. file:close()
  11. return config
  12. end
  13.  
  14. local function save_config(config)
  15. local file = io.open("/etc/sc", "w")
  16. if not file then
  17. return
  18. end
  19. file:write(serialization.serialize(config, math.huge))
  20. file:close()
  21. end
  22.  
  23. local config = load_config()
  24.  
  25. local gpu = component.gpu
  26.  
  27. local args = require("shell").parse(...)
  28.  
  29. if args[1] == "scan" then
  30. local primary = component.getPrimary("screen").address
  31. if not config.primary then
  32. config.primary = primary
  33. end
  34. if not config.map then
  35. config.map = {}
  36. config.umap = {}
  37. end
  38. local index = config.index or 1
  39. for k, v in pairs(component.list("screen")) do
  40. if config.umap[k] == nil then
  41. config.map["s" .. tostring(index)] = k
  42. config.umap[k] = "s" .. tostring(index)
  43. index = index + 1
  44. end
  45. if k ~= primary then
  46. gpu.bind(k, false)
  47. local x, y = component.invoke(k, "getAspectRatio")
  48. gpu.setResolution(x * 10, y * 5)
  49. gpu.setBackground(0x000000)
  50. gpu.setForeground(0xffffff)
  51. gpu.fill(1, 1, x * 10, y * 5, " ")
  52. gpu.set(1, 1, "Screen " .. config.umap[k])
  53. gpu.set(1, 2, "Address: " .. k)
  54. gpu.set(1, 3, "Name: " .. config.umap[k])
  55. gpu.set(1, 4, "Primary: " .. (k == primary and "Yes" or "No"))
  56. gpu.set(1, 5, "Aspect Ratio: " .. x .. ":" .. y)
  57. end
  58. end
  59. gpu.bind(primary, false)
  60. config.index = index
  61. save_config(config)
  62. print("Done.")
  63. end
  64. if args[1] == "rename" then
  65. local name = args[2]
  66. local newName = args[3]
  67. if not config.map[name] then
  68. print("Screen not found")
  69. return
  70. end
  71. local screen = config.map[name]
  72. config.map[name] = nil
  73. config.map[newName] = screen
  74. config.umap[screen] = newName
  75. save_config(config)
  76. print("Done.")
  77. end
  78. if args[1] == "primary" then
  79. local name = args[2]
  80. if not config.map[name] then
  81. print("Screen not found")
  82. return
  83. end
  84. config.primary = config.map[name]
  85. save_config(config)
  86. print("Done.")
  87. end
  88.  
  89. local function switchTo(address)
  90. gpu.bind(address, false)
  91. gpu.setResolution(gpu.maxResolution())
  92. require("tty").clear()
  93. end
  94.  
  95. if args[1] == "switch" then
  96. local name = args[2]
  97. if not config.map[name] then
  98. print("Screen not found")
  99. return
  100. end
  101. local screen = config.map[name]
  102. switchTo(screen)
  103. end
  104. if args[1] == "reset" then
  105. local primary = config.primary
  106. if not primary then
  107. print("Primary screen not found")
  108. return
  109. end
  110. switchTo(primary)
  111. end
  112.  
  113. if args[1] == "with" then
  114. local name = args[2]
  115. if not config.map[name] then
  116. print("Screen not found")
  117. return
  118. end
  119. local screen = config.map[name]
  120. switchTo(screen)
  121. local left = table.concat({select(3, ...)}, " ")
  122. os.execute(left)
  123. switchTo(config.primary)
  124. end
  125.  
Advertisement
Add Comment
Please, Sign In to add comment