Repository navigation
Expand file tree
/
Copy pathperipheralDetector.lua
More file actions
204 lines (185 loc) · 5.8 KB
/
Copy pathperipheralDetector.lua
File metadata and controls
204 lines (185 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
--[[
PeripheralDetector program
By Out-Feu
version 2.0.0
Free to distribute/alter
so long as proper credit to original
author is maintained.
This program will display the available methods of all the connected peripherals.
A side can be provided when launching the program to only display the methods of the associated peripheral.
--]]
function printPeripheral(side, file)
local per = peripheral.getType(side)
if file == nil then
print(side .. ": " .. per)
else
file.writeLine(side .. ": " .. per)
end
if displayMethods then
printMethods(side, file)
end
end
function printMethods(side, file)
local methods = peripheral.getMethods(side)
for i, method in pairs(methods) do
if file == nil then
print(" " .. i .. " - " .. method)
else
file.writeLine(" " .. i .. " - " .. method)
end
end
end
function findPeripherals()
if sides == nil or next(sides) == nil then
local foundPeripheral = false
local per = peripheral.getNames()
for i, side in pairs(per) do
table.insert(peripherals, side)
foundPeripheral = true
end
if not foundPeripheral then
printError("No peripheral was found.")
end
else
for i, side in pairs(sides) do
if peripheral.isPresent(side) then
table.insert(peripherals, side)
else
printError("No peripheral was found for the side: " .. side)
end
end
end
end
--------------------------------------------------------------------------------
graphicInterface = true -- Should the program display its graphical interface instead of the standard output
displayMethods = true -- Should all peripherals' methods be printed
exportFile = "" -- If set, output will be exported to a file (override content)
sides = {...}
peripherals = {}
--------------------------------------------------------------------------------
findPeripherals()
if peripherals == nil or next(peripherals) == nil then
return -- exit from the script if no peripherals were found
end
if exportFile ~= nil and exportFile ~= "" then
local file = fs.open(exportFile, "w")
for i, peripheral in pairs(peripherals) do
printPeripheral(peripheral, file)
end
file.close()
print("Exported " .. #peripherals .. " peripherals to " .. exportFile)
elseif not graphicInterface then
for i, peripheral in pairs(peripherals) do
printPeripheral(peripheral, nil)
end
end
if graphicInterface then
local width, height = term.getSize()
local usableHeight = height - 5
local currentSide = ""
local currentMethods = nil
local selectedIndex = 1
local scrollIndex = 0
repeat --main loop
term.clear()
-- Print title and help
term.setCursorPos(1, 1)
if currentSide == "" then
term.write("Found " .. #peripherals .. " connected peripherals")
else
term.write(currentSide .. ": " .. peripheral.getType(currentSide) .. " (" .. #currentMethods .. " methods)")
end
term.setCursorPos(1, 2)
term.write(string.rep("=", width))
if currentSide == "" then
term.setCursorPos(1, height - 2)
term.write(string.rep("=", width))
term.setCursorPos(1, height - 1)
term.write("ARROW KEY to move")
term.setCursorPos(1, height)
term.write("ENTER or SPACE to select")
elseif currentMethods ~= nil and #currentMethods > usableHeight then
term.setCursorPos(1, height - 2)
term.write(string.rep("=", width))
term.setCursorPos(1, height - 1)
term.write("ARROW KEY to move")
term.setCursorPos(1, height)
term.write("SHIFT or BACKPSPACE to go back")
else
term.setCursorPos(1, height - 1)
term.write(string.rep("=", width))
term.setCursorPos(1, height)
term.write("SHIFT or BACKPSPACE to go back")
end
if currentSide == "" then
-- Print peripherals
for n = 1 + scrollIndex, usableHeight + scrollIndex do
if n > #peripherals then
break
end
term.setCursorPos(1, 2 + n - scrollIndex)
if n == selectedIndex then
term.write(" > ")
else
term.write(" - ")
end
term.write(peripherals[n] .. ": " .. peripheral.getType(peripherals[n]))
end
else
-- Print methods
for n = 1 + scrollIndex, usableHeight + scrollIndex do
if n > #currentMethods then
break
end
term.setCursorPos(1, 2 + n - scrollIndex)
term.write(" - " .. currentMethods[n])
end
end
-- Wait for key press
local event, key = os.pullEvent("key")
if key == keys.up or key == keys.pageUp then -- UP ARROW or PAGE UP was pressed
if currentSide == "" then
if selectedIndex > 1 then
selectedIndex = selectedIndex - 1
else
selectedIndex = #peripherals
end
elseif currentSide ~= "" and currentMethods ~= nil and scrollIndex > 1 then
scrollIndex = scrollIndex - 1
end
elseif key == keys.down or key == keys.pageDown then -- DOWN ARROW or PAGE DOWN was pressed
if currentSide == "" then
if selectedIndex < #peripherals then
selectedIndex = selectedIndex + 1
else
selectedIndex = 1
end
elseif currentSide ~= "" and currentMethods ~= nil and (scrollIndex + usableHeight) < #currentMethods then
scrollIndex = scrollIndex + 1
end
elseif (key == keys.enter or key == keys.space or key == keys.insert) and currentSide == "" then -- ENTER or SPACE or INSERT was pressed
currentSide = peripherals[selectedIndex]
currentMethods = peripheral.getMethods(currentSide)
scrollIndex = 0
usableHeight = height - 4
if #currentMethods > usableHeight then
usableHeight = height - 5
end
elseif (key == keys.leftShift or key == keys.rightShift or key == keys.backspace or key == keys.delete) and currentSide ~= "" then -- SHIFT or BACKSPACE or DELETE was pressed
currentSide = ""
currentMethods = nil
scrollIndex = 0
usableHeight = height - 5
end
-- Handle scrolling
if currentSide == "" then
if selectedIndex > usableHeight + scrollIndex then
scrollIndex = selectedIndex - usableHeight
elseif selectedIndex <= scrollIndex then
scrollIndex = selectedIndex - 1
end
end
until key == keys.t
term.clear()
term.setCursorPos(1, 1)
end