Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2014
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.22 KB | None | 0 0
  1. local component = require("component")
  2. local fs = require("filesystem")
  3. local internet = require("internet")
  4. local process = require("process")
  5. local event = require("event")
  6. local keyboard = require("keyboard")
  7. local shell = require("shell")
  8. local term = require("term")
  9. local text = require("text")
  10. local unicode = require("unicode")
  11. local sides = require("sides")
  12. local colors=require("colors")
  13. local gml=require("gml")
  14.  
  15. -- The following code added by Michiyo, to check if:
  16. -- A.) The computer has HTTP access, and
  17. -- B.) if the required files exist to run this program.
  18. -- C.) And to download them if it can.
  19. --#~#~#~#~#~#~#~ CHANGE THE FOLLOWING VARIABLE TO FALSE TO DISABLE AUTOUPDATE CHECKING~#~#~#~#~#~#~#
  20. autoUpdate = true
  21.  
  22. function file_check(file_name)
  23. local file = fs.open(file_name)
  24. if file ~= nil then
  25. return true
  26. else
  27. return false
  28. end
  29. end
  30. wget = loadfile("/bin/wget.lua")
  31. function downloadFile(remotename, filename)
  32. wget("http://pc-logix.com/https_proxy.php?address=https://raw.githubusercontent.com/OpenPrograms/Kenny-Programs/master/CompViewer/" .. remotename, filename)
  33. end
  34.  
  35. function remoteVersion()
  36. for line in internet.request("http://pc-logix.com/https_proxy.php?address=https://raw.githubusercontent.com/OpenPrograms/Kenny-Programs/master/CompViewer/compviewer-version.txt") do
  37. if tonumber(line) ~= nil then
  38. return tonumber(line)
  39. else
  40. return 0
  41. end
  42. end
  43. end
  44.  
  45. function localVersion()
  46. if not file_check(os.getenv("PWD") .. "compviewer-version.txt") then
  47. return 0
  48. else
  49. local f = io.open(os.getenv("PWD") .. "compviewer-version.txt", "rb")
  50. local content = f:read("*all")
  51. f:close()
  52. return tonumber(content)
  53. end
  54. end
  55.  
  56. function doUpdate(watdo)
  57. if (watdo == "update") then
  58. if autoUpdate == true then
  59. print("Cleaning up previous install")
  60. fs.remove(os.getenv("PWD") .. "default.gss")
  61. fs.remove(os.getenv("PWD") .. "gml.lua")
  62. fs.remove(os.getenv("PWD") .. "gfxbuffer.lua")
  63. fs.remove(os.getenv("PWD") .. "colorutils.lua")
  64. fs.remove(os.getenv("PWD") .. "colorutils.lua")
  65. fs.remove(os.getenv("PWD") .. "compviewer-version.txt")
  66. currFile = process.running()
  67. fs.remove(currFile)
  68. if not file_check(os.getenv("PWD") .. currFile) then
  69. print("Downloading CompViewer.lua")
  70. downloadFile("CompViewer.lua", currFile)
  71. end
  72. end
  73. end
  74. print("Downloading latest versions of required files")
  75. if not file_check(os.getenv("PWD") .. "default.gss") then
  76. print("Downloading default.gss")
  77. downloadFile("default.gss","default.gss")
  78. end
  79. if not file_check(os.getenv("PWD") .. "gml.lua") then
  80. print("Downloading gml.lua")
  81. downloadFile("gml.lua","gml.lua")
  82. end
  83. if not file_check(os.getenv("PWD") .. "gfxbuffer.lua") then
  84. print("Downloading gfxbuffer.lua")
  85. downloadFile("gfxbuffer.lua","gfxbuffer.lua")
  86. end
  87. if not file_check(os.getenv("PWD") .. "colorutils.lua") then
  88. print("Downloading colorutils.lua")
  89. downloadFile("colorutils.lua","colorutils.lua")
  90. end
  91. if not file_check(os.getenv("PWD") .. "compviewer-version.txt") then
  92. print("Downloading compviewer-version.txt")
  93. downloadFile("compviewer-version.txt","compviewer-version.txt")
  94. end
  95. end
  96.  
  97. if not component.isAvailable("internet") then
  98. if not file_check(os.getenv("PWD") .. "gml.lua") or not file_check(os.getenv("PWD") .. "default.gss") or not file_check(os.getenv("PWD") .. "gfxbuffer.lua") or not file_check(os.getenv("PWD") .. "colorutils.lua") or not file_check(os.getenv("PWD") .. "CompInfo.txt") then
  99. io.stderr:write("You are missing one or more of the required files 'gml.lua', 'colorutils.lua', 'gfxbuffer.lua', 'CompInfo.txt', or 'default.gss' and do not have internet access to download them automaticly!\n")
  100. return
  101. end
  102. else
  103. remoteVer = remoteVersion()
  104. localVer = localVersion()
  105. if not file_check(os.getenv("PWD") .. "compviewer-version.txt") then
  106. print("Setting up version cache")
  107. downloadFile("compviewer-version.txt","compviewer-version.txt")
  108. end
  109.  
  110. if(remoteVer > localVer) then
  111. if(localVer == 0) then
  112. doUpdate("fresh")
  113. else
  114. doUpdate("update")
  115. end
  116. end
  117.  
  118. print("Updating Component Info file, One moment plaease")
  119. fs.remove(os.getenv("PWD") .. "CompInfo.txt")
  120. os.sleep(0.5)
  121. downloadFile("CompInfo.txt")
  122. end
  123. --We now return you to the previous code by Kenny.
  124.  
  125.  
  126. local function spaces(cnt)
  127. return string.rep(string.char(32), cnt)
  128. end
  129.  
  130. local function spChar(letter, cnt)
  131. return string.rep(unicode.char(letter), cnt)
  132. end
  133.  
  134. 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).." "
  135. 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).." "
  136. 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).." "
  137. 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).." "
  138. 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).." "
  139.  
  140. local gpu = component.gpu
  141. local Tier1 = 1
  142. local Tier2 = 4
  143. local Tier3 = 8
  144.  
  145. local menuList = {}
  146.  
  147. local compList = {}
  148. local tmpList = {}
  149. local sentStr = {}
  150. local compLen = 1
  151.  
  152. local fname = "CompInfo.txt"
  153. local filename = shell.resolve(fname)
  154.  
  155. local w, h = gpu.getResolution()
  156.  
  157. local guiRow = 1
  158. local guiWidth = 1
  159. local guiHeight = 1
  160. local guiContentsLabelCol = 1
  161. local guiContentsLabelWidth = 1
  162. local menuDirWidth = 1
  163. local menuDirHeight = 1
  164. local functionsCol = 1
  165. local functionsWidth = 1
  166. local functionsHeight = 1
  167. local infoGuiWidth = 1
  168. local infoGuiHeight = 1
  169. local infoFunctionsLabelWidth = 1
  170. local infoListboxWidth = 1
  171. local infoListboxHeight = 1
  172.  
  173. local function table_count(tt, item)
  174. local count
  175. count = 0
  176. for ii,xx in pairs(tt) do
  177. if item == xx then count = count + 1 end
  178. end
  179. return count
  180. end
  181.  
  182. local function table_unique(tt)
  183. local newtable = {}
  184. for ii,xx in ipairs(tt) do
  185. if table_count(newtable, xx) == 0 then
  186. newtable[#newtable+1] = xx
  187. end
  188. end
  189. return newtable
  190. end
  191.  
  192. local function isAdvanced()
  193. return gpu.getDepth()
  194. end
  195.  
  196. local function setColors(fore, back)
  197. gpu.setForeground(fore)
  198. gpu.setBackground(back)
  199. end
  200.  
  201. local function setCursor(col, row)
  202. term.setCursor(col, row)
  203. end
  204.  
  205. local function getSize()
  206. return gpu.getResolution()
  207. end
  208.  
  209. local function drawBox(col, row, wid, hgt, fore, back, opt)
  210. local ul = {0x250C, 0x2554}
  211. local ur = {0x2510, 0x2557}
  212. local ll = {0x2514, 0x255A}
  213. local lr = {0x2518, 0x255D}
  214. local sl = {0x2502, 0x2551}
  215. local al = {0x2500, 0x2550}
  216. setColors(fore, back)
  217. gpu.set(col, row, unicode.char(ul[opt])..spChar(al[opt], wid - 2)..unicode.char(ur[opt]))
  218. for a = 1, hgt - 2 do
  219. gpu.set(col, row + a, unicode.char(sl[opt])..spaces(wid - 2)..unicode.char(sl[opt]))
  220. end
  221. gpu.set(col, row + hgt - 2, unicode.char(ll[opt])..spChar(al[opt], wid - 2)..unicode.char(lr[opt]))
  222. setCursor(col, row)
  223. end
  224.  
  225. local function printXY(col, row, menuSel)
  226. gpu.set(col, row, menuSel)
  227. end
  228.  
  229. local function printDocXY(col, row, menuSel)
  230. setCursor(col, row)
  231. print(menuSel)
  232. end
  233.  
  234.  
  235. local function centerText(row, msg)
  236. local w, h = getSize()
  237. local len = string.len(msg)
  238. gpu.set((w - len)/2, row, msg)
  239. end
  240.  
  241. local function centerIntroText(row, msg)
  242. local msg1 = " * * * * * ** * * * * * * * * * * * * * * * * "
  243. local w, h = getSize()
  244. local len = string.len(msg1)
  245. gpu.set((w - len)/2, row, msg)
  246. end
  247.  
  248.  
  249. local defaultTheme = { -- Water Theme
  250. textColor = 0xFFFFFF,
  251. background = 0x000099,
  252. introText = 0xFF0000,
  253. introBackground = 0x000000,
  254. menuHintText = 0xFFFF00,
  255. menuHint = 0x000000,
  256. prompt = 0xBBBB00,
  257. promptHighlight = 0x000000,
  258. fancyDots = 0xAAFFCC,
  259. fancyBackground = 0x113322
  260. }
  261.  
  262. local normalTheme = { -- Water Theme
  263. textColor = 0xFFFFFF,
  264. background = 0x000000,
  265. prompt = 0x000000,
  266. promptHighlight = 0xFFFFFF,
  267. }
  268.  
  269. term.clear()
  270. local w, h = gpu.getResolution()
  271.  
  272. local function intro()
  273. local w, h = gpu.getResolution()
  274. local msg1 = " * * * * * ** * * * * * * * * * * * * * * * * "
  275. local len = string.len(msg1) + 1
  276.  
  277. drawBox(1, 3, w, h - 3, theme.textColor, theme.background, 2)
  278. drawBox((w - len)/2 - 2, 5, len + 4, 10, theme.introText, theme.introBackground, 1)
  279.  
  280. centerIntroText(6, OC_1)
  281. centerIntroText(7, OC_2)
  282. centerIntroText(8, OC_3)
  283. centerIntroText(9, OC_4)
  284. centerIntroText(10, OC_5)
  285. centerText(12, "Component Viewer")
  286. end
  287.  
  288. if (gpu.getDepth() == Tier3) then
  289. guiRow = 15
  290. guiWidth = 80
  291. guiHeight = 25
  292. guiContentsLabelCol = 30
  293. guiContentsLabelWidth = 31
  294. menuDirWidth = 24
  295. menuDirHeight = 25
  296. functionsCol = 30
  297. functionsWidth = 50
  298. functionsHeight = 25
  299. infoGuiWidth = 160
  300. infoGuiHeight = 50
  301. infoFunctionsLabelWidth = 50
  302. infoListboxWidth = 1600
  303. infoListboxHeight = 42
  304. theme = defaultTheme
  305. intro()
  306. else
  307. guiRow = (h - 50) / 2
  308. guiWidth = 50
  309. guiHeight = 16
  310. guiContentsLabelCol = 0
  311. guiContentsLabelWidth = 0
  312. menuDirWidth = 0
  313. menuDirHeight = 0
  314. functionsCol = 0
  315. functionsWidth = 0
  316. functionsHeight = 0
  317. infoGuiWidth = 0
  318. infoGuiHeight = 0
  319. infoFunctionsLabelWidth = 0
  320. infoListboxWidth = 0
  321. infoListboxHeight = 0
  322. theme = normalTheme
  323. end
  324.  
  325. local function strripos(s, delim)
  326. return s:match('^.*()'..delim)
  327. end
  328.  
  329. local gui=gml.create("center", guiRow, guiWidth, guiCol)
  330. gui.style=gml.loadStyle("cv.gss")
  331.  
  332. gui:addLabel(2,1,14,"Component")
  333. local contentsLabel=gui:addLabel(guiContentsLabelCol,1, guiContentsLabelWidth, "contents of")
  334.  
  335.  
  336. local function getMenuList()
  337. menuList = {}
  338. local tmpName = ""
  339. for address, name in component.list() do
  340. table.insert(menuList,name)
  341. for k, v in pairs(component.proxy(address)) do
  342. tmpName = name.."."..k
  343. table.insert(compList,tmpName)
  344. end
  345. end
  346. table.sort(menuList)
  347. end
  348.  
  349. function loadInfoData(select)
  350. local lineLen, lineHeight = 0, 0
  351. local lineCnt = 0
  352. local optName = "["..string.upper(select).."]"
  353. local optNameEnd = "["..string.upper(select).."_END]"
  354. local optNameStart, optNameLast = 0,0
  355. local w, h = gpu.getResolution()
  356. local tmpLine = {}
  357. local tmpStr = ""
  358. sentStr = {}
  359. lineLen = w - 8
  360.  
  361. do
  362. local f = io.open(filename)
  363. if f then
  364. for line in f:lines() do
  365. lineCnt = lineCnt + 1
  366. if text.trim(line) == optName then
  367. optNameStart = lineCnt + 1
  368. end
  369. if text.trim(line) == optNameEnd then
  370. optNameLast = lineCnt - 1
  371. end
  372. table.insert(tmpLine, line)
  373. end
  374. f:close()
  375. end
  376. end
  377. if lineCnt > 2 then
  378. if optNameStart < 2 then
  379. table.insert(sentStr, "No information available for "..select)
  380. end
  381. local tStr = ""
  382. for k, v in pairs(tmpLine) do
  383. if k >= optNameStart and k <= optNameLast then
  384. v = text.trim(v)
  385. if string.len(v) > lineLen then
  386. while string.len(v) > lineLen do
  387. v = spaces(6)..v
  388. tmpStr = string.sub(v, 1, lineLen)
  389. if string.len(tmpStr) < lineLen then
  390. tStr = string.sub(tmpStr, 1, string.len(tmpStr) - 1)
  391. else
  392. delimPos = strripos(tmpStr, " ")
  393. tStr = string.sub(tmpStr, 1, delimPos - 1)
  394. end
  395. table.insert(sentStr, tStr)
  396. v = string.sub(v, delimPos + 1)
  397. end
  398. if string.len(v) < lineLen then
  399. table.insert(sentStr, spaces(6)..text.trim(v))
  400. end
  401. elseif string.sub(v, 1, string.len(select)) == select then
  402. table.insert(sentStr, v)
  403. else
  404. table.insert(sentStr, spaces(6)..v)
  405. end
  406. end
  407. end
  408. end
  409. end
  410.  
  411. getMenuList()
  412.  
  413. local menuDirList=gui:addListBox(2, 2, menuDirWidth, menuDirHeight, menuList)
  414. local functionsList=gui:addListBox(functionsCol, 2, functionsWidth, functionsHeight, tmpList)
  415.  
  416. local function updateMenuList()
  417. getMenuList()
  418. menuDirList:updateList(menuList)
  419. end
  420.  
  421. local function updateFunctionsList(comp)
  422. local tLen = 1
  423. local sPos = 1
  424. local sTmp = ""
  425. local tmpList = {}
  426.  
  427. contentsLabel.text="Functions for "..comp
  428. contentsLabel:draw()
  429.  
  430. for len = 1, #compList do
  431. sPos = string.find(compList[len], ".", 1, true)
  432. if (string.sub(compList[len], 1, sPos - 1) == comp) then
  433. sTmp = string.sub(compList[len], sPos + 1, string.len(compList[len]))
  434. table.insert(tmpList, sTmp)
  435. end
  436. end
  437. table.sort(tmpList)
  438. tmpList = table_unique(tmpList)
  439. functionsList:updateList(tmpList)
  440. end
  441.  
  442. function newListBox()
  443. local infoGUI=gml.create("center", "center", infoGuiWidth, infoGuiHeight)
  444. select = menuDirList:getSelected()
  445. loadInfoData(select)
  446. term.clear()
  447. local infoLabel=infoGUI:addLabel("center", 1, infoFunctionsLabelWidth, "Functions explanation for "..select)
  448. local infoList=infoGUI:addListBox(1, -5, infoListboxWidth, infoListboxHeight, sentStr)
  449. infoGUI:addButton("center",-1,12,2,"Close",infoGUI.close)
  450. infoGUI:run()
  451. term.clear()
  452. intro()
  453. gui:draw()
  454. end
  455.  
  456. gui:addButton(-20,-1,8,1,"Info", newListBox)
  457. gui:addButton(-11,-1,8,1,"Reload", updateMenuList)
  458. gui:addButton(-2,-1,8,1,"Close",gui.close)
  459.  
  460. local function onMenuSelect(lb,prevIndex,selIndex)
  461. updateFunctionsList(menuDirList:getSelected())
  462. end
  463.  
  464. menuDirList.onChange=onMenuSelect
  465.  
  466. menuDirList.onDoubleClick=function()
  467. updateFunctionsList(menuDirList:getSelected())
  468. end
  469.  
  470. updateFunctionsList(menuDirList:getSelected())
  471.  
  472. gui:run()
  473. term.setCursor(1,1)
  474. gpu.setForeground(0xFFFFFF)
  475. gpu.setBackground(0x000000)
  476.  
  477. term.clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement