Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2014
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.15 KB | None | 0 0
  1. local component = require("component")
  2. local event = require("event")
  3. local fs = require("filesystem")
  4. local keyboard = require("keyboard")
  5. local shell = require("shell")
  6. local term = require("term")
  7. local text = require("text")
  8. local unicode = require("unicode")
  9. local sides = require("sides")
  10. local colors=require("colors")
  11. local gml=require("gml")
  12.  
  13. local gpu = component.gpu
  14. local compInfo = {}
  15.  
  16. local function spaces(cnt)
  17. return string.rep(string.char(32), cnt)
  18. end
  19.  
  20. local function spChar(letter, cnt)
  21. return string.rep(unicode.char(letter), cnt)
  22. end
  23.  
  24. local OC_1 = " "..spChar(0x2584,3).." "..spChar(0x2584,4).." "..spChar(0x2584,5).." "..spChar(0x2584,1).." "..spChar(0x2584,1).." "..spChar(0x2584,4).." "..spChar(0x2584,3).." "..spChar(0x2584,1).." "..spChar(0x2584,1).." "..spChar(0x2584,4).." "..spChar(0x2584,1).." "..spChar(0x2584,1).." "..spChar(0x2584,5).." "..spChar(0x2584,5).." "..spChar(0x2584,4).." "..spChar(0x2584,4).." "
  25. local OC_2 = " "..spChar(0x2588,1).." "..spChar(0x2588,1).." "..spChar(0x2588,1).." "..spChar(0x2588,1).." "..spChar(0x2588,1).." "..spChar(0x2588,1)..spChar(0x2584,1).." "..spChar(0x2588,1).." "..spChar(0x2588,1).." "..spChar(0x2588,1).." "..spChar(0x2588,1).." "..spChar(0x2588,1)..spChar(0x2580,1)..spChar(0x2584,1)..spChar(0x2580,1)..spChar(0x2588,1).." "..spChar(0x2588,1).." "..spChar(0x2588,1).." "..spChar(0x2588,1).." "..spChar(0x2588,1).." "..spChar(0x2588,1).." "..spChar(0x2588,1).." "..spChar(0x2588,1).." "..spChar(0x2588,1).." "..spChar(0x2588,1).." "
  26. local OC_3 = " "..spChar(0x2588,1).." "..spChar(0x2588,1).." "..spChar(0x2588,1)..spChar(0x2580,3).." "..spChar(0x2588,1)..spChar(0x2580,2).." "..spChar(0x2588,1).." "..spChar(0x2588,1).." "..spChar(0x2588,1).." "..spChar(0x2588,1).." "..spChar(0x2588,1).." "..spChar(0x2588,1).." "..spChar(0x2588,1).." "..spChar(0x2588,1).." "..spChar(0x2588,1)..spChar(0x2580,3).." "..spChar(0x2588,1).." "..spChar(0x2588,1).." "..spChar(0x2588,1).." "..spChar(0x2588,1)..spChar(0x2580,2).." "..spChar(0x2588,1)..spChar(0x2580,1)..spChar(0x2588,1)..spChar(0x2580,1).." "..spChar(0x2580,2)..spChar(0x2584,1).." "
  27. local OC_4 = " "..spChar(0x2588,1).." "..spChar(0x2588,1).." "..spChar(0x2588,1).." "..spChar(0x2588,1).." "..spChar(0x2588,1).." "..spChar(0x2588,2).." "..spChar(0x2588,1).." "..spChar(0x2588,1).." "..spChar(0x2588,1).." "..spChar(0x2588,1).." "..spChar(0x2588,1).." "..spChar(0x2588,1).." "..spChar(0x2588,1).." "..spChar(0x2588,1).." "..spChar(0x2588,1).." "..spChar(0x2588,1).." "..spChar(0x2588,1).." "..spChar(0x2588,1).." "..spChar(0x2588,1).." "
  28. local OC_5 = " "..spChar(0x2580,3).." "..spChar(0x2580,1).." "..spChar(0x2580,5).." "..spChar(0x2580,1).." "..spChar(0x2580,1).." "..spChar(0x2580,4).." "..spChar(0x2580,3).." "..spChar(0x2580,1).." "..spChar(0x2580,1).." "..spChar(0x2580,1).." "..spChar(0x2580,3).." "..spChar(0x2580,1).." "..spChar(0x2580,5).." "..spChar(0x2580,1).." "..spChar(0x2580,1).." "..spChar(0x2580,4).." "
  29.  
  30. local gpu = component.gpu
  31. local Tier1 = 1
  32. local Tier2 = 4
  33. local Tier3 = 8
  34.  
  35. local menuList = {}
  36. local menuLen = 1
  37. local menuWid = 0
  38.  
  39. local compList = {}
  40. local tmpList = {}
  41. local compLen = 1
  42. local compListWid = 0
  43. local col = 1
  44. local currRow = 1
  45. local sentStr = {}
  46.  
  47. local offset = 0
  48. local running = true
  49. local w, h = gpu.getResolution()
  50. local tmpW, tmpH = gpu.getResolution()
  51.  
  52. local fname = "CompInfo.txt"
  53. local filename = shell.resolve(fname)
  54.  
  55. local gui=gml.create("center",15,80,30)
  56.  
  57. gui:addLabel(2,1,14,"Component")
  58. local contentsLabel=gui:addLabel(30,1,31,"contents of")
  59.  
  60. local function table_count(tt, item)
  61. local count
  62. count = 0
  63. for ii,xx in pairs(tt) do
  64. if item == xx then count = count + 1 end
  65. end
  66. return count
  67. end
  68.  
  69. local function table_unique(tt)
  70. local newtable = {}
  71. for ii,xx in ipairs(tt) do
  72. if table_count(newtable, xx) == 0 then
  73. newtable[#newtable+1] = xx
  74. end
  75. end
  76. return newtable
  77. end
  78.  
  79. local function isAdvanced()
  80. return gpu.getDepth()
  81. end
  82.  
  83. local function setColors(fore, back)
  84. gpu.setForeground(fore)
  85. gpu.setBackground(back)
  86. end
  87.  
  88. local function setCursor(col, row)
  89. term.setCursor(col, row)
  90. end
  91.  
  92. local function getSize()
  93. return gpu.getResolution()
  94. end
  95.  
  96. local function drawBox(col, row, wid, hgt, fore, back, opt)
  97. local ul = {0x250C, 0x2554}
  98. local ur = {0x2510, 0x2557}
  99. local ll = {0x2514, 0x255A}
  100. local lr = {0x2518, 0x255D}
  101. local sl = {0x2502, 0x2551}
  102. local al = {0x2500, 0x2550}
  103. setColors(fore, back)
  104. gpu.set(col, row, unicode.char(ul[opt])..spChar(al[opt], wid - 2)..unicode.char(ur[opt]))
  105. for a = 1, hgt - 2 do
  106. gpu.set(col, row + a, unicode.char(sl[opt])..spaces(wid - 2)..unicode.char(sl[opt]))
  107. end
  108. gpu.set(col, row + hgt - 2, unicode.char(ll[opt])..spChar(al[opt], wid - 2)..unicode.char(lr[opt]))
  109. setCursor(col, row)
  110. end
  111.  
  112. local function printXY(col, row, menuSel)
  113. gpu.set(col, row, menuSel)
  114. end
  115.  
  116. local function printDocXY(col, row, menuSel)
  117. setCursor(col, row)
  118. print(menuSel)
  119. end
  120.  
  121.  
  122. local function centerText(row, msg)
  123. local w, h = getSize()
  124. local len = string.len(msg)
  125. gpu.set((w - len)/2, row, msg)
  126. end
  127.  
  128. local function centerIntroText(row, msg)
  129. local msg1 = " * * * * * ** * * * * * * * * * * * * * * * * "
  130. local w, h = getSize()
  131. local len = string.len(msg1)
  132. gpu.set((w - len)/2, row, msg)
  133. end
  134.  
  135.  
  136. local defaultTheme = { -- Water Theme
  137. textColor = 0xFFFFFF,
  138. background = 0x000099,
  139. introText = 0xFF0000,
  140. introBackground = 0x000000,
  141. menuHintText = 0xFFFF00,
  142. menuHint = 0x000000,
  143. prompt = 0xBBBB00,
  144. promptHighlight = 0x000000,
  145. fancyDots = 0xAAFFCC,
  146. fancyBackground = 0x113322
  147. }
  148.  
  149. local normalTheme = { -- Water Theme
  150. textColor = 0xFFFFFF,
  151. background = 0x000000,
  152. prompt = 0x000000,
  153. promptHighlight = 0xFFFFFF,
  154. }
  155.  
  156. term.clear()
  157. local w, h = gpu.getResolution()
  158.  
  159. local function intro()
  160. local w, h = gpu.getResolution()
  161. local msg1 = " * * * * * ** * * * * * * * * * * * * * * * * "
  162. local len = string.len(msg1) + 1
  163.  
  164. drawBox(1, 3, w, h - 3, theme.textColor, theme.background, 2)
  165. drawBox((w - len)/2 - 2, 5, len + 4, 10, theme.introText, theme.introBackground, 1)
  166.  
  167. centerIntroText(6, OC_1)
  168. centerIntroText(7, OC_2)
  169. centerIntroText(8, OC_3)
  170. centerIntroText(9, OC_4)
  171. centerIntroText(10, OC_5)
  172. centerText(12, "Component Viewer")
  173. end
  174.  
  175. if (isAdvanced() == Tier3) then
  176. theme = defaultTheme
  177. intro()
  178. else
  179. theme = normalTheme
  180. end
  181.  
  182. local function strripos(s, delim)
  183. return s:match('^.*()'..delim)
  184. end
  185.  
  186. local function getMenuList()
  187. menuList = {}
  188. local tmpName = ""
  189. for address, name in component.list() do
  190. table.insert(menuList,name)
  191. for k, v in pairs(component.proxy(address)) do
  192. tmpName = name.."."..k
  193. table.insert(compList,tmpName)
  194. end
  195. end
  196. table.sort(menuList)
  197. end
  198.  
  199. function loadInfoData(select)
  200. local lineLen, lineHeight = 0, 0
  201. local lineCnt = 0
  202. local optName = "["..string.upper(select).."]"
  203. local optNameEnd = "["..string.upper(select).."_END]"
  204. local optNameStart, optNameLast = 0,0
  205. local w, h = gpu.getResolution()
  206. local tmpLine = {}
  207. local tmpStr = ""
  208. sentStr = {}
  209. lineLen = w - 8
  210.  
  211. do
  212. local f = io.open(filename)
  213. if f then
  214. for line in f:lines() do
  215. lineCnt = lineCnt + 1
  216. if text.trim(line) == optName then
  217. optNameStart = lineCnt + 1
  218. end
  219. if text.trim(line) == optNameEnd then
  220. optNameLast = lineCnt - 1
  221. end
  222. table.insert(tmpLine, line)
  223. end
  224. f:close()
  225. end
  226. end
  227. if lineCnt > 2 then
  228. if optNameStart < 2 then
  229. table.insert(sentStr, "No information available for "..select)
  230. end
  231. local tStr = ""
  232. for k, v in pairs(tmpLine) do
  233. if k >= optNameStart and k <= optNameLast then
  234. -- v = text.trim(v)
  235. if string.len(v) > lineLen then
  236. while string.len(v) > lineLen do
  237. tmpStr = string.sub(v, 1, lineLen)
  238. if string.len(tmpStr) < lineLen then
  239. tStr = string.sub(tmpStr, 1, string.len(tmpStr) - 1)..spaces(w - string.len(tmpStr))
  240. else
  241. delimPos = strripos(tmpStr, " ")
  242. tStr = string.sub(tmpStr, 1, delimPos - 1)..spaces((w - delimPos) + 12)
  243. end
  244. table.insert(sentStr, tStr)
  245. v = string.sub(v, delimPos + 1)
  246. end
  247. if string.len(v) < lineLen then
  248. table.insert(sentStr, v)
  249. end
  250. else
  251. table.insert(sentStr, v)
  252. end
  253. end
  254. end
  255. end
  256. end
  257.  
  258. getMenuList()
  259.  
  260. local menuDirList=gui:addListBox(2,2,24,25, menuList)
  261. local functionsList=gui:addListBox(30,2,50,25, tmpList)
  262.  
  263. local function updateMenuList()
  264. getMenuList()
  265. menuDirList:updateList(menuList)
  266. end
  267.  
  268. local function updateFunctionsList(comp)
  269. local tLen = 1
  270. local sPos = 1
  271. local sTmp = ""
  272. local tmpList = {}
  273.  
  274. contentsLabel.text="Functions for "..comp
  275. contentsLabel:draw()
  276.  
  277. for len = 1, #compList do
  278. sPos = string.find(compList[len], ".", 1, true)
  279. if (string.sub(compList[len], 1, sPos - 1) == comp) then
  280. sTmp = string.sub(compList[len], sPos + 1, string.len(compList[len]))
  281. table.insert(tmpList, sTmp)
  282. end
  283. end
  284. table.sort(tmpList)
  285. tmpList = table_unique(tmpList)
  286. functionsList:updateList(tmpList)
  287. end
  288.  
  289. function newListBox()
  290. local infoGUI=gml.create("center","center",160,50)
  291. select = menuDirList:getSelected()
  292. loadInfoData(select)
  293. term.clear()
  294. local infoLabel=infoGUI:addLabel(50,1,50,"Functions explanation for "..select)
  295. local infoList=infoGUI:addListBox(1,-5,160,42, sentStr)
  296. infoGUI:addButton(-70,-1,12,2,"Close",infoGUI.close)
  297. infoGUI:run()
  298. term.clear()
  299. intro()
  300. gui:draw()
  301. end
  302.  
  303. gui:addButton(-20,-1,8,1,"Info", newListBox)
  304. gui:addButton(-11,-1,8,1,"Reload", updateMenuList)
  305. gui:addButton(-2,-1,8,1,"Close",gui.close)
  306.  
  307. local function onMenuSelect(lb,prevIndex,selIndex)
  308. updateFunctionsList(menuDirList:getSelected())
  309. end
  310.  
  311. menuDirList.onChange=onMenuSelect
  312.  
  313. menuDirList.onDoubleClick=function()
  314. updateFunctionsList(menuDirList:getSelected())
  315. end
  316.  
  317. updateFunctionsList(menuDirList:getSelected())
  318.  
  319. gui:run()
  320. term.setCursor(1,1)
  321. gpu.setForeground(0xFFFFFF)
  322. gpu.setBackground(0x000000)
  323.  
  324. term.clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement