Skip to content

__fish_urlencode wide character output confuses OS X Terminal.app #2598

Closed

Description

@floam

If I make a directory called, for example, ~/ok-太空步教学视频 and change into it, the proxy icon in Terminal.app will not be resolvable, because of the file:// URI produced on interactive terminals.

It is outputting for example %592a where bash is outputting %E5%A4%AA which works.

I can easily pick out in the inspector under Directory the difference between a working proxy icon and these non-working ones. Feeding the ones fish makes into open(1) produces an error.

fish:
file://MacBook-Pro.local/%55%73%65%72%73/%66%6c%6f%61%6d/%6f%6b%2d%592a%7a7a%6b65%6559%5b66%89c6%9891

bash:
file://MacBook-Pro.local/Users/floam/ok-%E5%A4%AA%E7%A9%BA%E6%AD%A5%E6%95%99%E5%AD%A6%E8%A7%86%E9%A2%91

Bash uses this function from /etc/bashrc_Apple_Terminal:

# Working Directory
#
# Tell the terminal about the current working directory at each prompt.

if [ -z "$INSIDE_EMACS" ]; then
    update_terminal_cwd() {
    # Identify the directory using a "file:" scheme URL, including
    # the host name to disambiguate local vs. remote paths.

    # Percent-encode the pathname.
    local url_path=''
    {
        # Use LC_CTYPE=C to process text byte-by-byte. Ensure that
        # LC_ALL isn't set, so it doesn't interfere.
        local i ch hexch LC_CTYPE=C LC_ALL=
        for ((i = 0; i < ${#PWD}; ++i)); do
        ch="${PWD:i:1}"
        if [[ "$ch" =~ [/._~A-Za-z0-9-] ]]; then
            url_path+="$ch"
        else
            printf -v hexch "%02X" "'$ch"
            # printf treats values greater than 127 as
            # negative and pads with "FF", so truncate.
            url_path+="%${hexch: -2:2}"
        fi
        done
    }

    printf '\e]7;%s\a' "file://$HOSTNAME$url_path"
    }
    PROMPT_COMMAND="update_terminal_cwd${PROMPT_COMMAND:+; $PROMPT_COMMAND}"
fi

I tried to imitate that script using fish's new string functions and the printf it has but cannot get fish's printf to do the same conversion here no matter what my LC_ALL, etc.

Activity

  1. floam commented on Dec 11, 2015

    @floam
    MemberAuthor

    For what it's worth, this fixes OS X. Maybe not the best way.

    function __fish_urlencode --description "URL-encode stdin"
        set -l IFS ''
        set -l output
        while read --array --local lines
            if [ (count $lines) -gt 0 ]
                set output $output (command printf '%%%02X' "'"$lines"'" | string replace --all 'FF' '' | string replace --all "%2F" '/')
            end
        end
        echo $output
    end
    
  2. floam commented on Dec 12, 2015

    @floam
    MemberAuthor

    Whoops. While I don't get those warnings, I just actually tried cd'ing into the directory with my code on a fresh HEAD and it doesn't work to produce resolvable-to-Terminal.app or open()able URIs either... I think while trying random stuff I probably did something to mess up my locale across sessions. Your fix produces working proxy icons after cd'ing. Alternatively the same LC_ALL allowed me to use the builtin printf versus /usr/bin/printf the way I wanted to (unless I'm crazy again). Thanks!

    function __fish_urlencode --description "URL-encode stdin"
        set -l IFS ''
        set -l LC_ALL C
        while read --array --local chars
            if test (count $chars) -gt 0
                for char in $chars
                    if test (string match -r -q '[-_/\w.]' $char) -eq 0
                        printf $char
                    else
                        printf '%%%02X' "'$char"
                    end
                end
            end
        end
    end
  3. added this to the milestone on Feb 28, 2016
  4. locked as resolved and limited conversation to collaborators on Apr 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions