-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(zsh): change prompt {playtime => century}
- Loading branch information
Showing
6 changed files
with
199 additions
and
202 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
# File: prompt_century_setup | ||
# Author: Zakhary Kaplan <https://zakhary.dev> | ||
# Created: 11 Feb 2024 | ||
# SPDX-License-Identifier: MIT | ||
# Vim: set ft=zsh: | ||
|
||
# ---------------- | ||
# -- Modules -- | ||
# ---------------- | ||
|
||
zmodload zsh/datetime | ||
|
||
|
||
# ---------------- | ||
# -- Global -- | ||
# ---------------- | ||
|
||
# Create a hash table for globally stashing variables without polluting main | ||
# scope with a bunch of identifiers. | ||
typeset -gA CENTURY | ||
|
||
# Primary | ||
CENTURY[RED]=167 | ||
CENTURY[GRN]=107 | ||
CENTURY[BLU]=025 | ||
CENTURY[CYN]=110 | ||
CENTURY[MGA]=183 | ||
CENTURY[YLW]=222 | ||
# Secondary | ||
CENTURY[GR2]=071 | ||
CENTURY[YL2]=215 | ||
# Greys | ||
CENTURY[BW1]=239 | ||
CENTURY[BW2]=242 | ||
|
||
|
||
# ---------------- | ||
# -- Help -- | ||
# ---------------- | ||
|
||
prompt_century_help () { | ||
cat <<EOF | ||
Usage: prompt century [OPTIONS] | ||
Options: | ||
-1 Render as one line | ||
-2 Render as two lines | ||
EOF | ||
} | ||
|
||
|
||
|
||
# ---------------- | ||
# -- Prompt -- | ||
# ---------------- | ||
|
||
function prompt_century_setup() { | ||
# Parse opts | ||
local nlines=2 | ||
while getopts "12" opt; do | ||
case $opt in | ||
1) | ||
nlines=1 | ||
;; | ||
2) | ||
nlines=2 | ||
;; | ||
esac | ||
done | ||
|
||
# Autoloads | ||
local -A schars | ||
autoload -Uz prompt_special_chars | ||
prompt_special_chars | ||
|
||
# Blocks | ||
local CLK="%b%F{$CENTURY[BW2]}[%D{%X}]" | ||
local CTX="%b%F{$CENTURY[YLW]}%_" | ||
local CWD="%B%F{$CENTURY[BLU]}%2~" | ||
local GIT='$CENTURY[GIT] ' | ||
local JOB="%b%F{$CENTURY[YLW]}%(1j.%j! .)" | ||
local RET="%b%F{$CENTURY[RED]}%(0?..%? )" | ||
local SYM="%B%F{%(0?.$CENTURY[GRN].$CENTURY[RED])}%(!.#.❯) " | ||
local TMR='$CENTURY[TMR] ' | ||
local USR="%b%F{$CENTURY[CYN]}%n${SSH_TTY:+@%m} " | ||
|
||
# Special | ||
local TOP="%F{$CENTURY[BW1]}┏ " | ||
local PLN="$prompt_newline" | ||
local BTM="%F{$CENTURY[BW1]}┗ " | ||
local RST="%b%f%k" | ||
|
||
# Prompts | ||
PS1= | ||
PS2= | ||
case $nlines in | ||
1) | ||
PS1+="${USR}${JOB}${RET}${SYM}${RST}" | ||
RPS1="${TMR}${CWD}${GIT}${CLK}${RST}" | ||
;; | ||
2) | ||
PS1+="${TOP}${CWD}${GIT}${PLN}" | ||
PS1+="${BTM}${USR}${JOB}${RET}${SYM}${RST}" | ||
RPS1="${TMR}${CLK}${RST}" | ||
;; | ||
esac | ||
PS2+="${CTX} » ${RST}" | ||
ZLE_RPROMPT_INDENT=0 | ||
} | ||
|
||
|
||
# ---------------- | ||
# -- Hooks -- | ||
# ---------------- | ||
|
||
function hook-preexec-timer() { | ||
CENTURY[ERT]=$EPOCHREALTIME | ||
} | ||
add-zsh-hook preexec hook-preexec-timer | ||
|
||
function hook-precmd-timer() { | ||
let local timer="$EPOCHREALTIME-${CENTURY[ERT]:-$EPOCHREALTIME}" | ||
local tmr | ||
(( $timer > 0 )) && { | ||
tmr="$(printf '%.2fs' $timer)" | ||
CENTURY[ERT]= | ||
} | ||
|
||
CENTURY[TMR]="%b%F{$CENTURY[BW1]}${tmr}" | ||
} | ||
add-zsh-hook precmd hook-precmd-timer | ||
|
||
function hook-preexec-git() { | ||
CENTURY[GIT]= | ||
} | ||
add-zsh-hook preexec hook-preexec-git | ||
|
||
function hook-precmd-git() { | ||
git rev-parse --is-inside-work-tree &> /dev/null || return | ||
|
||
local dry="$(git status --porcelain)" | ||
local ref="$(git symbolic-ref -q --short HEAD)" \ | ||
|| ref="$(git rev-parse --short HEAD)" \ | ||
|| return | ||
|
||
# Blocks | ||
local DRY="%b%F{$CENTURY[YL2]}${dry:+*}" | ||
local REF="%b%F{$CENTURY[GR2]}${ref}" | ||
|
||
# Special | ||
local OPN="%b%F{$CENTURY[BLU]}(" | ||
local CLS="%b%F{$CENTURY[BLU]})" | ||
|
||
CENTURY[GIT]="${OPN}${REF}${DRY}${CLS}" | ||
} | ||
add-zsh-hook precmd hook-precmd-git | ||
|
||
|
||
# ---------------- | ||
# -- Setup -- | ||
# ---------------- | ||
|
||
prompt_century_setup "$@" |
Oops, something went wrong.