From dc4fbe00347559f4e3851b247451d58ba190b575 Mon Sep 17 00:00:00 2001 From: Yarden Sod-Moriah Date: Fri, 9 Jun 2017 15:28:51 +0300 Subject: [PATCH] Update README, add support for promptinit, fix for #2 --- README.md | 12 ++--- blox.zsh | 108 ++++++++++++++++++++++++++++-------------- src/blocks/bgjobs.zsh | 2 +- src/blocks/cwd.zsh | 2 +- src/blocks/git.zsh | 31 ++++++++---- src/blocks/host.zsh | 4 +- src/blocks/nodejs.zsh | 2 +- src/blocks/symbol.zsh | 4 +- src/main.zsh | 63 +++++++++++++++++------- 9 files changed, 152 insertions(+), 76 deletions(-) diff --git a/README.md b/README.md index 09abd49..88b0dc8 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,8 @@ ![](./.github/terminal.png) Blox is a minimal and fast ZSH theme that shows you what you need. It consists of blocks, and you can play with the order and change everything; it comes with some pre-defined blocks, but you can create your own or even modify them. +You can consider Blox as a "framework", since you can do whatever you want with it. + --- ## Install @@ -21,16 +23,12 @@ antigen bundle yardnsm/blox-zsh-theme ### For zgen users -Similarly to antigen, just add the following to your `~/.zshrc`: - ```console zgen load yardnsm/blox-zsh-theme ``` ### For antibody users -Again, just add the following to your `~/.zshrc`: - ```console antibody bundle yardnsm/blox-zsh-theme ``` @@ -46,7 +44,7 @@ antibody bundle yardnsm/blox-zsh-theme - Clone this repo - ... or add it as a submodule. - Just download `blox.zsh`. -2. `source` it. Simply put the following in your `~/.zshrc`: +2. `source` it in your `~/.zshrc`: ```console source PATH_FOR_BLOX.zsh ``` @@ -111,6 +109,8 @@ Shows git info. | Variable | Default | Meaning | | :------- | :------ | :------ | +| `BLOX_BLOCK__GIT_BRANCH_COLOR` | `'242' (grey)` | Branch color | +| `BLOX_BLOCK__GIT_COMMIT_COLOR` | `'magenta'` | Commit hash color | | `BLOX_BLOCK__GIT_CLEAN_COLOR` | `'green'` | Clean color | | `BLOX_BLOCK__GIT_CLEAN_SYMBOL` | `'✔︎'` | Clean symbol | | `BLOX_BLOCK__GIT_DIRTY_COLOR` | `'red'` | Dirty color | @@ -170,7 +170,7 @@ function blox_block__helloworld() { } ``` -:warning: **Note:** there may be some spacing issues in the upper segments when using multiline mode. To solve this, try wrapping variables (e.g. `$fg[...]`) within a [percent-braces construct](http://unix.stackexchange.com/a/90876) (`%{…%}`); +:warning: **Note:** there may be some spacing issues in the upper segments when using multiline mode. To solve this, try wrapping variables (especially color variables, e.g. `$fg[...]`) within `%{…%}` (see [this link](http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html#Visual-effects) for more details). ### Setting the structure diff --git a/blox.zsh b/blox.zsh index 9d5a20e..c2450d5 100644 --- a/blox.zsh +++ b/blox.zsh @@ -21,7 +21,7 @@ function blox_block__bgjobs() { res="" if [[ ! $bgjobs == "0" ]]; then - res+="%{$fg[${BLOX_BLOCK__BGJOBS_COLOR}]%}" + res+="%F{${BLOX_BLOCK__BGJOBS_COLOR}}" res+="${BLOX_CONF__BLOCK_PREFIX}${BLOX_BLOCK__BGJOBS_SYMBOL}${bgjobs}${BLOX_CONF__BLOCK_SUFFIX}"; res+="%{$reset_color%}" fi @@ -37,7 +37,7 @@ BLOX_BLOCK__CWD_TRUNC="${BLOX_BLOCK__CWD_TRUNC:-3}" # --------------------------------------------- function blox_block__cwd() { - res="%{$fg_bold[${BLOX_BLOCK__CWD_COLOR}]%}" + res="%F{${BLOX_BLOCK__CWD_COLOR}}" res+="%${BLOX_BLOCK__CWD_TRUNC}~"; res+="%{$reset_color%}" @@ -46,9 +46,13 @@ function blox_block__cwd() { # --------------------------------------------- # Git block options +# Colors +BLOX_BLOCK__GIT_BRANCH_COLOR="${BLOX_BLOCK__GIT_BRANCH_COLOR:-242}" +BLOX_BLOCK__GIT_COMMIT_COLOR="${BLOX_BLOCK__GIT_COMMIT_COLOR:-magenta}" + # Clean BLOX_BLOCK__GIT_CLEAN_COLOR="${BLOX_BLOCK__GIT_CLEAN_COLOR:-green}" -BLOX_BLOCK__GIT_CLEAN_SYMBOL="${BLOX_BLOCK__GIT_CLEAN_SYMBOL:-✔︎%{ %}}" +BLOX_BLOCK__GIT_CLEAN_SYMBOL="${BLOX_BLOCK__GIT_CLEAN_SYMBOL:-✔}" # Dirty BLOX_BLOCK__GIT_DIRTY_COLOR="${BLOX_BLOCK__GIT_DIRTY_COLOR:-red}" @@ -65,10 +69,10 @@ BLOX_BLOCK__GIT_UNPUSHED_SYMBOL="${BLOX_BLOCK__GIT_UNPUSHED_SYMBOL:-⇡}" # --------------------------------------------- # Themes -BLOX_BLOCK__GIT_THEME_CLEAN="%{$fg[${BLOX_BLOCK__GIT_CLEAN_COLOR}]%}$BLOX_BLOCK__GIT_CLEAN_SYMBOL%{$reset_color%}" -BLOX_BLOCK__GIT_THEME_DIRTY="%{$fg[${BLOX_BLOCK__GIT_DIRTY_COLOR}]%}$BLOX_BLOCK__GIT_DIRTY_SYMBOL%{$reset_color%}" -BLOX_BLOCK__GIT_THEME_UNPULLED="%{$fg[${BLOX_BLOCK__GIT_UNPULLED_COLOR}]%}$BLOX_BLOCK__GIT_UNPULLED_SYMBOL%{$reset_color%}" -BLOX_BLOCK__GIT_THEME_UNPUSHED="%{$fg[${BLOX_BLOCK__GIT_UNPUSHED_COLOR}]%}$BLOX_BLOCK__GIT_UNPUSHED_SYMBOL%{$reset_color%}" +BLOX_BLOCK__GIT_THEME_CLEAN="%F{${BLOX_BLOCK__GIT_CLEAN_COLOR}]%}$BLOX_BLOCK__GIT_CLEAN_SYMBOL%{$reset_color%}" +BLOX_BLOCK__GIT_THEME_DIRTY="%F{${BLOX_BLOCK__GIT_DIRTY_COLOR}]%}$BLOX_BLOCK__GIT_DIRTY_SYMBOL%{$reset_color%}" +BLOX_BLOCK__GIT_THEME_UNPULLED="%F{${BLOX_BLOCK__GIT_UNPULLED_COLOR}]%}$BLOX_BLOCK__GIT_UNPULLED_SYMBOL%{$reset_color%}" +BLOX_BLOCK__GIT_THEME_UNPUSHED="%F{${BLOX_BLOCK__GIT_UNPUSHED_COLOR}]%}$BLOX_BLOCK__GIT_UNPUSHED_SYMBOL%{$reset_color%}" # --------------------------------------------- # Helper functions @@ -78,7 +82,7 @@ function blox_block__git_helper__commit() { echo $(command git rev-parse --short HEAD 2> /dev/null) } -# Get the current branch +# Get the current branch name function blox_block__git_helper__branch() { ref=$(command git symbolic-ref HEAD 2> /dev/null) || \ ref=$(command git rev-parse --short HEAD 2> /dev/null) || return 0 @@ -129,12 +133,19 @@ function blox_block__git() { if blox_block__git_helper__is_git_repo; then - local branch="%F{242}$(blox_block__git_helper__branch)%{$reset_color%}" - local remote="$(blox_block__git_helper__remote_status)" - local commit="%{$fg[magenta]%}[$(blox_block__git_helper__commit)]%{$reset_color%}" + local branch="$(blox_block__git_helper__branch)" + local commit="$(blox_block__git_helper__commit)" local b_status="$(blox_block__git_helper__status)" + local remote="$(blox_block__git_helper__remote_status)" - echo "${branch}${commit} ${b_status}${remote}" + res="" + + res+="%F{${BLOX_BLOCK__GIT_BRANCH_COLOR}}${branch}%{$reset_color%}" + res+="%F{${BLOX_BLOCK__GIT_COMMIT_COLOR}}${BLOX_CONF__BLOCK_PREFIX}${commit}${BLOX_CONF__BLOCK_SUFFIX}%{$reset_color%} " + res+="${b_status}" + res+="${remote}" + + echo $res fi } # --------------------------------------------- @@ -160,13 +171,13 @@ function blox_block__host() { # Check if the user info is needed if [[ $BLOX_BLOCK__HOST_USER_SHOW_ALWAYS == true ]] || [[ $(who am i | awk '{print $1}') != $USER ]]; then - info+="%{$fg[$USER_COLOR]%}%n%{$reset_color%}" + info+="%F{$USER_COLOR]%}%n%{$reset_color%}" fi # Check if the machine name is needed if [[ $BLOX_BLOCK__HOST_MACHINE_SHOW_ALWAYS == true ]] || [[ -n $SSH_CONNECTION ]]; then [[ $info != "" ]] && info+="@" - info+="%{$fg[${BLOX_BLOCK__HOST_MACHINE_COLOR}]%}%m%{$reset_color%}" + info+="%F{${BLOX_BLOCK__HOST_MACHINE_COLOR}]%}%m%{$reset_color%}" fi if [[ $info != "" ]]; then @@ -189,7 +200,7 @@ function blox_block__nodejs() { res="" if [[ ! -z "${node_version}" ]]; then - res+="%{$fg[${BLOX_BLOCK__NODEJS_COLOR}]%}" + res+="%F{${BLOX_BLOCK__NODEJS_COLOR}}" res+="${BLOX_CONF__BLOCK_PREFIX}${BLOX_BLOCK__NODEJS_SYMBOL} ${node_version:1}${BLOX_CONF__BLOCK_SUFFIX}" res+="%{$reset_color%}" fi @@ -211,8 +222,8 @@ BLOX_BLOCK__SYMBOL_ALTERNATE="${BLOX_BLOCK__SYMBOL_ALTERNATE:-◇}" # --------------------------------------------- function blox_block__symbol() { - res="%{$fg[${BLOX_BLOCK__SYMBOL_COLOR}]%}" - res+="%(?.$BLOX_BLOCK__SYMBOL_SYMBOL.%{$fg[$BLOX_BLOCK__SYMBOL_EXIT_COLOR]%}$BLOX_BLOCK__SYMBOL_EXIT_SYMBOL)"; + res="%F{${BLOX_BLOCK__SYMBOL_COLOR}}" + res+="%(?.$BLOX_BLOCK__SYMBOL_SYMBOL.%F{$BLOX_BLOCK__SYMBOL_EXIT_COLOR}$BLOX_BLOCK__SYMBOL_EXIT_SYMBOL)"; res+="%{$reset_color%}" echo $res } @@ -225,18 +236,9 @@ function blox_block__time() { # --------------------------------------------- # Initialize stuff -# Enable command substitution in prompt -setopt prompt_subst - # Initialize prompt autoload -Uz promptinit && promptinit -# Initialize colors -autoload -Uz colors && colors - -# Hooks -autoload -U add-zsh-hook - # --------------------------------------------- # Core options @@ -256,7 +258,7 @@ BLOX_CHAR__NEWLINE=" BLOX_SEG_DEFAULT__UPPER_LEFT=(blox_block__host blox_block__cwd blox_block__git) BLOX_SEG_DEFAULT__UPPER_RIGHT=(blox_block__bgjobs blox_block__nodejs blox_block__time) BLOX_SEG_DEFAULT__LOWER_LEFT=(blox_block__symbol) -BLOX_SEG_DEFAULT__LOWER_RIGHT=() +BLOX_SEG_DEFAULT__LOWER_RIGHT=( ) # Upper BLOX_SEG__UPPER_LEFT=${BLOX_SEG__UPPER_LEFT:-$BLOX_SEG_DEFAULT__UPPER_LEFT} @@ -307,6 +309,9 @@ function blox_helper__calculate_spaces() { left=${#${(S%%)left//$~zero/}} right=${#${(S%%)right//$~zero/}} + # We don't need spaces if there nothing on the right + [[ $right -le 1 ]] && echo && return 0 + # Desired spaces length local termwidth (( termwidth = ${COLUMNS} - ${left} - ${right} )) @@ -345,7 +350,7 @@ function blox_hook__build_prompt() { spacing="$(blox_helper__calculate_spaces ${upper_left} ${upper_right})" # Should we add a newline? - [[ $BLOX_CONF__NEWLINE == false ]] && BLOX_CHAR__NEWLINE="" + [[ $BLOX_CONF__NEWLINE != false ]] && print "" # In oneline mode, we set $PROMPT to the # upper left segment and $RPROMPT to the upper @@ -358,14 +363,14 @@ function blox_hook__build_prompt() { if [[ $BLOX_CONF__ONELINE == true ]]; then # Setting only the upper segments - PROMPT='${BLOX_CHAR__NEWLINE}${upper_left} ' + PROMPT='${upper_left} ' # Right segment RPROMPT='${upper_right}' else # The prompt - PROMPT='${BLOX_CHAR__NEWLINE}${upper_left}${spacing}${upper_right} + PROMPT='%{${upper_left}%}${spacing}%{${upper_right}%} ${lower_left} ' # Right prompt @@ -377,10 +382,43 @@ ${lower_left} ' } # --------------------------------------------- -# Setup hooks +# Setup -# Build the prompt -add-zsh-hook precmd blox_hook__build_prompt +prompt_blox_help() { + cat <<'EOF' +Blox is a minimal and fast ZSH theme that shows you what you need. It consists of blocks, +and you can play with the order and change everything; it comes with some +pre-defined blocks, but you can create your own or even modify them. + +You can consider Blox as a "framework", since you can do whatever you want with it. + +Configuration: + +See: https://github.com/yardnsm/blox-zsh-theme + +You can invoke it thus: + + prompt blox + +EOF +} + +prompt_blox_setup() { + setopt prompt_subst + + autoload -Uz colors && colors + autoload -U add-zsh-hook + autoload -Uz vcs_info + + add-zsh-hook precmd blox_hook__build_prompt + add-zsh-hook precmd blox_hook__title + + return 0 +} + +prompt_blox_preview () { + local +h PS1='%# ' + prompt_preview_theme blox "$@" +} -# Set title -add-zsh-hook precmd blox_hook__title +prompt_blox_setup "$@" \ No newline at end of file diff --git a/src/blocks/bgjobs.zsh b/src/blocks/bgjobs.zsh index ec6a175..644aab0 100644 --- a/src/blocks/bgjobs.zsh +++ b/src/blocks/bgjobs.zsh @@ -11,7 +11,7 @@ function blox_block__bgjobs() { res="" if [[ ! $bgjobs == "0" ]]; then - res+="%{$fg[${BLOX_BLOCK__BGJOBS_COLOR}]%}" + res+="%F{${BLOX_BLOCK__BGJOBS_COLOR}}" res+="${BLOX_CONF__BLOCK_PREFIX}${BLOX_BLOCK__BGJOBS_SYMBOL}${bgjobs}${BLOX_CONF__BLOCK_SUFFIX}"; res+="%{$reset_color%}" fi diff --git a/src/blocks/cwd.zsh b/src/blocks/cwd.zsh index 82dbba2..5389fcf 100644 --- a/src/blocks/cwd.zsh +++ b/src/blocks/cwd.zsh @@ -7,7 +7,7 @@ BLOX_BLOCK__CWD_TRUNC="${BLOX_BLOCK__CWD_TRUNC:-3}" # --------------------------------------------- function blox_block__cwd() { - res="%{$fg_bold[${BLOX_BLOCK__CWD_COLOR}]%}" + res="%F{${BLOX_BLOCK__CWD_COLOR}}" res+="%${BLOX_BLOCK__CWD_TRUNC}~"; res+="%{$reset_color%}" diff --git a/src/blocks/git.zsh b/src/blocks/git.zsh index e560ae8..ecda5b3 100644 --- a/src/blocks/git.zsh +++ b/src/blocks/git.zsh @@ -1,9 +1,13 @@ # --------------------------------------------- # Git block options +# Colors +BLOX_BLOCK__GIT_BRANCH_COLOR="${BLOX_BLOCK__GIT_BRANCH_COLOR:-242}" +BLOX_BLOCK__GIT_COMMIT_COLOR="${BLOX_BLOCK__GIT_COMMIT_COLOR:-magenta}" + # Clean BLOX_BLOCK__GIT_CLEAN_COLOR="${BLOX_BLOCK__GIT_CLEAN_COLOR:-green}" -BLOX_BLOCK__GIT_CLEAN_SYMBOL="${BLOX_BLOCK__GIT_CLEAN_SYMBOL:-✔︎%{ %}}" +BLOX_BLOCK__GIT_CLEAN_SYMBOL="${BLOX_BLOCK__GIT_CLEAN_SYMBOL:-✔}" # Dirty BLOX_BLOCK__GIT_DIRTY_COLOR="${BLOX_BLOCK__GIT_DIRTY_COLOR:-red}" @@ -20,10 +24,10 @@ BLOX_BLOCK__GIT_UNPUSHED_SYMBOL="${BLOX_BLOCK__GIT_UNPUSHED_SYMBOL:-⇡}" # --------------------------------------------- # Themes -BLOX_BLOCK__GIT_THEME_CLEAN="%{$fg[${BLOX_BLOCK__GIT_CLEAN_COLOR}]%}$BLOX_BLOCK__GIT_CLEAN_SYMBOL%{$reset_color%}" -BLOX_BLOCK__GIT_THEME_DIRTY="%{$fg[${BLOX_BLOCK__GIT_DIRTY_COLOR}]%}$BLOX_BLOCK__GIT_DIRTY_SYMBOL%{$reset_color%}" -BLOX_BLOCK__GIT_THEME_UNPULLED="%{$fg[${BLOX_BLOCK__GIT_UNPULLED_COLOR}]%}$BLOX_BLOCK__GIT_UNPULLED_SYMBOL%{$reset_color%}" -BLOX_BLOCK__GIT_THEME_UNPUSHED="%{$fg[${BLOX_BLOCK__GIT_UNPUSHED_COLOR}]%}$BLOX_BLOCK__GIT_UNPUSHED_SYMBOL%{$reset_color%}" +BLOX_BLOCK__GIT_THEME_CLEAN="%F{${BLOX_BLOCK__GIT_CLEAN_COLOR}]%}$BLOX_BLOCK__GIT_CLEAN_SYMBOL%{$reset_color%}" +BLOX_BLOCK__GIT_THEME_DIRTY="%F{${BLOX_BLOCK__GIT_DIRTY_COLOR}]%}$BLOX_BLOCK__GIT_DIRTY_SYMBOL%{$reset_color%}" +BLOX_BLOCK__GIT_THEME_UNPULLED="%F{${BLOX_BLOCK__GIT_UNPULLED_COLOR}]%}$BLOX_BLOCK__GIT_UNPULLED_SYMBOL%{$reset_color%}" +BLOX_BLOCK__GIT_THEME_UNPUSHED="%F{${BLOX_BLOCK__GIT_UNPUSHED_COLOR}]%}$BLOX_BLOCK__GIT_UNPUSHED_SYMBOL%{$reset_color%}" # --------------------------------------------- # Helper functions @@ -33,7 +37,7 @@ function blox_block__git_helper__commit() { echo $(command git rev-parse --short HEAD 2> /dev/null) } -# Get the current branch +# Get the current branch name function blox_block__git_helper__branch() { ref=$(command git symbolic-ref HEAD 2> /dev/null) || \ ref=$(command git rev-parse --short HEAD 2> /dev/null) || return 0 @@ -84,11 +88,18 @@ function blox_block__git() { if blox_block__git_helper__is_git_repo; then - local branch="%F{242}$(blox_block__git_helper__branch)%{$reset_color%}" - local remote="$(blox_block__git_helper__remote_status)" - local commit="%{$fg[magenta]%}[$(blox_block__git_helper__commit)]%{$reset_color%}" + local branch="$(blox_block__git_helper__branch)" + local commit="$(blox_block__git_helper__commit)" local b_status="$(blox_block__git_helper__status)" + local remote="$(blox_block__git_helper__remote_status)" + + res="" + + res+="%F{${BLOX_BLOCK__GIT_BRANCH_COLOR}}${branch}%{$reset_color%}" + res+="%F{${BLOX_BLOCK__GIT_COMMIT_COLOR}}${BLOX_CONF__BLOCK_PREFIX}${commit}${BLOX_CONF__BLOCK_SUFFIX}%{$reset_color%} " + res+="${b_status}" + res+="${remote}" - echo "${branch}${commit} ${b_status}${remote}" + echo $res fi } diff --git a/src/blocks/host.zsh b/src/blocks/host.zsh index 0bda85f..67491e2 100644 --- a/src/blocks/host.zsh +++ b/src/blocks/host.zsh @@ -21,13 +21,13 @@ function blox_block__host() { # Check if the user info is needed if [[ $BLOX_BLOCK__HOST_USER_SHOW_ALWAYS == true ]] || [[ $(who am i | awk '{print $1}') != $USER ]]; then - info+="%{$fg[$USER_COLOR]%}%n%{$reset_color%}" + info+="%F{$USER_COLOR]%}%n%{$reset_color%}" fi # Check if the machine name is needed if [[ $BLOX_BLOCK__HOST_MACHINE_SHOW_ALWAYS == true ]] || [[ -n $SSH_CONNECTION ]]; then [[ $info != "" ]] && info+="@" - info+="%{$fg[${BLOX_BLOCK__HOST_MACHINE_COLOR}]%}%m%{$reset_color%}" + info+="%F{${BLOX_BLOCK__HOST_MACHINE_COLOR}]%}%m%{$reset_color%}" fi if [[ $info != "" ]]; then diff --git a/src/blocks/nodejs.zsh b/src/blocks/nodejs.zsh index cadb121..c941289 100644 --- a/src/blocks/nodejs.zsh +++ b/src/blocks/nodejs.zsh @@ -14,7 +14,7 @@ function blox_block__nodejs() { res="" if [[ ! -z "${node_version}" ]]; then - res+="%{$fg[${BLOX_BLOCK__NODEJS_COLOR}]%}" + res+="%F{${BLOX_BLOCK__NODEJS_COLOR}}" res+="${BLOX_CONF__BLOCK_PREFIX}${BLOX_BLOCK__NODEJS_SYMBOL} ${node_version:1}${BLOX_CONF__BLOCK_SUFFIX}" res+="%{$reset_color%}" fi diff --git a/src/blocks/symbol.zsh b/src/blocks/symbol.zsh index 32bbdca..363c91c 100644 --- a/src/blocks/symbol.zsh +++ b/src/blocks/symbol.zsh @@ -13,8 +13,8 @@ BLOX_BLOCK__SYMBOL_ALTERNATE="${BLOX_BLOCK__SYMBOL_ALTERNATE:-◇}" # --------------------------------------------- function blox_block__symbol() { - res="%{$fg[${BLOX_BLOCK__SYMBOL_COLOR}]%}" - res+="%(?.$BLOX_BLOCK__SYMBOL_SYMBOL.%{$fg[$BLOX_BLOCK__SYMBOL_EXIT_COLOR]%}$BLOX_BLOCK__SYMBOL_EXIT_SYMBOL)"; + res="%F{${BLOX_BLOCK__SYMBOL_COLOR}}" + res+="%(?.$BLOX_BLOCK__SYMBOL_SYMBOL.%F{$BLOX_BLOCK__SYMBOL_EXIT_COLOR}$BLOX_BLOCK__SYMBOL_EXIT_SYMBOL)"; res+="%{$reset_color%}" echo $res } diff --git a/src/main.zsh b/src/main.zsh index 69f6662..1a87bb3 100644 --- a/src/main.zsh +++ b/src/main.zsh @@ -1,18 +1,9 @@ # --------------------------------------------- # Initialize stuff -# Enable command substitution in prompt -setopt prompt_subst - # Initialize prompt autoload -Uz promptinit && promptinit -# Initialize colors -autoload -Uz colors && colors - -# Hooks -autoload -U add-zsh-hook - # --------------------------------------------- # Core options @@ -32,7 +23,7 @@ BLOX_CHAR__NEWLINE=" BLOX_SEG_DEFAULT__UPPER_LEFT=(blox_block__host blox_block__cwd blox_block__git) BLOX_SEG_DEFAULT__UPPER_RIGHT=(blox_block__bgjobs blox_block__nodejs blox_block__time) BLOX_SEG_DEFAULT__LOWER_LEFT=(blox_block__symbol) -BLOX_SEG_DEFAULT__LOWER_RIGHT=() +BLOX_SEG_DEFAULT__LOWER_RIGHT=( ) # Upper BLOX_SEG__UPPER_LEFT=${BLOX_SEG__UPPER_LEFT:-$BLOX_SEG_DEFAULT__UPPER_LEFT} @@ -83,6 +74,9 @@ function blox_helper__calculate_spaces() { left=${#${(S%%)left//$~zero/}} right=${#${(S%%)right//$~zero/}} + # We don't need spaces if there nothing on the right + [[ $right -le 1 ]] && echo && return 0 + # Desired spaces length local termwidth (( termwidth = ${COLUMNS} - ${left} - ${right} )) @@ -121,7 +115,7 @@ function blox_hook__build_prompt() { spacing="$(blox_helper__calculate_spaces ${upper_left} ${upper_right})" # Should we add a newline? - [[ $BLOX_CONF__NEWLINE == false ]] && BLOX_CHAR__NEWLINE="" + [[ $BLOX_CONF__NEWLINE != false ]] && print "" # In oneline mode, we set $PROMPT to the # upper left segment and $RPROMPT to the upper @@ -134,14 +128,14 @@ function blox_hook__build_prompt() { if [[ $BLOX_CONF__ONELINE == true ]]; then # Setting only the upper segments - PROMPT='${BLOX_CHAR__NEWLINE}${upper_left} ' + PROMPT='${upper_left} ' # Right segment RPROMPT='${upper_right}' else # The prompt - PROMPT='${BLOX_CHAR__NEWLINE}${upper_left}${spacing}${upper_right} + PROMPT='%{${upper_left}%}${spacing}%{${upper_right}%} ${lower_left} ' # Right prompt @@ -153,10 +147,43 @@ ${lower_left} ' } # --------------------------------------------- -# Setup hooks +# Setup -# Build the prompt -add-zsh-hook precmd blox_hook__build_prompt +prompt_blox_help() { + cat <<'EOF' +Blox is a minimal and fast ZSH theme that shows you what you need. It consists of blocks, +and you can play with the order and change everything; it comes with some +pre-defined blocks, but you can create your own or even modify them. + +You can consider Blox as a "framework", since you can do whatever you want with it. + +Configuration: + +See: https://github.com/yardnsm/blox-zsh-theme + +You can invoke it thus: + + prompt blox + +EOF +} + +prompt_blox_setup() { + setopt prompt_subst + + autoload -Uz colors && colors + autoload -U add-zsh-hook + autoload -Uz vcs_info + + add-zsh-hook precmd blox_hook__build_prompt + add-zsh-hook precmd blox_hook__title + + return 0 +} + +prompt_blox_preview () { + local +h PS1='%# ' + prompt_preview_theme blox "$@" +} -# Set title -add-zsh-hook precmd blox_hook__title +prompt_blox_setup "$@" \ No newline at end of file