Powerlevel9k / powerlevel9k

Powerlevel9k was a tool for building a beautiful and highly functional CLI, customized for you. P9k had a substantial impact on CLI UX, and its legacy is now continued by P10k.
https://github.com/romkatv/powerlevel10k
MIT License
13.46k stars 947 forks source link

public_ip on MacOS with GNU CoreUtils installed problem with file-stat #1373

Open rmpel opened 5 years ago

rmpel commented 5 years ago

Describe Your Issue

When using the prompt-part 'public_ip' the first display is ok (it will refresh the IP and cache in /tmp/p9k_public_ip). The second time the theme will stat -f "%m" /tmp/p9k_public_ip to find the last-modified stamp. On macOS with GNU CoreUtils installed, this will fail.

prompt_publicip:15: bad math expression: operand expected at `"/tmp/p9k...'

Have you tried to debug or fix it?

disabling the stat -f version and using the date -r version solves the problem

in function prompt_public_ip: if [[ "$OS" == "OSX" ]]; then timediff=$(($(date +%s) - $(stat -f "%m" $POWERLEVEL9K_PUBLIC_IP_FILE))) else timediff=$(($(date +%s) - $(date -r $POWERLEVEL9K_PUBLIC_IP_FILE +%s))) fi

becomes #if [[ "$OS" == "OSX" ]]; then # timediff=$(($(date +%s) - $(stat -f "%m" $POWERLEVEL9K_PUBLIC_IP_FILE))) # else timediff=$(($(date +%s) - $(date -r $POWERLEVEL9K_PUBLIC_IP_FILE +%s))) # fi

or if [[ "$OS" == "OSX" ]] && [[ "gnubin" != "$(basename $(dirname $(which stat)))" ]]; then timediff=$(($(date +%s) - $(stat -f "%m" $POWERLEVEL9K_PUBLIC_IP_FILE))) else timediff=$(($(date +%s) - $(date -r $POWERLEVEL9K_PUBLIC_IP_FILE +%s))) fi

this last one tests stat for being in the gnubin subfolder, but I doubt this is universal over all macOS version and/or brew versions and/or coreutils versions

I know this is is a one in thousands situation, but at least now it is known and people with the same problem know how to fix it :)

Environment Information

This information will help us understand your configuration.

GNU CoreUtils installed with brew install coreutils $ which stat /usr/local/opt/coreutils/libexec/gnubin/stat

onaforeignshore commented 5 years ago

We already have a test in Next for this situation:

function __p9k_detect_os() {
  typeset -g __P9K_OS __P9K_OS_ID
  case $(uname) in
    Darwin)
      __P9K_OS='OSX'
      __P9K_OS_ID="$(uname -r)"
      [[ "$(which stat)" != "/usr/bin/stat" ]] && __P9K_OSX_COREUTILS=true || __P9K_OSX_COREUTILS=false
    ;;
    ...
rmpel commented 5 years ago

Noice! thanks :)