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

The Go prompt detection seems incorrect (and incomplete) #329

Open
akutz opened this issue Oct 17, 2020 · 2 comments
Open

The Go prompt detection seems incorrect (and incomplete) #329

akutz opened this issue Oct 17, 2020 · 2 comments

Comments

@akutz
Copy link

akutz commented Oct 17, 2020

Hi,

I just started using Zsh, not to mention oh-my-zsh, so please excuse me if I make a mistake triaging this issue. I was trying to get the Go prompt working for bullet-train (great theme by the way!), and I just couldn't. It turns out the logic that detects whether to use a Go prompt appears to be incorrect and incomplete:

# Go
prompt_go() {
  setopt extended_glob
  if [[ (-f *.go(#qN) || -d Godeps || -f glide.yaml) ]]; then
    if command -v go > /dev/null 2>&1; then
      prompt_segment $BULLETTRAIN_GO_BG $BULLETTRAIN_GO_FG $BULLETTRAIN_GO_PREFIX" $(go version | grep --colour=never -oE '[[:digit:]].[[:digit:]]+')"
    fi
  fi
}

Issue 1 - Glob Glob

Here's what I mean:

  1. Create a temporary directory:

    cd $(mktemp -d)
  2. Enable extended glob:

    setopt extended_glob
  3. Test for go sources:

    $ { [[ -f *.go(#qN) ]] && echo 'go source(s) detected!'; } || echo 'no go :('
    no go :(
  4. Create a file that ends with .go:

    touch hello.go
  5. Test for go sources again:

    $ { [[ -f *.go(#qN) ]] && echo 'go source(s) detected!'; } || echo 'no go :('
    go source(s) detected!
  6. Create a second file that ends with .go:

    touch world.go
  7. Test for go sources again:

    $ { [[ -f *.go(#qN) ]] && echo 'go source(s) detected!'; } || echo 'no go :('
    no go :(

So why does the above fail? Because the -f operator does not work against multiple operands:

-f file

    true if file exists and is a regular file.

Instead zsh indicates to use the -n operator for detecting multiple files:

$ { [[ -n *.go(#qN) ]] && echo 'go source(s) detected!'; } || echo 'no go :('
go source(s) detected!

Oh, and just to prove -n works in the other cases:

# Remove world.go and expect a positive message
$ rm world.go && { [[ -n *.go(#qN) ]] && echo 'go source(s) detected!'; } || echo 'no go :('
go source(s) detected!

# Remove hello.go and expect a negative message
$ rm hello.go && { [[ -n *.go(#qN) ]] && echo 'go source(s) detected!'; } || echo 'no go :('
no go :(

Yep, works :)

Issue 2 - Go Modules

Finally, it looks like y'all don't yet support Go modules for detection. The detection line should be updated as follows (with the fix for the s/-f/-n/:

if [[ (-n *.go(#qN) || -d Godeps || -f glide.yaml || -f go.mod) ]]; then

Thanks!

@FallenWarrior2k
Copy link

I addressed some of these issues in #317, although in my own fork, I removed the glob for Go sources entirely, so I didn't commit a fix for that first.
If you want to keep the glob, I could make a commit on the branch for that PR that fixes it (doesn't hurt anyone after all), although I would probably discard that commit for the merge back into my own master.

@ProfessorConfundus
Copy link

Hm, I have actually noticed this too! How odd.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants