Create Custom Segment on Powerlevel10k

Table of Contents

Create custom Temperature segment on Powerlevel10k on Mac

terminal

As I mentioned when described my tooling in 2024 things change fast, I was just doing my stuff on my laptop when I realized “would be good to have the temperature on my terminal, why no do it now?”

Do we have something already built?

My first thought was to see if powerlevel10k had something already built, it could be the easier and fastest way as we can see on the project read me exists a lot of great options. It was not my original quest here but these options gave me some ideas!!!

To enable it is so simple like open ~~/.p10k.zsh find the segment that you want (left or right) and then uncomment it!!

typeset -g POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(
    # =========================[ Line #1 ]=========================
    status                  # exit code of the last command
    command_execution_time  # duration of the last command
    background_jobs         # presence of background jobs
    direnv                  # direnv status (https://direnv.net/)
    asdf                    # asdf version manager (https://github.com/asdf-vm/asdf)
    virtualenv              # python virtual environment (https://docs.python.org/3/library/venv.html)
    anaconda                # conda environment (https://conda.io/)
    pyenv                   # python environment (https://github.com/pyenv/pyenv)
    goenv                   # go environment (https://github.com/syndbg/goenv)
    nodenv                  # node.js version from nodenv (https://github.com/nodenv/nodenv)
    nvm                     # node.js version from nvm (https://github.com/nvm-sh/nvm)
    nodeenv                 # node.js environment (https://github.com/ekalinin/nodeenv)
    # node_version          # node.js version
    # go_version            # go version (https://golang.org)
    # rust_version          # rustc version (https://www.rust-lang.org)
    # dotnet_version        # .NET version (https://dotnet.microsoft.com)
    # php_version           # php version (https://www.php.net/)
    # laravel_version       # laravel php framework version (https://laravel.com/)
    # java_version          # java version (https://www.java.com/)
    # package               # name@version from package.json (https://docs.npmjs.com/files/package.json)
    rbenv                   # ruby version from rbenv (https://github.com/rbenv/rbenv)
    rvm                     # ruby version from rvm (https://rvm.io)
    fvm                     # flutter version management (https://github.com/leoafarias/fvm)
    luaenv                  # lua version from luaenv (https://github.com/cehoffman/luaenv)
    jenv                    # java version from jenv (https://github.com/jenv/jenv)
    plenv                   # perl version from plenv (https://github.com/tokuhirom/plenv)
    perlbrew                # perl version from perlbrew (https://github.com/gugod/App-perlbrew)
    phpenv                  # php version from phpenv (https://github.com/phpenv/phpenv)
    scalaenv                # scala version from scalaenv (https://github.com/scalaenv/scalaenv)
    haskell_stack           # haskell version from stack (https://haskellstack.org/)
    kubecontext             # current kubernetes context (https://kubernetes.io/)
    terraform               # terraform workspace (https://www.terraform.io)
    # terraform_version     # terraform version (https://www.terraform.io)
    aws                     # aws profile (https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html)
    aws_eb_env              # aws elastic beanstalk environment (https://aws.amazon.com/elasticbeanstalk/)
    azure                   # azure account name (https://docs.microsoft.com/en-us/cli/azure)
    gcloud                  # google cloud cli account and project (https://cloud.google.com/)
    google_app_cred         # google application credentials (https://cloud.google.com/docs/authentication/production)
    toolbox                 # toolbox name (https://github.com/containers/toolbox)
    context                 # user@hostname
    nordvpn                 # nordvpn connection status, linux only (https://nordvpn.com/)
    ranger                  # ranger shell (https://github.com/ranger/ranger)
    yazi                    # yazi shell (https://github.com/sxyazi/yazi)
    nnn                     # nnn shell (https://github.com/jarun/nnn)
    lf                      # lf shell (https://github.com/gokcehan/lf)
    xplr                    # xplr shell (https://github.com/sayanarijit/xplr)
    vim_shell               # vim shell indicator (:sh)
    midnight_commander      # midnight commander shell (https://midnight-commander.org/)
    nix_shell               # nix shell (https://nixos.org/nixos/nix-pills/developing-with-nix-shell.html)
    chezmoi_shell           # chezmoi shell (https://www.chezmoi.io/)
    # vi_mode               # vi mode (you don't need this if you've enabled prompt_char)
    # vpn_ip                # virtual private network indicator
    load                    # CPU load
    disk_usage              # disk usage
    # ram                   # free RAM
    # swap                  # used swap
    todo                    # todo items (https://github.com/todotxt/todo.txt-cli)
    timewarrior             # timewarrior tracking status (https://timewarrior.net/)
    taskwarrior             # taskwarrior task count (https://taskwarrior.org/)
    per_directory_history   # Oh My Zsh per-directory-history local/global indicator
    # cpu_arch              # CPU architecture
    # time                  # current time
    #battery               # internal battery
    # =========================[ Line #2 ]=========================
    newline
    #wifi                  # wifi speed
    # ip                    # ip address and bandwidth usage for a specified network interface
    # public_ip             # public IP address
    # proxy                 # system-wide http/https/ftp proxy
    #example               # example user-defined segment (see prompt_example function below)
  )

