Add status dirname and status basename convenience commands

There's a terrible number of fishscripts that start with

    set path (dirname (status filename))

And that's really just a bit boring.

So let's let it be

    set path (status dirname)
This commit is contained in:
Fabian Homborg 2020-06-01 16:46:20 +02:00
parent af2d19bde0
commit 44184f68e4
3 changed files with 40 additions and 5 deletions

View File

@ -19,6 +19,8 @@ Synopsis
status is-interactive-job-control
status current-command
status filename
status basename
status dirname
status fish-path
status function
status line-number
@ -52,7 +54,11 @@ The following operations (sub-commands) are available:
- ``current-command`` prints the name of the currently-running function or command, like the deprecated ``_`` variable.
- ``filename`` prints the filename of the currently running script. Also ``current-filename``, ``-f`` or ``--current-filename``.
- ``filename`` prints the filename of the currently running script. Also ``current-filename``, ``-f`` or ``--current-filename``. This depends on how the script was called - if it was called via a symlink, the symlink will be returned, and if the current script was received via ``source`` it will be ``-``.
- ``basename`` prints just the filename of the running script, without any path-components before.
- ``dirname`` prints just the path to the running script, without the actual filename itself. This can be relative to $PWD (including just "."), depending on how the script was called. This is the same as passing the ``filename`` to ``dirname(3)``. It's useful if you want to use other files in the current script's directory or similar.
- ``fish-path`` prints the absolute path to the currently executing instance of fish.

View File

@ -19,6 +19,8 @@
enum status_cmd_t {
STATUS_CURRENT_CMD = 1,
STATUS_BASENAME,
STATUS_DIRNAME,
STATUS_FEATURES,
STATUS_FILENAME,
STATUS_FISH_PATH,
@ -40,10 +42,14 @@ enum status_cmd_t {
// Must be sorted by string, not enum or random.
const enum_map<status_cmd_t> status_enum_map[] = {
{STATUS_BASENAME, L"basename"},
{STATUS_BASENAME, L"current-basename"},
{STATUS_CURRENT_CMD, L"current-command"},
{STATUS_DIRNAME, L"current-dirname"},
{STATUS_FILENAME, L"current-filename"},
{STATUS_FUNCTION, L"current-function"},
{STATUS_LINE_NUMBER, L"current-line-number"},
{STATUS_DIRNAME, L"dirname"},
{STATUS_FEATURES, L"features"},
{STATUS_FILENAME, L"filename"},
{STATUS_FISH_PATH, L"fish-path"},
@ -357,12 +363,20 @@ int builtin_status(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
}
break;
}
case STATUS_BASENAME:
case STATUS_DIRNAME:
case STATUS_FILENAME: {
CHECK_FOR_UNEXPECTED_STATUS_ARGS(opts.status_cmd)
const wchar_t *fn = parser.current_filename();
if (!fn) fn = _(L"Standard input");
streams.out.append_format(L"%ls\n", fn);
auto res = parser.current_filename();
wcstring fn = res ? res : L"";
if (!fn.empty() && opts.status_cmd == STATUS_DIRNAME) {
fn = wdirname(fn);
} else if (!fn.empty() && opts.status_cmd == STATUS_BASENAME) {
fn = wbasename(fn);
} else if (fn.empty()) {
fn = _(L"Standard input");
}
streams.out.append_format(L"%ls\n", fn.c_str());
break;
}
case STATUS_FUNCTION: {

View File

@ -36,3 +36,18 @@ echo $status
echo (status is-command-substitution; echo $status)
# CHECK: 1
# CHECK: 0
test (status filename) = (status dirname)/(status basename)
status basename
#CHECK: status-command.fish
status dirname | string match -q '*checks'
echo $status
#CHECK: 0
echo "status dirname" | source
#CHECK: .
$FISH_PATH -c 'status dirname'
#CHECK: Standard input