This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
CrazedProgrammer's profile picture

File Dialog API

Started by CrazedProgrammer, 28 April 2015 - 07:57 PM
CrazedProgrammer #1
Posted 28 April 2015 - 09:57 PM
This is a simple file dialog API.
You can use this when you want the user to select a file (load file, save file).
It works on all devices that have a color display.
It is easy to use and it only has one function: fdialog.open(dir)
The dir argument is optional and specifies in what directory the dialog is.
The function returns the absolute path to the file.
If the user cancels, it returns nil.
There is a gray bar at the bottom and when you click on it you can put in a file manually.
That means that you can select a file that doesn’t already exist.
You can also put in an absolute path beginning with “/”.
When you put in a folder, the dialog jumps to the folder.

Download:
pastebin get 2GV4JgZR fdialog
You can simply load it using os.loadAPI("fdialog").

Screenshots:

Edited on 12 June 2015 - 01:13 PM
Bomb Bloke #2
Posted 29 April 2015 - 01:02 AM
Oy, why are all your folders listed at the bottom? :P/>
CrazedProgrammer #3
Posted 29 April 2015 - 06:42 AM
Oy, why are all your folders listed at the bottom? :P/>/>
Because that's how fs.list works apparently :P/>
Bomb Bloke #4
Posted 29 April 2015 - 07:36 AM
That's no good reason! :P

Truth be told, fs.list() is filesystem-dependant. You'll get different results under different platforms.

For what it's worth, this is the sort of code I've used to produce my listings:

local displayList = {}

if #shell.resolve(".") > 0 then displayList[1] = ".." end

do
	local fullList = fs.list(shell.resolve("."))
	table.sort(fullList, function (a, b) return string.lower(a) < string.lower(b) end)
	for i = 1, #fullList do if fs.isDir(shell.resolve(fullList[i])) then displayList[#displayList + 1] = fullList[i] end end
	for i = 1, #fullList do if not fs.isDir(shell.resolve(fullList[i])) then displayList[#displayList + 1] = fullList[i] end end
end
Edited on 29 April 2015 - 05:36 AM
CrazedProgrammer #5
Posted 29 April 2015 - 06:05 PM
That's no good reason! :P/>

Truth be told, fs.list() is filesystem-dependant. You'll get different results under different platforms.

For what it's worth, this is the sort of code I've used to produce my listings:

local displayList = {}

if #shell.resolve(".") > 0 then displayList[1] = ".." end

do
	local fullList = fs.list(shell.resolve("."))
	table.sort(fullList, function (a, B)/> return string.lower(a) < string.lower(B)/> end)
	for i = 1, #fullList do if fs.isDir(shell.resolve(fullList[i])) then displayList[#displayList + 1] = fullList[i] end end
	for i = 1, #fullList do if not fs.isDir(shell.resolve(fullList[i])) then displayList[#displayList + 1] = fullList[i] end end
end
Thanks, I've updated the File Dialog API to correctly show files and folders.
You can download it from the same pastebin as before.
ebernerd #6
Posted 21 May 2015 - 03:18 AM
Can I use this in my projects? I will obviously give you credit, because this is actually really awesome. It would be cool if you could make it fit into the ComputerCraft "Window API" thing, so you could have a dialog window, not a screen.

Either way, this is pure genius, and I would love to be able to use it.
CrazedProgrammer #7
Posted 21 May 2015 - 07:34 AM
Can I use this in my projects? I will obviously give you credit, because this is actually really awesome. It would be cool if you could make it fit into the ComputerCraft "Window API" thing, so you could have a dialog window, not a screen.

Either way, this is pure genius, and I would love to be able to use it.
Thanks!
Of course you can use it! :D/>

It wouldn't really work with the window API because then you would have to change inputs and other things.
On top of that, it hangs the entire program until it returns.
Maybe I'll make a GUI API so that you can have a small file dialog.