In my case, I added disk_usage, load, and ram as some useful information for my day-to-day coding.

Now, you are probably asking yourself, what about the temperature? As we can see there is no pre-made segment but the extensibility is also mentioned on read-me

If there is no prompt segment that does what you need, implement your own. Powerlevel10k provides public API for defining segments that are as fast and as flexible as built-in ones.

On Linux you can fetch the current CPU temperature by reading /sys/class/thermal/thermal_zone0/temp. The screencast shows how to define a prompt segment to display this value. Once the segment is defined, you can use it like any other segment. All standard customization parameters will work for it out of the box.

Type the p10k help segment for reference.

Note: If you modify POWERLEVEL9K_* parameters in an already initialized interactive shell (as opposed to editing ~/.p10k.zsh), the changes might not be immediately effective. To apply the modifications, invoke p10k reload. Setting POWERLEVEL9K_DISABLE_HOT_RELOAD=false eliminates the necessity for p10k reload but results in a marginally slower prompt.

Ok ok, How can I get Mac temperature?

The first question was, how can I get the current mac temperature? My first requisite was that is not need to be the most accurate temperature since I just want something informative.

After some googling, I found this on stack exchange I tried to great options powermetrics and oxs-cpu-temp

What is powermetrics? based on the page of powermetrics we can describe:

powermetrics gathers and displays CPU usage statistics (divided into time spent in user mode and supervisor mode), timer and interrupt wakeup frequency (total and, for near-idle workloads, those that resulted in package idle exits), and on supported platforms, interrupt frequencies (categorized by CPU number), package C-state statistics (an indication of the time the core complex + integrated graphics, if any, were in low-power idle states), as well as the average execution frequency for each CPU when not idle.

After executing sudo powermetrics --samplers smc |grep -i "CPU die temperature" I got:

CPU die temperature: 62.57 C
CPU die temperature: 62.20 C

Great it worked and to get the current temperature I used sudo powermetrics --samplers smc -i1 -n1 | grep -i "CPU die temperature"

But powermetrics need to be invoked as superuser and I don’t want it on powerlevel10k config, so let’s see the second the oxs-cpu-temp, an open source cli tool and also has a brew formula, fist installed with brew install osx-cpu-temp and then executing:

 osx-cpu-temp
53.8°

It was exactly the expected output, now let’s add it as a segment!

Creating my segment

The first step was to understand how segment works, this was the easiest part because powerlevel10k has an already prepared sample, that I uncommented to validate my idea.

So I had to created my own function

function prompt_my_cpu_temp() {
    temp=$(osx-cpu-temp)
     p10k segment -f 007 -i '🌡️ ' -t "${temp}"
  }

The final step as add a segment, the same as the previous pre-made ones. Finally, my terminal was customized as the first image of this post.

If you read this, see you soon!