This can be used e.g. by a bookmarking script to show normalized paths.
Unified · Split
| @@ -0,0 +1 @@ | ||
| 1 | +add the `normalize-path` command | |
| @@ -827,6 +827,21 @@ Remember to quote string arguments in input.conf (see `Flat command syntax`_). · 826 unmodified lines | ||
| 827 | 827 | This line of Lua would show the location of the user's mpv |
| 828 | 828 | configuration directory on the OSD. |
| 829 | 829 | |
| 830 | +``normalize-path <filename>`` | |
| 831 | + Return a canonical representation of the path ``filename`` by converting it | |
| 832 | + to an absolute path, removing consecutive slashes, removing ``.`` | |
| 833 | + components, resolving ``..`` components, and converting slashes to | |
| 834 | + backslashes on Windows. Symlinks are not resolved unless the platform is | |
| 835 | + Unix-like and one of the path components is ``..``. If ``filename`` is a | |
| 836 | + URL, it is returned unchanged. This can only be used through the client API | |
| 837 | + or from a script using ``mp.command_native``. | |
| 838 | + | |
| 839 | + .. admonition:: Example | |
| 840 | + | |
| 841 | + ``mp.osd_message(mp.command_native({"normalize-path", "/foo//./bar"}))`` | |
| 842 | + | |
| 843 | + This line of Lua prints "/foo/bar" on the OSD. | |
| 844 | + | |
| 830 | 845 | ``escape-ass <text>`` |
| 831 | 846 | Modify ``text`` so that commands and functions that interpret ASS tags, |
| 832 | 847 | such as ``osd-overlay`` and ``mp.create_osd_overlay``, will display it |
| @@ -5644,6 +5644,19 @@ static void cmd_expand_path(void *p) · 5643 unmodified lines | ||
| 5644 | 5644 | }; |
| 5645 | 5645 | } |
| 5646 | 5646 | |
| 5647 | +static void cmd_normalize_path(void *p) | |
| 5648 | +{ | |
| 5649 | + struct mp_cmd_ctx *cmd = p; | |
| 5650 | + void *ctx = talloc_new(NULL); | |
| 5651 | + | |
| 5652 | + cmd->result = (mpv_node){ | |
| 5653 | + .format = MPV_FORMAT_STRING, | |
| 5654 | + .u.string = talloc_strdup(NULL, mp_normalize_path(ctx, cmd->args[0].v.s)), | |
| 5655 | + }; | |
| 5656 | + | |
| 5657 | + talloc_free(ctx); | |
| 5658 | +} | |
| 5659 | + | |
| 5647 | 5660 | static void cmd_escape_ass(void *p) |
| 5648 | 5661 | { |
| 5649 | 5662 | struct mp_cmd_ctx *cmd = p; |
| @@ -6764,6 +6777,7 @@ const struct mp_cmd_def mp_cmds[] = { · 1114 unmodified lines | ||
| 6764 | 6777 | .is_noisy = true }, |
| 6765 | 6778 | { "expand-path", cmd_expand_path, { {"text", OPT_STRING(v.s)} }, |
| 6766 | 6779 | .is_noisy = true }, |
| 6780 | + { "normalize-path", cmd_normalize_path, { {"filename", OPT_STRING(v.s)} }}, | |
| 6767 | 6781 | { "escape-ass", cmd_escape_ass, { {"text", OPT_STRING(v.s)} }, |
| 6768 | 6782 | .is_noisy = true }, |
| 6769 | 6783 | { "show-progress", cmd_show_progress, .allow_auto_repeat = true, |