#absolute to relative path

28 messages · Page 1 of 1 (latest)

thick cosmos
#

I have a UI that shows a path, but i don't want it to show the full path (just relative).
then when they click it, it will open it in the explorer.

i tried to do this a while ago but it didn't work.
it would be nice to do it on the frontend

astral pond
#

Which part are you asking for help with? What exactly is the issue? What have you tried? What "didn't work?"

thick cosmos
#

the problem: i can't find a way to turn an absolute path into relative.
without writing a custom regex

thick cosmos
#

@astral pond

oak sedge
#

@thick cosmos This depends on how you obtain the path in the first place. And what the relative path is relative to.

thick cosmos
#

i have a path to "cut" the front off.
and the absolute.
it is obtained by a Path type in the rust backend.

#

it's a string in frontend

oak sedge
#

Can you provide an example?

  • What's the full/absolute path?
  • What to do you want to cut off and what part remains?
  • How is it supposed to look in the UI?
#
  • How do you plan to display the path and make it clickable (using a link or js?)
thick cosmos
#

do you not know just path lingo?
abs path: full path from drive to file
relative: a short term path (sometimes starting with ./) "relative" to the current path.

thick cosmos
#

i already have the UI setup. i just want a way to turn abs to rel

oak sedge
# thick cosmos do you not know just path lingo? abs path: full path from drive to file relative...

I do know what absolute and relative paths are, but it is hard to know what you mean, for example by "current path". Current path of a file browser? Or of the shell's working directory? A concrete example will make it clearer what exactly you are working with.

If you have the two components of the path in two different variables, it should be easy enough to use JavaScript to subtract the base path from the absolute path to get your relative path.

const absolutePath = '/home/{user}/Documents/file.txt';
const prefix       = '/home/{user}'

const relativePath = absolutePath.substring(prefix.length, absolutePath.length);
// console.log(relativePath);
// relativePath contains '/Documents/file.txt'
thick cosmos
#

it's a file system

oak sedge
#

Sorry, I meant a concrete example with actual paths so we know what you are trying to do, not what absolute/relative paths are in general. 😉

thick cosmos
#

C:/beans/taco/arf.what & C:/beans -> taco/arf.what

oak sedge
#

Ok great. You can use the code I posted above to get exactly that.
You just need C:\beans in a variable. This may need to change dynamically, depending on where you get your current path from or what the directory separators of your OS are.

thick cosmos
thick cosmos
#

also, i want to join paths too 😄

astral pond
#

You are not making yourself easy to help.

This path manipulation stuff is easy to do in Rust. If you want to do it in the JS only, find a browser-compatible implementation somewhere.

thick cosmos
#

exactly,

#

i'm looking for an npm package.

astral pond
#

I don't suspect you're going to find many browser-compatible packages for cross-platform (assuming that's a concern of yours, it may not be) file path manipulation, because that's not a common concern in browser-specific apps.

As I've said, doing what you want in Rust is very easy. But if you refuse to do that or to write your own JS code to do it, you can search the npm package registry just the same as others here.

thick cosmos
#

but in tauri you can use npm packages inside the frontend.
so why couldn't you use a package ment for node backend in the front?
*not the Node builtin modules themselves

#

starting over:
i want to not write a custom function. just a module or smth i can import.
*not code to paste.

astral pond
#

Because packages that depend on Node runtime APIs ("a package meant for Node backend") will not work... because the Node runtime-provided APIs are not present.

#

Because Tauri doesn't use/ship Node.