caiogondim / bullet-train.zsh

:bullettrain_side: An oh-my-zsh shell theme based on the Powerline Vim plugin
MIT License
2.82k stars 382 forks source link

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

Open akutz opened 3 years ago

akutz commented 3 years ago

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 commented 3 years ago

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 commented 2 years ago

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