Skip to content

Instantly share code, notes, and snippets.

@joshmfrankel
Last active August 20, 2023 23:04
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshmfrankel/fb9b4f44244aa492d8cdc3fd5f0db7cb to your computer and use it in GitHub Desktop.
Save joshmfrankel/fb9b4f44244aa492d8cdc3fd5f0db7cb to your computer and use it in GitHub Desktop.
Oh-my-zsh + powerlevel9k + powerline fonts + awesome-terminal-fonts + syntax highlighting
# References
# OhMyZsh: https://github.com/robbyrussell/oh-my-zsh
# Powerlevel9k: https://github.com/bhilburn/powerlevel9k
# Powerline fonts: https://github.com/powerline/fonts
# Awesome Terminal Fonts: https://github.com/gabrielelana/awesome-terminal-fonts
# ZSH Syntax Highlighting: https://github.com/zsh-users/zsh-syntax-highlighting
#
# For more plugins search this repo: https://github.com/unixorn/awesome-zsh-plugins
# 256 color mode
export TERM="xterm-256color"
# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH
# Path to your oh-my-zsh installation.
export ZSH=/home/josh/.oh-my-zsh
# Set name of the theme to load. Optionally, if you set this to "random"
# it'll load a random theme each time that oh-my-zsh is loaded.
# See https://github.com/robbyrussell/oh-my-zsh/wiki/Themes
# ZSH_THEME="af-magic"
POWERLEVEL9K_MODE='awesome-fontconfig' # Powerlevel9k_mode must always come before ZST_THEME
ZSH_THEME="powerlevel9k/powerlevel9k"
################
# Powerlevel9k #
################
# Sidebars
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(os_icon dir vcs)
POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(status command_execution_time custom_ruby custom_git_stats)
# Custom Ruby
# Just shows a cute ruby icon (in red)
POWERLEVEL9K_CUSTOM_RUBY="echo '\Ue639 '"
POWERLEVEL9K_CUSTOM_RUBY_FOREGROUND="red"
# Custom Git stats
# Displays listing of files changes, insertions, and deletions made for the current branch
POWERLEVEL9K_CUSTOM_GIT_STATS="custom_git_stats"
custom_git_stats() {
# https://tecadmin.net/bash-shell-test-if-file-or-directory-exists/#
if test -d .git
then
local total_changed=0
local total_insertions=0
local total_deletions=0
# TODO: Clean up while loops
# Either memoize git diff results
# Or only loop once while matching separately in grep
{git diff --shortstat --cached; git diff --shortstat} | grep -P '\d+ (?=files? changed)' -o | while read value; do
(( total_changed = total_changed + $value ))
done
{git diff --shortstat --cached; git diff --shortstat} | grep -P '\d+ (?=insertions?)' -o | while read value; do
(( total_insertions = total_insertions + $value ))
done
{git diff --shortstat --cached; git diff --shortstat} | grep -P '\d+ (?=deletions?)' -o | while read value; do
(( total_deletions = total_deletions + $value ))
done
# No circle
# + \Uf067
# - \Uf068
echo "\Uf15c $total_changed \Uf055 $total_insertions \Uf056 $total_deletions"
fi
}
# Element styles
POWERLEVEL9K_OS_ICON_BACKGROUND="white"
POWERLEVEL9K_OS_ICON_FOREGROUND="blue"
POWERLEVEL9K_SHORTEN_DIR_LENGTH=2
POWERLEVEL9K_DIR_HOME_FOREGROUND="white"
POWERLEVEL9K_DIR_HOME_SUBFOLDER_FOREGROUND="white"
POWERLEVEL9K_DIR_DEFAULT_FOREGROUND="white"
POWERLEVEL9K_SHOW_CHANGESET=true
POWERLEVEL9K_STATUS_VERBOSE=false
POWERLEVEL9K_COMMAND_EXECUTION_TIME_THRESHOLD=0
POWERLEVEL9K_COMMAND_EXECUTION_TIME_FOREGROUND="white"
POWERLEVEL9K_COMMAND_EXECUTION_TIME_BACKGROUND="blue"
# Prompt Line
POWERLEVEL9K_PROMPT_ON_NEWLINE=true
POWERLEVEL9K_MULTILINE_FIRST_PROMPT_PREFIX=''
POWERLEVEL9K_MULTILINE_SECOND_PROMPT_PREFIX="%K{white}%F{black} \Uf017 `date +%I:%M:%S` `date +%P` %f%k%F{white}%f "
###############################
# Uncomment the following line to use case-sensitive completion.
# CASE_SENSITIVE="true"
# Uncomment the following line to use hyphen-insensitive completion. Case
# sensitive completion must be off. _ and - will be interchangeable.
# HYPHEN_INSENSITIVE="true"
# Uncomment the following line to disable bi-weekly auto-update checks.
# DISABLE_AUTO_UPDATE="true"
# Uncomment the following line to change how often to auto-update (in days).
# export UPDATE_ZSH_DAYS=13
# Uncomment the following line to disable colors in ls.
# DISABLE_LS_COLORS="true"
# Uncomment the following line to disable auto-setting terminal title.
# DISABLE_AUTO_TITLE="true"
# Uncomment the following line to enable command auto-correction.
ENABLE_CORRECTION="true"
# Uncomment the following line to display red dots whilst waiting for completion.
COMPLETION_WAITING_DOTS="true"
# Uncomment the following line if you want to disable marking untracked files
# under VCS as dirty. This makes repository status check for large repositories
# much, much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"
# Uncomment the following line if you want to change the command execution time
# stamp shown in the history command output.
# The optional three formats: "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# HIST_STAMPS="mm/dd/yyyy"
# Would you like to use another custom folder than $ZSH/custom?
# ZSH_CUSTOM=/path/to/new-custom-folder
# Which plugins would you like to load? (plugins can be found in ~/.oh-my-zsh/plugins/*)
# Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(git rails ruby rvm sublime command-not-found jsontools npm nyan web-search bundler)
source $ZSH/oh-my-zsh.sh
# User configuration
# export MANPATH="/usr/local/man:$MANPATH"
# You may need to manually set your language environment
# export LANG=en_US.UTF-8
# Preferred editor for local and remote sessions
# if [[ -n $SSH_CONNECTION ]]; then
# export EDITOR='vim'
# else
# export EDITOR='mvim'
# fi
# Compilation flags
# export ARCHFLAGS="-arch x86_64"
# ssh
# export SSH_KEY_PATH="~/.ssh/rsa_id"
#########
# Paths #
#########
export NVM_DIR="/home/josh/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
##################
# Custom Plugins #
##################
source ~/.oh-my-zsh/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
########################
# User-defined Aliases #
########################
# Set personal aliases, overriding those provided by oh-my-zsh libs,
# plugins, and themes. Aliases can be placed here, though oh-my-zsh
# users are encouraged to define aliases within the ZSH_CUSTOM folder.
# For a full list of active aliases, run `alias`.
#
# Example aliases
# alias ohmyzsh="mate ~/.oh-my-zsh"
alias zshconfig="subl ~/.zshrc"
alias zshreload="source ~/.zshrc"
alias ag="alias | grep"
# Rails helpers
alias remove-server-pid="rm -rf tmp/pids/server.pid && kill-process-by name ruby"
# Octopress
alias os="jekyll serve --config _config.dev.yml"
alias od="jekyll build --config _config.yml && octopress deploy"
# Gem helpers
alias pryr="pry-remote"
# Application Helpers
alias spotify-1080="spotify --force-device-scale-factor=1"
# Service helpers
alias redis-start="sudo service redis_6379 start"
alias redis-stop="sudo service redis_6379 stop"
alias postgres-start="sudo /etc/init.d/postgresql start"
alias elasticsearch-start="sudo service elasticsearch start"
# Work helpers
alias lessonly-psql="psql --dbname=lessonly_development"
########################
# User-defined Methods #
########################
# Kill Process by
#
# port - All processes with port will be killed
# name - All processes given a name will be killed
function kill-process-by()
{
case $1 in
port )
kill -9 $(lsof -ti tcp:$2)
;;
name )
kill -9 $(lsof -t -c $2)
;;
esac
}
########
# MOTD #
########
fortune | cowsay -f dragon-and-cow
@danylkaaa
Copy link

