43 posts
Location
Spain (or Hisperia)
Posted 07 July 2013 - 02:05 PM
Hello, I've been recently busy with a file browser for my
Graphical Operating Interface, in which I have also made some significant progress, when I found that I was unable, or rather could not figure out, how to ascend one level of folders. You see, I work with a variable named
route which I get from clicking in one file icon and which includes the whole route of it. If it is a file, it runs it (by now), and if it is a folder, it opens it. However, I find that I only know one way of going back to the previous folder, which is having the user type it in the route bar. I would extremely appreciate a way to recognise the last slash in the route and separate the two strings which lay at each of the sides, taking only the first as the new route. Thanks in advance!
8543 posts
Posted 07 July 2013 - 02:08 PM
path, end = string.match(currentPath, "(.+)/([^/]+)$")
43 posts
Location
Spain (or Hisperia)
Posted 08 July 2013 - 12:15 PM
path, end = string.match(currentPath, "(.+)/([^/]+)$")
Thank you very much! I have to indicate, though, that there has to be an if statement that converts the nil variable that results into an empty string for this to work correctly. Otherwise, thank you very much!
Also, could anyone post a link to any good patterns tutorial? I've already had some issues because of my absolute ignorance on how to use them.
8543 posts
Posted 08 July 2013 - 12:39 PM
if path == nil then path = "" end
Check out the Lua Reference Manual for 5.1, look at the Patterns subsection of the String Manipulation section near the end.
808 posts
Posted 08 July 2013 - 12:40 PM
path, end = string.match(currentPath, "(.+)/([^/]+)$")
This doesn't handle paths that escape out the forward slash, keeping it in the name. But then again I guess CraftOS generally doesn't handle that stuff.
8543 posts
Posted 08 July 2013 - 12:45 PM
It also doesn't handle paths that use backslash instead, which ComputerCraft will consume fine, but never produces. It's simple, but covers 99% of actual usage in CC, I'd wager. Shenanigans with fs.getName and other string manipulations might handle the remaining cases, but that seems unnecessary for an internal-use function for a file browser.