nushell/crates/nu-command/src
Bahex 8771872d86
Add path self command for getting absolute paths to files at parse time (#14303)
Alternative solution to:
- #12195 

The other approach:
- #14305

# Description
Adds ~`path const`~ `path self`, a parse-time only command for getting
the absolute path of the source file containing it, or any file relative
to the source file.

- Useful for any script or module that makes use of non nuscript files.
- Removes the need for `$env.CURRENT_FILE` and `$env.FILE_PWD`.
- Can be used in modules, sourced files or scripts.

# Examples

```nushell
# ~/.config/nushell/scripts/foo.nu
const paths = {
    self: (path self),
    dir: (path self .),
    sibling: (path self sibling),
    parent_dir: (path self ..),
    cousin: (path self ../cousin),
}

export def main [] {
    $paths
}
```

```nushell
> use foo.nu
> foo
╭────────────┬────────────────────────────────────────────╮
│ self       │ /home/user/.config/nushell/scripts/foo.nu  │
│ dir        │ /home/user/.config/nushell/scripts         │
│ sibling    │ /home/user/.config/nushell/scripts/sibling │
│ parent_dir │ /home/user/.config/nushell                 │
│ cousin     │ /home/user/.config/nushell/cousin          │
╰────────────┴────────────────────────────────────────────╯
```


Trying to run in a non-const context
```nushell
> path self
Error:   × this command can only run during parse-time
   ╭─[entry #1:1:1]
 1 │ path self 
   · ─────┬────
   ·      ╰── can't run after parse-time
   ╰────
  help: try assigning this command's output to a const variable
```

Trying to run in the REPL i.e. not in a file
```nushell
> const foo = path self
Error:   × Error: nu:🐚:file_not_found
  │ 
  │   × File not found
  │    ╭─[entry #3:1:13]
  │  1 │ const foo = path self
  │    ·             ─────┬────
  │    ·                  ╰── Couldn't find current file
  │    ╰────
  │ 
   ╭─[entry #3:1:13]
 1 │ const foo = path self
   ·             ─────┬────
   ·                  ╰── Encountered error during parse-time evaluation
   ╰────
```

# Comparison with #14305
## Pros
- Self contained implementation, does not require changes in the parser.
- More concise usage, especially with parent directories.

---------

Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
2024-12-06 08:19:08 -06:00
..
bytes Remove duplicate implementations of CallExt::rest (#14484) 2024-12-01 15:03:45 +01:00
charting Add Filesize type (#14369) 2024-11-29 21:24:17 +00:00
conversions Deprecate date to-record and date to-table (#14319) 2024-11-29 23:06:26 +01:00
database Add Filesize type (#14369) 2024-11-29 21:24:17 +00:00
date Deprecate date to-record and date to-table (#14319) 2024-11-29 23:06:26 +01:00
debug Start to Add WASM Support Again (#14418) 2024-11-30 07:57:11 -06:00
env Start to Add WASM Support Again (#14418) 2024-11-30 07:57:11 -06:00
experimental Start to Add WASM Support Again (#14418) 2024-11-30 07:57:11 -06:00
filesystem Make glob stream (#14495) 2024-12-03 15:21:09 -06:00
filters allow select to stream more (#14492) 2024-12-03 20:45:31 -06:00
formats Add Filesize type (#14369) 2024-11-29 21:24:17 +00:00
generators Seq char update will work on all char (#14261) 2024-11-15 21:05:29 +01:00
hash add binary as input to hash commands (#13923) 2024-09-25 16:47:52 +08:00
help Change append operator to concatenation operator (#14344) 2024-11-24 10:59:54 -08:00
math Add Filesize type (#14369) 2024-11-29 21:24:17 +00:00
misc Add environment variables for sourced files (#14486) 2024-12-02 06:19:20 -06:00
network Start to Add WASM Support Again (#14418) 2024-11-30 07:57:11 -06:00
path Add path self command for getting absolute paths to files at parse time (#14303) 2024-12-06 08:19:08 -06:00
platform Improve sleep example using multiple durations (#14520) 2024-12-05 07:54:14 -06:00
random Add Filesize type (#14369) 2024-11-29 21:24:17 +00:00
removed Change the usage misnomer to "description" (#13598) 2024-08-22 12:02:08 +02:00
shells Change the usage misnomer to "description" (#13598) 2024-08-22 12:02:08 +02:00
stor feat: stor insert accepts lists (#14175) 2024-10-29 06:32:55 -05:00
strings update unicode-width to 0.2 (#14456) 2024-11-29 09:09:45 +08:00
system Remove duplicate implementations of CallExt::rest (#14484) 2024-12-01 15:03:45 +01:00
viewers Start to Add WASM Support Again (#14418) 2024-11-30 07:57:11 -06:00
default_context.rs Add path self command for getting absolute paths to files at parse time (#14303) 2024-12-06 08:19:08 -06:00
example_test.rs Overhaul $in expressions (#13357) 2024-07-17 16:02:42 -05:00
lib.rs Start to Add WASM Support Again (#14418) 2024-11-30 07:57:11 -06:00
progress_bar.rs Cut down unnecessary lint allows (#14335) 2024-11-15 19:24:39 +01:00
sort_utils.rs Rework sorting and add cell path and closure comparators to sort-by (#13154) 2024-10-09 19:18:16 -07:00