Path Functions

There are a number of path manipulation functions, primarily for manipulating PATH-like variables.

Warning

As these functions intend to alter a variable, like PATH, you must pass the symbol for the variable 'PATH (otherwise Idio will evaluate the variable and pass a string.

simple-path-mods.idio
P := "/a/b:/c/d"

path-append 'P "/e/f"

printf "P is %s\n" P

;; only if "/a/b" is not already in P
path-append-unique 'P "/a/b"

printf "P is %s\n" P

path-remove 'P "/c/d"

printf "P is %s\n" P

;; append only if /g/h is a directory
path-append 'P "/g/h" :test d?

printf "P is %s\n" P

;; foolishly duplicate P
path-append 'P P

printf "P is %s\n" P

;; clean it up (first instance of each element)
P = trim-path P

printf "P is %s\n" P
$ idio simple-path-mods
P is /a/b:/c/d:/e/f
P is /c/d:/e/f:/a/b
P is /e/f:/a/b
P is /e/f:/a/b
P is /e/f:/a/b:/e/f:/a/b
P is /e/f:/a/b

There are functions for cd/pushd/popd (and a reminder that you must wrap the function in parenthesis if not passing any arguments). These functions maintain the value of PWD.

There is a normalize-pathname function and simple dirname-pathname and basename-pathname functions (named to avoid clashing with the similar external commands):

simple-cd.idio
tmpdir := "/tmp/a/b/c/d"
mkdir -p tmpdir

cd tmpdir

printf "pwd: "
;; run the external command pwd
(pwd)

printf "PWD: %s\n" PWD

printf ".  is %s\n" (normalize-pathname ".")
printf ".. is %s\n" (normalize-pathname "..")
printf "dirname for PWD is %s\n" (dirname-pathname PWD)
printf "basename for PWD is %s\n" (basename-pathname PWD)
$ idio simple-cd
pwd: /tmp/a/b/c/d
PWD: /tmp/a/b/c/d
.  is /tmp/a/b/c/d
.. is /tmp/a/b/c
dirname for PWD is /tmp/a/b/c
basename for PWD is d

Last built at 2024-05-21T06:11:41Z+0000 from 77077af (dev) for Idio 0.3