Paths and directories
In the terminalela --spec paths-and-directories
- Modelled on Go's
filepathpackage, withpath.programas Go'sexec.LookPath:
| Built-in | Does |
|---|---|
path.join(parts...) | The parts joined with /, cleaned |
path.base(p), path.dir(p), path.ext(p) | The last element, everything before it, the last element's extension |
path.abs(p) | The path made absolute against the current folder |
path.glob(pattern) | The paths that match, as a list, empty when none do |
path.program(name) | The full path of the program name on the script's PATH, or "" when there is none |
path.real(p) | The path made absolute with every link in it followed, as far as the path is there; the rest as written, as realpath -m gives it |
path.link(p) | The target a symbolic link holds, as it is written in the link, as readlink gives it; anything else at p, or nothing, is an error |
path.script() | The absolute path of the file the call is written in, with its links followed, as readlink -f gives it; its folder is path.dir(path.script()) |
path.program(name)splits the script's own$env.PATHat:, so awith env.PATHblock counts, and takesfolder/namein each folder in turn, classified without opening it: the first regular file this user may run is the answer. A name with a/in it is checked as written. A script asks whether a program is there without running it or printing anything; the shell built-inwhichalso names aliases, procs and packages.
if path.program("docker") != "" docker compose up -dend if
path.realreads the links: a..after a link goes up from where the link leads, and more than 40 links on the way, or a loop of them, is an error. Test that the path is there withfile.exists.
root = path.real($args[0]) # a checkout named through a link, as its own folderif file.is_link("$root/current") echo "current is ${path.link("$root/current")}"end if
path.script()is the file the call is written in, worked out as the file is read, so awith diror acdlater changes nothing. In an imported file it is that file, so a library finds what ships beside it. A script started through a link gives the file the link leads to. At the prompt, inela -cand in a script read from stdin there is no file, and the call is an error:path.script: the code came from -c, so there is no file; give a path, or run it as a file.
#!/usr/bin/env ela# ~/Code/play/house-rules/scripts/roots, a link to ../ela/roots.elahere = path.dir(path.script())listed = file.read("$here/../roots")echo $here # /home/ada/Code/play/house-rules/ela
path.absworks on the text alone, joining the path to the current folder and cleaning it: the path need not exist, and a link in it is not followed.path.base("/srv/app/")isappandpath.dir("app")is., as in Go.- Directories:
dir.list(path, fold = false)returns the names in it, sorted by character code (Bbeforea,äafterz), hidden ones included, and withfold = trueas people read them, case and accents folded,abesideAandäbesidea, the same on every machine whateverLANGsays (LANGUAGE.md, Sorting a list);dir.create(path)makes it and any missing parents, and a directory already there is fine;dir.delete(path)removes an empty directory only, since deleting a tree is one typo away from deleting the wrong one.dir.temp(),dir.size(path)anddir.walk(path)aremktemp -d,du -sbandfind, under File programs in#files. - Globbing works as in Bash,
for f in *.elaandfor f in $dir/*.log, with one difference: a glob that matches nothing is an error, never passed on as text, sorm *.bakcan't handrma file named*.bak. - A glob has
*for any run of characters and?for one, neither crossing a/,[abc]and[a-z]for one of a set and[^abc]for one outside it;\makes the next character itself, in a set too ([\]\-]). Bash's[!abc]is an error that names[^abc], since Odin's match, which ela's globs use, reads it as!or one ofabc. Text from a variable or a quoted string is never a glob character, so[$x]is a set of$x's characters whatever they are. A name starting with a dot matches only a pattern that starts with one,.or\., sorm *leaves.gitalone. The same rules hold in a command, inpath.globand instr.match. - Where no match is fine,
path.glob(pattern)returns the matches as a list, empty when there are none, as Go'sfilepath.Globdoes. Every*,?and[in its argument counts, since the argument is the pattern:
backups = path.glob("*.bak")if len($backups) > 0 rm $backupsend if