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

fix: Unescaping certain regex options. #177

Merged
merged 4 commits into from
Dec 3, 2023
Merged

fix: Unescaping certain regex options. #177

merged 4 commits into from
Dec 3, 2023

Commits on Dec 3, 2023

  1. fix: Unescaping certain regex options.

    Many regexes contain escaped characters
    like `\s` or `\w`.
    `tmux-fastcopy` queries the tmux environment
    for regex overwrites.
    Specifically,
    it uses the `tmux show-options -g` command.
    The output in turn escapes the backward slashes,
    turning `\s` into `\\s`.
    To correctly parse the regex overwrites,
    `tmux-fastcopy` needs to unescape these backward slashes too.
    
    The `tmux show-options -g` command prints values in three forms:
    
    -   Unquoted for simple, unspaced values:
    
        ```shell
        $ tmux set @myopt hey ; tmux show @myopt
        @myopt hey
        ```
    
    -   Single-quoted for simple, double-quoted values:
    
        ```shell
        $ tmux set @myopt hey\"world ; tmux show @myopt
        @myopt 'hey"world'
        ```
    
    -   Double-quoted for other values:
    
        ```shell
        $ tmux set @myopt hey\  ; tmux show @myopt
        @myopt "hey "
        ```
    
    Note that the output escapes backward slashes
    in all three forms:
    
    ```shell
    $ tmux set @myopt '\hey' ; tmux show @myopt
    @myopt \\hey
    
    $ tmux set @myopt '\"hey' ; tmux show @myopt
    @myopt '\\"hey'
    
    $ tmux set @myopt '\ hey' ; tmux show @myopt
    @myopt "\\ hey"
    ```
    
    For many regexes,
    tmux would print the value unquoted.
    For example, the path regex built-in to `tmux-fastcopy` is like this:
    
    ```shell
    $ tmux set-option @myopt '(\b([\w.-]+|~)?(/[\w.-]+)+\b)' ; tmux show @myopt
    @myopt (\\b([\\w.-]+|~)?(/[\\w.-]+)+\\b)
    ```
    
    Currently,
    `tmux-fastcopy` relies on `strconv.Unquote`
    in the `tmuxopt.Loader.ReadValue` function
    to unescape the backslashes.
    However,
    a caveat is that `strconv.Unquote` only performs
    if the given string is quoted.
    As a result,
    `tmux-fastcopy` never unescapes the regex from the last paragraph,
    and would fail to match it.
    
    This commit fixes the problem
    by manually double-quoting unquoted strings,
    forcing `strconv.Unquote` to unescape them.
    Jy Yuan committed Dec 3, 2023
    Configuration menu
    Copy the full SHA
    dbc288a View commit details
    Browse the repository at this point in the history
  2. Add an integration test

    Adds an integration test to verify the unquoting behavior
    against a running copy of tmux.
    
    This required exporting the readValue function
    so that we can access it from the integration test package.
    abhinav committed Dec 3, 2023
    Configuration menu
    Copy the full SHA
    61dcfc2 View commit details
    Browse the repository at this point in the history
  3. go mod tidy

    abhinav committed Dec 3, 2023
    Configuration menu
    Copy the full SHA
    5a02fda View commit details
    Browse the repository at this point in the history
  4. fix data race in test

    abhinav committed Dec 3, 2023
    Configuration menu
    Copy the full SHA
    b2ea5d5 View commit details
    Browse the repository at this point in the history