tuogex

gist-ui

Jul 13th, 2014
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.97 KB | None | 0 0
  1. --thanks timia2109
  2. if (not fs.exists('json.lua') or not fs.exists('txUI')) then
  3.     print("Downloading required libraries...")
  4.     if not fs.exists('json.lua') then
  5.         local code = http.get('http://regex.info/code/JSON.lua').readAll()
  6.         local file = fs.open('json.lua','w')
  7.         file.write(code)
  8.         file.close()
  9.     end
  10.     if not fs.exists('txUI') then
  11.         local code = http.get('https://raw.githubusercontent.com/tuogex/txUI/master/txUI.lua').readAll()
  12.         local file = fs.open('txUI','w')
  13.         file.write(code)
  14.         file.close()
  15.     end
  16. end
  17. os.loadAPI("txUI")
  18. JSON = (loadfile "json.lua")()
  19. local w, h = term.getSize()
  20. local contentWindow = txUI.Window:new({w = w; h = h;})
  21.  
  22. txUI.UIManager:addWindow(contentWindow)
  23. contentWindow.onView = function(self)
  24.     local infoLabel = txUI.Label:new({
  25.         y = 2;
  26.         w = w;
  27.         h = h - 1;
  28.         bgColor = colors.lightGray;
  29.         textColor = colors.gray;
  30.         text = "There's no Gist loaded :(\n\nType in a Gist ID at the top\nand press the green button to get started.";
  31.     })
  32.     local descriptionLabel = txUI.Label:new({
  33.         x = 1;
  34.         y = 8;
  35.         w = 33;
  36.         h = h - 6;
  37.         bgColor = colors.lightGray;
  38.         textColor = colors.gray;
  39.         textAlign = "left";
  40.         vertCenter = false;
  41.         text = "Loading description...";
  42.     })
  43.     local gistPanel = txUI.Panel:new({x = 18; y = 2; w = 36; h = h - 1;})
  44.     local filePanel = txUI.Panel:new({x = 18; y = 2; w = 36; h = h - 1;})
  45.     local gistIdField = txUI.TextField:new({x = 3; w = 21; h = 1; placeholder = "Gist ID"; bgColor = colors.lightGray; placeholderColor = colors.gray; textColor = colors.gray;})
  46.     local loadGist = function(self)
  47.         local gistId = gistIdField.text
  48.         infoLabel.text = "Loading..."
  49.         local loadedGist = function(decodedGistData)
  50.             contentWindow:addComponent(gistPanel)
  51.             local filesLabel = txUI.Label:new({x = 1; y = 2; h = 1; bgColor = colors.lightGray; textColor = colors.gray; text = "Files"})
  52.             local helpLabel = txUI.Label:new({x = 1; y = 4; h = 1; w = 34; bgColor = colors.lightGray; textColor = colors.gray; text = "Click on a file to see more."})
  53.             local descTitleLabel = txUI.Label:new({x = 1; y = 6; h = 1; w = 34; bgColor = colors.lightGray; textColor = colors.gray; text = "Info"})
  54.             local fileList = txUI.List:new({x = 1; y = 3; h = h - 2;});
  55.             local saveFile = function(self, fileUrl, saveAs, saveIn)
  56.                 if not fs.exists(saveAs) then
  57.                     local code = http.get(fileUrl).readAll()
  58.                     local file = fs.open(saveAs,'w')
  59.                     file.write(code)
  60.                     file.close()
  61.                     self.text = "Saved!"
  62.                     return true
  63.                 else
  64.                     self.text = "File exists!"
  65.                     return false
  66.                 end
  67.             end
  68.             local loadFile = function(self, decodedGistData, fileName)
  69.                 local fileDescriptionLabel = txUI.Label:new({x = 1; y = 4; h = h - 5; w = 32; textAlign = "left"; vertCenter = false; bgColor = colors.lightGray; textColor = colors.gray;})
  70.                 fileDescriptionLabel.text = txUI.DrawUtils:wrapText(fileName, fileDescriptionLabel.w) ..
  71.                 "\n\n" .. txUI.DrawUtils:wrapText("Type: " .. decodedGistData["files"][fileName]["type"], fileDescriptionLabel.w) ..
  72.                 "\n" .. txUI.DrawUtils:wrapText("Language: " .. (decodedGistData["files"][fileName]["language"] and decodedGistData["files"][fileName]["language"] or "unknown"), fileDescriptionLabel.w) ..
  73.                 "\n" .. txUI.DrawUtils:wrapText("Size: " .. decodedGistData["files"][fileName]["size"], fileDescriptionLabel.w)
  74.                 local saveAsField = txUI.TextField:new({x = 1; y = 2; w = 21; placeholder = "Save as"; text = shell.dir() .. fileName;})
  75.                 local saveButton = txUI.Button:new({x = 22; y = 2; w = 12; h = 1; text = "Save"; action = (function(self) saveFile(self, decodedGistData["files"][fileName]["raw_url"], saveAsField.text) end);})
  76.                 contentWindow:removeComponent(gistPanel)
  77.                 contentWindow:addComponent(filePanel)
  78.                 filePanel:addComponent(fileDescriptionLabel)
  79.                 filePanel:addComponent(saveAsField)
  80.                 filePanel:addComponent(saveButton)
  81.                 txUI.UIManager.appUpdate = function(self)
  82.                     if (not fileList.active and not saveButton.active and not saveAsField.active) then
  83.                         contentWindow:addComponent(gistPanel)
  84.                         contentWindow:removeComponent(filePanel)
  85.                         filePanel.components = {}
  86.                         self.appUpdate = function(self) end
  87.                     end
  88.                 end
  89.             end
  90.             contentWindow:addComponent(filesLabel)
  91.             for k, v in pairs(decodedGistData["files"]) do
  92.                 fileList:addComponent(txUI.Button:new({text = k; action = (function(self) loadFile(self, decodedGistData, k) end);}))
  93.             end
  94.             infoLabel.text = ""
  95.             local saveInField = txUI.TextField:new({x = 1; y = 2; w = 21; placeholder = "Save in"; text = shell.dir();})
  96.             gistPanel:addComponent(saveInField)
  97.             gistPanel:addComponent(txUI.Button:new({x = 22; y = 2; w = 12; h = 1; text = "Save all"; action = (function(self)
  98.                 for k, v in pairs(decodedGistData["files"]) do
  99.                     if (not fs.exists(saveInField.text)) then
  100.                         fs.makeDir(saveInField.text)
  101.                     end
  102.                     local numFailed = 0
  103.                     if (not saveFile(self, v["raw_url"], saveInField.text .. "/" .. k)) then
  104.                         numFailed = numFailed + 1
  105.                     end
  106.                     self.text = (numFailed == 0 and "Saved!" or numFailed .. " exists!")
  107.                 end
  108.             end);}))
  109.             gistPanel:addComponent(descriptionLabel);
  110.             contentWindow:addComponent(fileList)
  111.             gistPanel:addComponent(helpLabel)
  112.             gistPanel:addComponent(descTitleLabel)
  113.             descriptionLabel.text = txUI.DrawUtils:wrapText("Description: " .. decodedGistData["description"], descriptionLabel.w) ..
  114.             "\n" .. txUI.DrawUtils:wrapText("Created by: " .. decodedGistData["owner"]["login"], descriptionLabel.w) ..
  115.             "\n" .. txUI.DrawUtils:wrapText("Updated at: " .. decodedGistData["updated_at"], descriptionLabel.w)
  116.         end
  117.         --get gist data and decode
  118.         txUI.UIManager.appUpdate = function(self)
  119.             os.queueEvent("dummy", "1") -- force an update cycle
  120.             self.appUpdate = function(self)
  121.                 self.appUpdate = function(self) end
  122.                 local gistDataRequest = http.get("https://api.github.com/gists/" .. gistId)
  123.                 if (gistDataRequest == nil) then
  124.                     infoLabel.text = "There's no data for this Gist ID.\nAre you sure the ID is correct?"
  125.                     os.queueEvent("dummy", "1") -- force an update cycle
  126.                     return
  127.                 end
  128.                 local gistData = gistDataRequest.readAll()
  129.                 local decodedGistData = JSON:decode(gistData)
  130.                 loadedGist(decodedGistData)
  131.             end
  132.         end
  133.     end
  134.     local goButton = txUI.Button:new({x = 24; w = 3; h = 1; text = ">"; bgColor = colors.lime; activeColor = colors.green; action = loadGist;})
  135.     gistIdField.action = function(self)
  136.         loadGist(goButton)
  137.     end
  138.     self:setTitleLabel(txUI.Label:new({text = "Gist"; bgColor = self.tlColor; textColor = colors.white; w = self.w; x = self.x; textAlign = "right";}))
  139.     self:addComponent(txUI.Button:new({x = 1; y = 1; w = 1; h = 1; action = (function(self) txUI.UIManager:terminate() end); textColor = colors.red; bgColor = self.tlColor; text = "x";}))
  140.     self:addComponent(gistIdField)
  141.     self:addComponent(goButton)
  142.     self:addComponent(infoLabel)
  143. end
  144. contentWindow.onHide = function(self)
  145.     self.components = {}
  146.     self.titleLabel = {}
  147. end
  148. txUI.UIManager:setVisibleWindow(contentWindow)
  149.  
  150. txUI.UIManager:startUpdateCycle()
Advertisement
Add Comment
Please, Sign In to add comment