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

zsh: cd into folder name including brackets fails to match a supposed pattern #595

Open
AndydeCleyre opened this issue Sep 7, 2022 · 9 comments
Labels
bug Something isn't working

Comments

@AndydeCleyre
Copy link

$ podman run -it -e TERM=$TERM --rm docker.io/library/alpine:edge
# apk add broot zsh
# broot --version
broot 1.14.2
# broot --print-shell-function zsh >>~/.zshrc
# exec zsh
# mkdir '[notapattern]'
# br

In broot, use verb cd on the folder named [notapattern]

New Configuration files written in "/root/.config/broot".
You should have a look at them.
(eval):1: no matches found: /[notapattern]
@AndydeCleyre AndydeCleyre added the bug Something isn't working label Sep 7, 2022
@Canop
Copy link
Owner

Canop commented Sep 7, 2022

I confirm that this looks like a zsh specific problem:

  • I reproduce it on zsh
  • I can't reproduce it on bash
@AndydeCleyre
Copy link
Author

AndydeCleyre commented Sep 8, 2022

I'm not sure what the generated outcmd looks like. Is it generated specifically depending on the shell, or is the same outcmd used regardless of shell? And I really don't know how the filename gets formatted/inserted...

If it's shell-aware, for zsh it might be a good idea to:

  • pipe the filename into a variable, e.g.
    print -rl -- FILENAME $'\0' | read -d $'\0' itsaname 
  • minimally quote it, e.g.
    cd ${(q-)itsaname}
@Canop
Copy link
Owner

Canop commented Sep 8, 2022

The function used for zsh is defined here: https://github.com/Canop/broot/blob/master/src/shell_install/bash.rs#L31

If you're willing to investigate and try a better general solution, go ahead, I'm a little too busy atm.

@AndydeCleyre
Copy link
Author

AndydeCleyre commented Sep 8, 2022

Thanks, I'll look around. But I'm not so much wondering about the br function as the outcmd generation.

EDIT: found cmd_result_exec_from_parent_shell, will take a closer look.

EDIT: even more relevant: shell_exec_string

@AndydeCleyre
Copy link
Author

Can you replace the Zsh-specific cd command with noglob cd?

$ mkdir '[nopat]'
$ cd [nopat]
zsh: no matches found: [nopat]
$ noglob cd [nopat]
$ pwd
/home/andy/[nopat]

If broot's cd verb always operates on a literal path, it should never need a glob, so it seems safe enough.

@Canop
Copy link
Owner

Canop commented Aug 2, 2023

@AndydeCleyre Would that entry in verbs.hjson solve the problem for zsh users ?

    {
        key: alt-enter
        external: "noglob cd {file}"
        from_shell: true
    }
@AndydeCleyre
Copy link
Author

That verb works, yes.

But I don't use a shortcut and rely on typing :cd. I currently override the :cd behavior for files so that it works on that file's parent:

{
  apply_to: "file"
  cmd: ":focus;:cd"
  shortcut: cd
}

But reviewing the docs and this conversation, it seems I can replace that with:

{
  external: "noglob cd {directory}"
  invocation: cd
  from_shell: true
}

And it all seems to work. Thanks!

@Canop
Copy link
Owner

Canop commented Aug 2, 2023

Now the question is how to make this verb discoverable by zsh users...

{
  external: "noglob cd {directory}"
  key: alt-enter
  invocation: cd
  from_shell: true
}
@AndydeCleyre
Copy link
Author

This problem can now (>1.33.1) also be avoided by using the following verb:

{
  key: alt-enter
  invocation: cd
  cmd: ":write_output {directory};:quit"
}

with the following launcher function (Zsh):

zmodload zsh/mapfile

# -- Run broot, cd into pathfile if successful --
# Depends: zmapfile
br () {  # [<broot-opt>...]
  emulate -L zsh

  local pathfile=$(mktemp)
  trap "rm ${(q-)pathfile}" EXIT INT QUIT
  if { broot --verb-output "$pathfile" $@ } {
    if [[ -r $pathfile ]] {
      local folder=${mapfile[$pathfile]}
      if [[ $folder ]]  cd $folder
    }
  } else {
    return
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
2 participants