Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

command: add normalize-path command #14071

Merged
merged 1 commit into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DOCS/interface-changes/normalize-path.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add the `normalize-path` command
15 changes: 15 additions & 0 deletions DOCS/man/input.rst
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,21 @@ Remember to quote string arguments in input.conf (see `Flat command syntax`_).
This line of Lua would show the location of the user's mpv
configuration directory on the OSD.

``normalize-path <filename>``
Return a canonical representation of the path ``filename`` by converting it
to an absolute path, removing consecutive slashes, removing ``.``
components, resolving ``..`` components, and converting slashes to
backslashes on Windows. Symlinks are not resolved unless the platform is
Unix-like and one of the path components is ``..``. If ``filename`` is a
URL, it is returned unchanged. This can only be used through the client API
or from a script using ``mp.command_native``.

.. admonition:: Example

``mp.osd_message(mp.command_native({"normalize-path", "/foo//./bar"}))``

This line of Lua prints "/foo/bar" on the OSD.

``escape-ass <text>``
Modify ``text`` so that commands and functions that interpret ASS tags,
such as ``osd-overlay`` and ``mp.create_osd_overlay``, will display it
Expand Down
14 changes: 14 additions & 0 deletions player/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -5644,6 +5644,19 @@ static void cmd_expand_path(void *p)
};
}

static void cmd_normalize_path(void *p)
{
struct mp_cmd_ctx *cmd = p;
void *ctx = talloc_new(NULL);

cmd->result = (mpv_node){
.format = MPV_FORMAT_STRING,
.u.string = talloc_strdup(NULL, mp_normalize_path(ctx, cmd->args[0].v.s)),
};

talloc_free(ctx);
}

static void cmd_escape_ass(void *p)
{
struct mp_cmd_ctx *cmd = p;
Expand Down Expand Up @@ -6764,6 +6777,7 @@ const struct mp_cmd_def mp_cmds[] = {
.is_noisy = true },
{ "expand-path", cmd_expand_path, { {"text", OPT_STRING(v.s)} },
.is_noisy = true },
{ "normalize-path", cmd_normalize_path, { {"filename", OPT_STRING(v.s)} }},
{ "escape-ass", cmd_escape_ass, { {"text", OPT_STRING(v.s)} },
.is_noisy = true },
{ "show-progress", cmd_show_progress, .allow_auto_repeat = true,
Expand Down