Skip to content

Commit

Permalink
Update README, add support for promptinit, fix for #2
Browse files Browse the repository at this point in the history
  • Loading branch information
yardnsm committed Jun 10, 2017
1 parent 5cf601e commit dc4fbe0
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 76 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
```
Expand All @@ -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
```
Expand Down Expand Up @@ -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 |
Expand Down Expand Up @@ -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

Expand Down
108 changes: 73 additions & 35 deletions blox.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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%}"

Expand All @@ -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}"
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}
# ---------------------------------------------
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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}
Expand Down Expand Up @@ -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} ))
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 "$@"
2 changes: 1 addition & 1 deletion src/blocks/bgjobs.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/blocks/cwd.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -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%}"

Expand Down
31 changes: 21 additions & 10 deletions src/blocks/git.zsh
Original file line number Diff line number Diff line change
@@ -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}"
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions src/blocks/host.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/blocks/nodejs.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/blocks/symbol.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Loading

0 comments on commit dc4fbe0

Please sign in to comment.