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

(How to) Add right prompt element for processor architecture? #1635

Closed
ahgraber opened this issue Nov 13, 2021 · 2 comments
Closed

(How to) Add right prompt element for processor architecture? #1635

ahgraber opened this issue Nov 13, 2021 · 2 comments

Comments

@ahgraber
Copy link

ahgraber commented Nov 13, 2021

It would be helpful to have a reminder whether I'm working in a rosetta emulated terminal or a native one. Is there a way to add architecture to the right prompt?

Currently I have an archcheck function that is called whenever I start a new zsh session, but I'd like a more persistent reference:

archcheck () {
   if [[ "$(/usr/bin/arch)" == "i386" ]] || [[ "$(arch)" == "x86_64" ]]; then
     echo "Running in $(/usr/bin/arch) mode (Rosetta)"
     [[ -f /usr/local/homebrew/bin/brew ]] && eval "$(/usr/local/homebrew/bin/brew shellenv)"
     alias brew='/usr/local/homebrew/bin/brew'
   elif [[ "$(/usr/bin/arch)" == "arm64" ]]; then
     echo "Running in $(/usr/bin/arch) mode (Native)"
     [[ -f /opt/homebrew/bin/brew ]] && eval "$(/opt/homebrew/bin/brew shellenv)"
     alias brew='/opt/homebrew/bin/brew'
   else
     echo "Unknown architecture detected: $(uname -p) // $(arch)"
   fi
}
archcheck

I don't want the full functionality in p10k (the majority of it does not make sense), but just having the output of /usr/bin/arch or uname -p in the right prompt would be very useful! Thanks!

@romkatv
Copy link
Owner

romkatv commented Nov 13, 2021

Add this to ~/.zshrc:

typeset -g my_arch=$(/usr/bin/arch)
case $my_arch in
  i386|x86_64)
    my_arch+=' (Rosetta)'
    if [[ -f /usr/local/homebrew/bin/brew ]];
      eval "$(/usr/local/homebrew/bin/brew shellenv)"
      alias brew='/usr/local/homebrew/bin/brew'
    fi
  ;;
  arm64)
    my_arch+=' (Native)'
    if [[ -f /opt/homebrew/bin/brew ]];
      eval "$(/opt/homebrew/bin/brew shellenv)"
      alias brew='/opt/homebrew/bin/brew'
    fi
  ;;
  *)
    my_arch+=' (Unknown)'
  ;;
esac

function prompt_my_arch() {
  p10k segment -b yellow -f red -t ${my_arch//\%/%%}
}

Then add my_arch to POWERLEVEL9K_LEFT_PROMPT_ELEMENTS or POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS in ~/.p10k.zsh.

You can customize how my_arch looks like (colors, icon, etc.) the same way as any other prompt segment (POWERLEVEL9K_MY_ARCH_FOREGROUND, etc.). See p10k help segment for documentation.

P.S.

I haven't tested this code.

@ahgraber
Copy link
Author

ahgraber commented Nov 14, 2021

Thanks much! I had much of it figured out after going back through the readme, but couldn't get it to display. I didn't realize that p10k parsed the prompt_ from the function name away.

For anyone who references this in the future, here's the final addition to my .zshrc:

### Autodetect architecture (and set `brew` path)
if [[ "$(sysctl -a | grep machdep.cpu.brand_string)" == *Apple* ]]; then
  archcheck=$(/usr/bin/arch)
  typeset -g archcheck
  case $archcheck in
    arm64)
      archcheck+=' (Native)'
      if [[ -f /opt/homebrew/bin/brew ]]; then
        eval "$(/opt/homebrew/bin/brew shellenv)"
        alias brew='/opt/homebrew/bin/brew'
      fi
    ;;
    i386|x86_64)
      archcheck+=' (Rosetta)'
      if [[ -f /usr/local/homebrew/bin/brew ]]; then
        eval "$(/usr/local/homebrew/bin/brew shellenv)"
        alias brew='/usr/local/homebrew/bin/brew'
      fi
    ;;
    *)
      archcheck+=' (Unknown)'
    ;;
  esac

  # add arch to p10k
  function prompt_my_arch() {
    p10k segment -f 250 -i '💻' -t "${archcheck//\%/%%}"
  }

  POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS+=my_arch  # to specify location, modify ~/.p10k.zsh
fi

and here's a screengrab of it running:
image

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

2 participants