danylkaaa commented Oct 8, 2018

Receive an output each time when using your custom_git_stats:
image

@surjikal
Copy link

surjikal commented Oct 3, 2019

@zulusk Try grep -Po instead

@joshmfrankel
Copy link
Author

The custom_git_stats was more or less experimental. I found that it was a bit slow given that it has to calculate each time a command is run. I'll have to check out grep -Po

@surjikal
Copy link

surjikal commented Mar 1, 2020

This is what I derived from your code. Just pasting it for fun, not saying it's better or anything like that:

custom_git_stats() {
  if git rev-parse --show-toplevel > /dev/null 2>&1
  then
    local total_insertions=0
    local total_deletions=0

    local data=$(git diff --shortstat --cached; git diff --shortstat)

    echo $data | grep -Po '\d+ (?=insertions?)' | while read value; do
      (( total_insertions = total_insertions + $value ))
    done

    echo $data | grep -Po '\d+ (?=deletions?)' | while read value; do
      (( total_deletions = total_deletions + $value ))
    done

    [[ $total_insertions -eq 0 ]] && total_insertions="" || total_insertions="%F{yellow}+$total_insertions"
    [[ $total_deletions  -eq 0 ]] && total_deletions=""  || total_deletions="%F{red}-$total_deletions"

    echo "$total_insertions $total_deletions"
  fi
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment