Advertisement
Guest User

herpderp

a guest
Mar 31st, 2014
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.63 KB | None | 0 0
  1. --[[
  2. save/load dialog gui
  3.  
  4. --]]
  5. package.loaded["gml"]=nil
  6. local term=require("term")
  7. local event=require("event")
  8. local component=require("component")
  9. local gml=require("gml")
  10. local shell=require("shell")
  11. local filesystem=require("filesystem")
  12.  
  13.  
  14.  
  15. local gui=gml.create("center","center",50,16)
  16.  
  17. gui:addLabel(1,1,14,"Common Folders")
  18. local commonDirList=gui:addListBox(1,2,16,11,{"/","/usr/","/tmp/","/lib/","/bin/"})
  19.  
  20. local contentsLabel=gui:addLabel(18,1,31,"contents of /")
  21. local directoryList=gui:addListBox(17,2,32,11,{})
  22.  
  23. local function test()
  24.   --need to set colors to desired
  25.   component.gpu.setForeground(0xffffff)
  26.   component.gpu.setBackground(0x000000)
  27.   term.clear()
  28.   print("hello, world!")
  29.   --could draw whatever, have an event loop, anything right here
  30.   event.pull("key_down")
  31.   --redraw gui. Might wanna clear the screen again first?
  32.   gui:draw()
  33. end
  34.  
  35. gui:addTextField(22,-2,26)
  36. gui:addLabel(17,-2,5,"File:")
  37. gui:addButton(-2,-1,8,1,"Open",gui.close)
  38. gui:addButton(-11,-1,8,1,"Cancel",test)
  39.  
  40. local curDirPath="/"
  41.  
  42. local function updateDirectoryList(dir)
  43.   local list={}
  44.   if dir~="/" then
  45.     list[1]=".."
  46.   end
  47.   local nextDir=#list+1
  48.   for file in filesystem.list(dir) do
  49.     if filesystem.isDirectory(file) then
  50.       if file:sub(-1)~="/" then
  51.         file=file.."/"
  52.       end
  53.       table.insert(list,nextDir,file)
  54.       nextDir=nextDir+1
  55.     else
  56.       table.insert(list,file)
  57.     end
  58.   end
  59.   curDirPath=dir
  60.   directoryList:updateList(list)
  61.   contentsLabel.text="contents of "..curDirPath
  62.   contentsLabel:draw()
  63. end
  64.  
  65. local function onDirSelect(lb,prevIndex,selIndex)
  66.   updateDirectoryList(commonDirList:getSelected())
  67. end
  68.  
  69. commonDirList.onChange=onDirSelect
  70. directoryList.onDoubleClick=function()
  71.   local selected=directoryList:getSelected()
  72.   if selected==".." then
  73.     selected=curDirPath:match("^(.*/)[^/]*/")
  74.   else
  75.     selected=curDirPath..selected
  76.   end
  77.   if filesystem.isDirectory(selected) then
  78.     updateDirectoryList(selected)
  79.   end
  80. end
  81.  
  82. updateDirectoryList("/")
  83.  
  84. gui:run()
  85.  
  86. --[[
  87. ultimaely will integrate this into gml.lua and boil down to:
  88.  
  89. filename = gml.filePicker(<"save" or "load"> [, startPath [, extensions ] ])
  90.  
  91. startPath will determine waht folder the file list view starts on
  92. if startPath is a file, rather than a folder, will start in the containing folder
  93. and select that file, populating the textfield with it.
  94.  
  95. extensions, if specified, can be a string or list of strings, and the file list
  96. will show only files matching that extension. For save, the extension will also be
  97. automatically applied to the name, if it does not end with it already.
  98.  
  99.  
  100.  
  101.  
  102. --]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement