Skip to content

Commit

Permalink
feat(powerlevel10k): Add Powerlevel10k segment to contrib
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Feb 24, 2024
1 parent d9c42b4 commit fc6bce2
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ for required dependencies.

To generate a Mac app, run [hack/build-app.sh](hack/build-app.sh).
An app will be created in the `dist` directory.

## Contrib

Integrations with external tools are available in the [contrib](contrib) directory.
52 changes: 52 additions & 0 deletions contrib/powerlevel10k/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Powerlevel10k Segment

Adds a custom segment to the [Powerlevel10k](https://github.com/romkatv/powerlevel10k) zsh theme.

![Demo](./screenshot.webp?raw=true)

## Install

### Prerequisites

#### Enable local file writes
1. Open the nightscout-menu-bar menu.
2. Hover over "Preferences".
3. Check "Write to local file".

### Install the Powerlevel10k segment

The Nightscout segment can either be downloaded to a separate file, and sourced from `~/.p10k.zsh`, or its contents can be pasted directly into `~/.p10k.zsh`.

#### Install directly into `~/.p10k.zsh`
This method adds the custom segment's code and configuration directly into `~/.p10k.zsh`. It is easier to set up, but may be harder to update in the future.

<details>
<summary>Click to expand</summary>

1. Copy the contents of the [segment script](nightscout.zsh).
2. Edit `~/.p10k.zsh`.
3. Search for `p10k reload`.
4. Somewhere before this line, paste the segment file contents.
5. Search for `POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS`.
6. Add `nightscout` somewhere in this array, depending on where you would like the widget to be rendered.
7. Open a new shell, or restart your current shell with `exec zsh`.
8. Nightscout data should be rendered as a right-segment!
</details>

#### Install to a separate file
This method places the custom segment's code and configuration in a separate file. It is less standard, but makes it easier to update the segment in the future.

<details>
<summary>Click to expand</summary>

1. Download the [segment script](nightscout.zsh) to a local file. This file will be sourced during Powerlevel10k initialization.
1. For example: `~/.p10k/nightscout.zsh`.
2. Edit `~/.p10k.zsh`.
3. Search for `p10k reload`.
4. Somewhere before this line, source the segment file.
1. For example: `source ~/.p10k/nightscout.zsh`
5. Search for `POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS`.
6. Add `nightscout` somewhere in this array, depending on where you would like the widget to be rendered.
7. Open a new shell, or restart your current shell with `exec zsh`.
8. Nightscout data should be rendered as a right-segment!
</details>
79 changes: 79 additions & 0 deletions contrib/powerlevel10k/nightscout.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#################################[ nightscout: blood sugar ]#################################
# Nightscout state file. Typically does not need to be changed.
typeset -g NIGHTSCOUT_STATE_FILE="$TMPDIR/nightscout.csv"

# Nightscout styling will be chosen if the reading is below a given value.
typeset -g NIGHTSCOUT_THRESHOLD_URGENT_LOW=55
typeset -g NIGHTSCOUT_THRESHOLD_LOW=80
typeset -g NIGHTSCOUT_THRESHOLD_IN_RANGE=160
typeset -g NIGHTSCOUT_THRESHOLD_HIGH=260

# Show/hide Nightscout parts.
typeset -g NIGHTSCOUT_SHOW_ARROW=true
typeset -g NIGHTSCOUT_SHOW_DELTA=true
typeset -g NIGHTSCOUT_SHOW_TIMESTAMP=true
# Can be commented out if timestamp is hidden.
zmodload zsh/datetime

# Nightscout colors.
# Urgent low styling.
typeset -g POWERLEVEL9K_NIGHTSCOUT_URGENT_LOW_BACKGROUND=1
typeset -g POWERLEVEL9K_NIGHTSCOUT_URGENT_LOW_FOREGROUND=7
# Low styling.
typeset -g POWERLEVEL9K_NIGHTSCOUT_LOW_BACKGROUND=1
typeset -g POWERLEVEL9K_NIGHTSCOUT_LOW_FOREGROUND=7
# In range styling.
typeset -g POWERLEVEL9K_NIGHTSCOUT_IN_RANGE_BACKGROUND=2
typeset -g POWERLEVEL9K_NIGHTSCOUT_IN_RANGE_FOREGROUND=0
# High styling.
typeset -g POWERLEVEL9K_NIGHTSCOUT_HIGH_BACKGROUND=3
typeset -g POWERLEVEL9K_NIGHTSCOUT_HIGH_FOREGROUND=0
# Urgent high styling.
typeset -g POWERLEVEL9K_NIGHTSCOUT_URGENT_HIGH_BACKGROUND=1
typeset -g POWERLEVEL9K_NIGHTSCOUT_URGENT_HIGH_FOREGROUND=7
# Custom icon.
# typeset -g POWERLEVEL9K_NIGHTSCOUT_VISUAL_IDENTIFIER_EXPANSION='⭐'

# Creates segment with Nightscout blood sugar data.
#
# Example output: 120 → -1 [1m]
function prompt_nightscout() {
emulate -L zsh

if [[ -s "$NIGHTSCOUT_STATE_FILE" ]]; then
# Read state file into local variables.
typeset bgnow arrow delta timestamp
IFS=, read -r bgnow arrow delta timestamp <"$NIGHTSCOUT_STATE_FILE"

# State file is invalid. Segment will be hidden.
if [[ -z "$bgnow" ]]; then
p10k segment -c ''
return
fi

# Choose current state for styling.
if (( bgnow <= NIGHTSCOUT_THRESHOLD_URGENT_LOW )); then
typeset state=URGENT_LOW
elif (( bgnow < NIGHTSCOUT_THRESHOLD_LOW )); then
typeset state=LOW
elif (( bgnow < NIGHTSCOUT_THRESHOLD_IN_RANGE )); then
typeset state=IN_RANGE
elif (( bgnow < NIGHTSCOUT_THRESHOLD_HIGH )); then
typeset state=HIGH
else
typeset state=URGENT_HIGH
fi

# Generate text
typeset text="$bgnow"
[[ "$NIGHTSCOUT_SHOW_ARROW" == true ]] && text+=" $arrow"
[[ "$NIGHTSCOUT_SHOW_DELTA" == true ]] && text+=" $delta"
[[ "$NIGHTSCOUT_SHOW_TIMESTAMP" == true ]] && text+=" [$(( (EPOCHSECONDS - timestamp + 30) / 60 ))m]"

# Write segment.
p10k segment -s "$state" -i $'\UF058C' -t "$text"
else
# State file does not exist. Segment will be hidden.
p10k segment -c ''
fi
}
Binary file added contrib/powerlevel10k/screenshot.webp
Binary file not shown.

0 comments on commit fc6bce2

Please sign in to comment.