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

implement string escape --style=xxx and string unescape #4159

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,8 @@
- The `COLUMNS` and `LINES` env vars are now correctly set the first time `fish_prompt` is run (#4141).
- New `status is-breakpoint` command that is true when a prompt is displayed in response to a `breakpoint` command (#1310).
- Invalid array indexes are now silently ignored (#826, #4127).
- `string escape` has a new `--style=xxx` flag where `xxx` can be `script`, `var`, or `url` (#4150)
- `string unescape` has been implemented to reverse the effects of `string escape` (#3543)

## Other significant changes

Expand Down
16 changes: 14 additions & 2 deletions doc_src/string.txt
Expand Up @@ -2,7 +2,7 @@

\subsection string-synopsis Synopsis
\fish{synopsis}
string escape [(-n | --no-quoted)] [STRING...]
string escape [(-n | --no-quoted)] [--style=xxx] [STRING...]
string join [(-q | --quiet)] SEP [STRING...]
string length [(-q | --quiet)] [STRING...]
string lower [(-q | --quiet)] [STRING...]
Expand All @@ -18,6 +18,7 @@ string sub [(-s | --start) START] [(-l | --length) LENGTH] [(-q | --quiet)]
[STRING...]
string trim [(-l | --left)] [(-r | --right)] [(-c | --chars CHARS)]
[(-q | --quiet)] [STRING...]
string unescape [--style=xxx] [STRING...]
string upper [(-q | --quiet)] [STRING...]
\endfish

Expand All @@ -36,7 +37,13 @@ The following subcommands are available.

\subsection string-escape "escape" subcommand

`string escape` escapes each STRING such that it can be passed back to `eval` to produce the original argument again. By default, all special characters are escaped, and quotes are used to simplify the output when possible. If `-n` or `--no-quoted` is given, the simplifying quoted format is not used. Exit status: 0 if at least one string was escaped, or 1 otherwise.
`string escape` escapes each STRING in one of three ways. The first is `--style=script`. This is the default. It alters the string such that it can be passed back to `eval` to produce the original argument again. By default, all special characters are escaped, and quotes are used to simplify the output when possible. If `-n` or `--no-quoted` is given, the simplifying quoted format is not used. Exit status: 0 if at least one string was escaped, or 1 otherwise.

The second is `--style=var` which ensures the string can be used as a variable name by hex encoding any non-alphanumeric characters. The string is first converted to UTF-8 before being encoded.

The third is `--style=url` which ensures the string can be used as a URL by hex encoding any character which is not legal in a URL. The string is first converted to UTF-8 before being encoded.

`string unescape` performs the inverse of the `string escape` command. If the string to be unescaped is not properly formatted it is ignored. For example, doing `string unescape --style=var (string escape --style=var $str)` will return the original string.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing the "not properly formatted" means "not valid in the requested escape-style"? I.e. a valid variable name will be returned, but a string that is not valid as a variable name won't be.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. string unescape will correctly handle any output from string escape. But if you hand it an arbitrary sequence of characters it may or may not return something useful. It won't crash but there is no guarantee you'll get an unescaped string. The alternative is to return the input string but I think returning nothing and setting status to one is better.


\subsection string-join "join" subcommand

Expand Down Expand Up @@ -159,6 +166,11 @@ In general, special characters are special by default, so `a+` matches one or mo
<bs>cg</bs>
\endfish

\fish{cli-dark}
>_ string escape --style=var 'a1 b2'\u6161
<bs>a1_20b2__c_E6_85_A1</bs>
\endfish

\subsection string-example-match-glob Match Glob Examples

\fish{cli-dark}
Expand Down
5 changes: 0 additions & 5 deletions po/de.po
Expand Up @@ -61324,11 +61324,6 @@ msgstr ""
msgid "Update man-page based completions"
msgstr "Befehlsspezifische Erweiterungen bearbeiten"

#: /tmp/fish/implicit/share/functions/__fish_urlencode.fish:1
#, fuzzy
msgid "URL-encode stdin"
msgstr "Standardeingabe umbenennen"

#: /tmp/fish/implicit/share/functions/__fish_use_subcommand.fish:1
#, fuzzy
msgid "Test if a non-switch argument has been given in the current commandline"
Expand Down
4 changes: 0 additions & 4 deletions po/en.po
Expand Up @@ -8114,10 +8114,6 @@ msgstr "Show current path"
msgid "Test if the token under the cursor matches the specified wildcard"
msgstr "Test if the token under the cursor matches the specified wildcard"

#: share/functions/__fish_urlencode.fish:1
msgid "URL-encode stdin"
msgstr "URL-encode stdin"

#: share/functions/__terlar_git_prompt.fish:23
msgid "Write out the git prompt"
msgstr "Write out the git prompt"
Expand Down
4 changes: 0 additions & 4 deletions po/fr.po
Expand Up @@ -60024,10 +60024,6 @@ msgstr ""
msgid "Update man-page based completions"
msgstr "Éditer les complétions spécifiques aux commandes"

#: /tmp/fish/implicit/share/functions/__fish_urlencode.fish:1
msgid "URL-encode stdin"
msgstr ""

#: /tmp/fish/implicit/share/functions/__fish_use_subcommand.fish:1
#, fuzzy
msgid "Test if a non-switch argument has been given in the current commandline"
Expand Down
4 changes: 0 additions & 4 deletions po/nb.po
Expand Up @@ -56722,10 +56722,6 @@ msgstr ""
msgid "Update man-page based completions"
msgstr ""

#: /tmp/fish/implicit/share/functions/__fish_urlencode.fish:1
msgid "URL-encode stdin"
msgstr ""

#: /tmp/fish/implicit/share/functions/__fish_use_subcommand.fish:1
msgid "Test if a non-switch argument has been given in the current commandline"
msgstr ""
Expand Down
4 changes: 0 additions & 4 deletions po/nn.po
Expand Up @@ -56722,10 +56722,6 @@ msgstr ""
msgid "Update man-page based completions"
msgstr ""

#: /tmp/fish/implicit/share/functions/__fish_urlencode.fish:1
msgid "URL-encode stdin"
msgstr ""

#: /tmp/fish/implicit/share/functions/__fish_use_subcommand.fish:1
msgid "Test if a non-switch argument has been given in the current commandline"
msgstr ""
Expand Down
4 changes: 0 additions & 4 deletions po/pl.po
Expand Up @@ -57143,10 +57143,6 @@ msgstr ""
msgid "Update man-page based completions"
msgstr "Edytuj sugestie do określonej komendy"

#: /tmp/fish/implicit/share/functions/__fish_urlencode.fish:1
msgid "URL-encode stdin"
msgstr ""

#: /tmp/fish/implicit/share/functions/__fish_use_subcommand.fish:1
msgid "Test if a non-switch argument has been given in the current commandline"
msgstr ""
Expand Down
5 changes: 0 additions & 5 deletions po/pt_BR.po
Expand Up @@ -61774,11 +61774,6 @@ msgstr ""
msgid "Update man-page based completions"
msgstr "Edit command specific completions"

#: /tmp/fish/implicit/share/functions/__fish_urlencode.fish:1
#, fuzzy
msgid "URL-encode stdin"
msgstr "Rename stdin"

#: /tmp/fish/implicit/share/functions/__fish_use_subcommand.fish:1
#, fuzzy
msgid "Test if a non-switch argument has been given in the current commandline"
Expand Down
4 changes: 0 additions & 4 deletions po/sv.po
Expand Up @@ -53577,10 +53577,6 @@ msgstr ""
msgid "Update man-page based completions"
msgstr ""

#: /tmp/fish/implicit/share/functions/__fish_urlencode.fish:1
msgid "URL-encode stdin"
msgstr ""

#: /tmp/fish/implicit/share/functions/__fish_use_subcommand.fish:1
msgid "Test if a non-switch argument has been given in the current commandline"
msgstr ""
Expand Down
4 changes: 0 additions & 4 deletions po/zh_CN.po
Expand Up @@ -57099,10 +57099,6 @@ msgstr ""
msgid "Update man-page based completions"
msgstr "编辑命令相关的补全"

#: /tmp/fish/implicit/share/functions/__fish_urlencode.fish:1
msgid "URL-encode stdin"
msgstr ""

#: /tmp/fish/implicit/share/functions/__fish_use_subcommand.fish:1
msgid "Test if a non-switch argument has been given in the current commandline"
msgstr ""
Expand Down
2 changes: 1 addition & 1 deletion share/functions/__fish_config_interactive.fish
Expand Up @@ -262,7 +262,7 @@ function __fish_config_interactive -d "Initializations that should be performed
status --is-command-substitution
or test -n "$INSIDE_EMACS"
and return
printf \e\]7\;file://\%s\%s\a (hostname) (echo -n $PWD | __fish_urlencode)
printf \e\]7\;file://\%s\%s\a (hostname) (string escape --style=url $PWD)
end
__update_cwd_osc # Run once because we might have already inherited a PWD from an old tab
end
Expand Down
26 changes: 0 additions & 26 deletions share/functions/__fish_urlencode.fish

This file was deleted.