Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --thanks timia2109
- if (not fs.exists('json.lua') or not fs.exists('txUI')) then
- print("Downloading required libraries...")
- if not fs.exists('json.lua') then
- local code = http.get('http://regex.info/code/JSON.lua').readAll()
- local file = fs.open('json.lua','w')
- file.write(code)
- file.close()
- end
- if not fs.exists('txUI') then
- local code = http.get('https://raw.githubusercontent.com/tuogex/txUI/master/txUI.lua').readAll()
- local file = fs.open('txUI','w')
- file.write(code)
- file.close()
- end
- end
- os.loadAPI("txUI")
- JSON = (loadfile "json.lua")()
- local w, h = term.getSize()
- local contentWindow = txUI.Window:new({w = w; h = h;})
- txUI.UIManager:addWindow(contentWindow)
- contentWindow.onView = function(self)
- local infoLabel = txUI.Label:new({
- y = 2;
- w = w;
- h = h - 1;
- bgColor = colors.lightGray;
- textColor = colors.gray;
- text = "There's no Gist loaded :(\n\nType in a Gist ID at the top\nand press the green button to get started.";
- })
- local descriptionLabel = txUI.Label:new({
- x = 1;
- y = 8;
- w = 33;
- h = h - 6;
- bgColor = colors.lightGray;
- textColor = colors.gray;
- textAlign = "left";
- vertCenter = false;
- text = "Loading description...";
- })
- local gistPanel = txUI.Panel:new({x = 18; y = 2; w = 36; h = h - 1;})
- local filePanel = txUI.Panel:new({x = 18; y = 2; w = 36; h = h - 1;})
- local gistIdField = txUI.TextField:new({x = 3; w = 21; h = 1; placeholder = "Gist ID"; bgColor = colors.lightGray; placeholderColor = colors.gray; textColor = colors.gray;})
- local loadGist = function(self)
- local gistId = gistIdField.text
- infoLabel.text = "Loading..."
- local loadedGist = function(decodedGistData)
- contentWindow:addComponent(gistPanel)
- local filesLabel = txUI.Label:new({x = 1; y = 2; h = 1; bgColor = colors.lightGray; textColor = colors.gray; text = "Files"})
- 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."})
- local descTitleLabel = txUI.Label:new({x = 1; y = 6; h = 1; w = 34; bgColor = colors.lightGray; textColor = colors.gray; text = "Info"})
- local fileList = txUI.List:new({x = 1; y = 3; h = h - 2;});
- local saveFile = function(self, fileUrl, saveAs, saveIn)
- if not fs.exists(saveAs) then
- local code = http.get(fileUrl).readAll()
- local file = fs.open(saveAs,'w')
- file.write(code)
- file.close()
- self.text = "Saved!"
- return true
- else
- self.text = "File exists!"
- return false
- end
- end
- local loadFile = function(self, decodedGistData, fileName)
- local fileDescriptionLabel = txUI.Label:new({x = 1; y = 4; h = h - 5; w = 32; textAlign = "left"; vertCenter = false; bgColor = colors.lightGray; textColor = colors.gray;})
- fileDescriptionLabel.text = txUI.DrawUtils:wrapText(fileName, fileDescriptionLabel.w) ..
- "\n\n" .. txUI.DrawUtils:wrapText("Type: " .. decodedGistData["files"][fileName]["type"], fileDescriptionLabel.w) ..
- "\n" .. txUI.DrawUtils:wrapText("Language: " .. (decodedGistData["files"][fileName]["language"] and decodedGistData["files"][fileName]["language"] or "unknown"), fileDescriptionLabel.w) ..
- "\n" .. txUI.DrawUtils:wrapText("Size: " .. decodedGistData["files"][fileName]["size"], fileDescriptionLabel.w)
- local saveAsField = txUI.TextField:new({x = 1; y = 2; w = 21; placeholder = "Save as"; text = shell.dir() .. fileName;})
- 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);})
- contentWindow:removeComponent(gistPanel)
- contentWindow:addComponent(filePanel)
- filePanel:addComponent(fileDescriptionLabel)
- filePanel:addComponent(saveAsField)
- filePanel:addComponent(saveButton)
- txUI.UIManager.appUpdate = function(self)
- if (not fileList.active and not saveButton.active and not saveAsField.active) then
- contentWindow:addComponent(gistPanel)
- contentWindow:removeComponent(filePanel)
- filePanel.components = {}
- self.appUpdate = function(self) end
- end
- end
- end
- contentWindow:addComponent(filesLabel)
- for k, v in pairs(decodedGistData["files"]) do
- fileList:addComponent(txUI.Button:new({text = k; action = (function(self) loadFile(self, decodedGistData, k) end);}))
- end
- infoLabel.text = ""
- local saveInField = txUI.TextField:new({x = 1; y = 2; w = 21; placeholder = "Save in"; text = shell.dir();})
- gistPanel:addComponent(saveInField)
- gistPanel:addComponent(txUI.Button:new({x = 22; y = 2; w = 12; h = 1; text = "Save all"; action = (function(self)
- for k, v in pairs(decodedGistData["files"]) do
- if (not fs.exists(saveInField.text)) then
- fs.makeDir(saveInField.text)
- end
- local numFailed = 0
- if (not saveFile(self, v["raw_url"], saveInField.text .. "/" .. k)) then
- numFailed = numFailed + 1
- end
- self.text = (numFailed == 0 and "Saved!" or numFailed .. " exists!")
- end
- end);}))
- gistPanel:addComponent(descriptionLabel);
- contentWindow:addComponent(fileList)
- gistPanel:addComponent(helpLabel)
- gistPanel:addComponent(descTitleLabel)
- descriptionLabel.text = txUI.DrawUtils:wrapText("Description: " .. decodedGistData["description"], descriptionLabel.w) ..
- "\n" .. txUI.DrawUtils:wrapText("Created by: " .. decodedGistData["owner"]["login"], descriptionLabel.w) ..
- "\n" .. txUI.DrawUtils:wrapText("Updated at: " .. decodedGistData["updated_at"], descriptionLabel.w)
- end
- --get gist data and decode
- txUI.UIManager.appUpdate = function(self)
- os.queueEvent("dummy", "1") -- force an update cycle
- self.appUpdate = function(self)
- self.appUpdate = function(self) end
- local gistDataRequest = http.get("https://api.github.com/gists/" .. gistId)
- if (gistDataRequest == nil) then
- infoLabel.text = "There's no data for this Gist ID.\nAre you sure the ID is correct?"
- os.queueEvent("dummy", "1") -- force an update cycle
- return
- end
- local gistData = gistDataRequest.readAll()
- local decodedGistData = JSON:decode(gistData)
- loadedGist(decodedGistData)
- end
- end
- end
- local goButton = txUI.Button:new({x = 24; w = 3; h = 1; text = ">"; bgColor = colors.lime; activeColor = colors.green; action = loadGist;})
- gistIdField.action = function(self)
- loadGist(goButton)
- end
- self:setTitleLabel(txUI.Label:new({text = "Gist"; bgColor = self.tlColor; textColor = colors.white; w = self.w; x = self.x; textAlign = "right";}))
- 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";}))
- self:addComponent(gistIdField)
- self:addComponent(goButton)
- self:addComponent(infoLabel)
- end
- contentWindow.onHide = function(self)
- self.components = {}
- self.titleLabel = {}
- end
- txUI.UIManager:setVisibleWindow(contentWindow)
- txUI.UIManager:startUpdateCycle()
Advertisement
Add Comment
Please, Sign In to add comment