Advertisement
TheGameBoy_95

Untitled

May 21st, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.77 KB | None | 0 0
  1.  
  2. Skip to content
  3.  
  4. Why GitHub?
  5.  
  6.  
  7.  
  8.  
  9. Enterprise
  10. Explore
  11.  
  12.  
  13.  
  14. Marketplace
  15. Pricing
  16.  
  17.  
  18.  
  19.  
  20.  
  21. Sign in
  22. Sign up
  23.  
  24. 1
  25. 1
  26.  
  27. 30
  28.  
  29. lyqyd/OpenCCSensors forked from Cloudhunter/OpenCCSensors
  30. Code
  31. Pull requests 0
  32. Projects 0
  33. Insights
  34. Join GitHub today
  35.  
  36. GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together.
  37. OpenCCSensors/resources/lua/sensorview
  38. @lyqyd lyqyd initial sensor data viewing program 7ea7964 on 29 Nov 2012
  39. 177 lines (170 sloc) 4.77 KB
  40. local sideNames = rs.getSides()
  41. local sensorSides = {}
  42. local sideSelection, targetSelection, targetOffset, detailOffset = 1, 1, 1, 1
  43. local detailLines, targetNameMenuTable
  44.  
  45. local function checkSensors()
  46. for _,side in ipairs(sideNames) do
  47. if peripheral.getType(side) == "sensor" then
  48. sensorSides[side] = true
  49. else
  50. sensorSides[side] = false
  51. end
  52. end
  53. end
  54.  
  55. local function writeEntry(menuTable, index, cursorPos)
  56. if cursorPos == index then
  57. term.setBackgroundColor(term.isColor() and colors.blue or colors.white)
  58. term.setTextColor(term.isColor() and colors.white or colors.black)
  59. term.write(string.sub(menuTable[index], 1, 16))
  60. term.setBackgroundColor(colors.black)
  61. term.setTextColor(colors.white)
  62. else
  63. term.write(string.sub(menuTable[index], 1, 16))
  64. end
  65. end
  66.  
  67. local function toLines(currTable, linesTable, trackingTable, depth)
  68. for k,v in pairs(currTable) do
  69. if type(v) == "table" then
  70. table.insert(linesTable, string.rep(" ", depth)..tostring(k)..":")
  71. if trackingTable[v] then
  72. table.insert(linesTable, string.rep(" ", depth + 1).."<Cyclic Reference>")
  73. else
  74. trackingTable[v] = true
  75. toLines(v, linesTable, trackingTable, depth + 1)
  76. end
  77. else
  78. table.insert(linesTable, string.rep(" ", depth)..tostring(k).."> "..tostring(v))
  79. end
  80. end
  81. end
  82.  
  83. local function redraw()
  84. w, h = term.getSize()
  85. term.clear()
  86. term.setCursorPos(1, 1)
  87. term.write("=Sensor Info Viewer="..string.rep("=", w - 20))
  88. term.setCursorPos(1, 2)
  89. for n,side in ipairs(sideNames) do
  90. if n == sideSelection then
  91. term.setBackgroundColor(term.isColor() and colors.blue or colors.white)
  92. term.setTextColor(term.isColor() and colors.white or colors.black)
  93. term.write(side)
  94. term.setBackgroundColor(colors.black)
  95. term.setTextColor(colors.white)
  96. term.write(" ")
  97. else
  98. term.write(side.." ")
  99. end
  100. end
  101. term.setCursorPos(1, 3)
  102. term.write("-Targets--------+-Info-"..string.rep("-", w - 23))
  103.  
  104. checkSensors()
  105. if sensorSides[sideNames[sideSelection]] then
  106. --targets
  107. local targetNames = peripheral.call(sideNames[sideSelection], "getTargets")
  108. targetNameMenuTable = {}
  109. for k,v in pairs(targetNames) do
  110. table.insert(targetNameMenuTable, k)
  111. end
  112. term.setCursorPos(1, 4)
  113. if targetOffset > 1 then
  114. term.write("/\\")
  115. else
  116. writeEntry(targetNameMenuTable, 1, targetSelection)
  117. end
  118. --h-5 to leave room for top and bottom entries.
  119. for i=1, math.min(h - 5, #targetNameMenuTable - 1) do
  120. term.setCursorPos(1, i + 4)
  121. writeEntry(targetNameMenuTable, targetOffset + i, targetSelection)
  122. end
  123. if #targetNameMenuTable >= h then
  124. term.setCursorPos(1, h)
  125. if #targetNameMenuTable > targetOffset + h - 1 then
  126. term.write("\\/")
  127. else
  128. writeEntry(targetNameMenuTable, #targetNameMenuTable, targetSelection)
  129. end
  130. end
  131.  
  132. --detailed info.
  133. detailLines = {}
  134. toLines(peripheral.call(sideNames[sideSelection], "getDetailTarget", targetNameMenuTable[targetSelection]), detailLines, {}, 0)
  135. for i=1, math.min(h - 3, #detailLines - ((detailOffset - 1) * (h - 3))) do
  136. term.setCursorPos(17, i + 3)
  137. term.write("|"..string.sub(detailLines[(detailOffset - 1) * (h - 3) + i], 1, w - 17))
  138. end
  139. local currX, currY = term.getCursorPos()
  140. for i=currY + 1, h do
  141. term.setCursorPos(17, i)
  142. term.write("|")
  143. end
  144. else
  145. term.setCursorPos(1, 4)
  146. term.write("No sensor found |")
  147. for i=5, h do
  148. term.setCursorPos(17, i)
  149. term.write("|")
  150. end
  151. end
  152. term.setCursorPos(1, h)
  153. end
  154.  
  155. while true do
  156. redraw()
  157. local e, p1 = os.pullEvent()
  158. if e == "key" then
  159. if p1 == 203 then
  160. --left
  161. if sideSelection > 1 then
  162. sideSelection = sideSelection - 1
  163. targetSelection = 1
  164. detailOffset = 1
  165. detailLines = nil
  166. end
  167. elseif p1 == 205 then
  168. --right
  169. if sideSelection < 6 then
  170. sideSelection = sideSelection + 1
  171. targetSelection = 1
  172. detailOffset = 1
  173. detailLines = nil
  174. end
  175. elseif p1 == 200 then
  176. --up
  177. if targetSelection > 1 then
  178. targetSelection = targetSelection - 1
  179. detailOffset = 1
  180. detailLines = nil
  181. end
  182. elseif p1 == 208 then
  183. --down
  184. if targetNameMenuTable and targetSelection < #targetNameMenuTable then
  185. targetSelection = targetSelection + 1
  186. detailOffset = 1
  187. detailLines = nil
  188. end
  189. elseif p1 == 201 then
  190. --pgup, moves detail
  191. if detailOffset > 1 then
  192. detailOffset = detailOffset - 1
  193. end
  194. elseif p1 == 209 then
  195. --pgdown, moves detail
  196. if detailLines and detailOffset < math.ceil(#detailLines / (h - 3)) then
  197. detailOffset = detailOffset + 1
  198. end
  199. end
  200. elseif e == "char" then
  201. if p1 == "s" then
  202. --save sensor detailed output.
  203. if detailLines then
  204. local fileHandle = io.open("sensorDetailed-"..sideNames[sideSelection], "w")
  205. if fileHandle then
  206. for k, v in ipairs(detailLines) do
  207. fileHandle:write(v.."\n")
  208. end
  209. fileHandle:close()
  210. end
  211. end
  212. elseif p1 == "q" then
  213. return
  214. end
  215. end
  216. end
  217.  
  218. © 2019 GitHub, Inc.
  219. Terms
  220. Privacy
  221. Security
  222. Status
  223. Help
  224.  
  225. Contact GitHub
  226. Pricing
  227. API
  228. Training
  229. Blog
  230. About
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement