dotfiles

My dotfiles for Arch Linux
Log | Files | Refs | README

zsh_functions (2749B)


      1 # Show all 256 colors with color number
      2 function spectrum_ls() {
      3   for code in {000..255}; do
      4     print -P -- "$code: %F{$code}Text%{$reset_color%}"
      5   done
      6 }
      7 
      8 # Helper function, simply prints current cursor position
      9 function get_cursor_pos() {
     10     echo -ne "\033[6n"
     11     read -s -d\[ garbage
     12     read -s -d R pos
     13 }
     14 
     15 # Function for getting the number of stripped dirs in the prompt
     16 function get_stripped_dir_count() {
     17 	COUNT=$(echo $PWD | grep -o "/" | wc -l)
     18 	SUB=$1
     19 	RES=$(($COUNT - $SUB))
     20 	echo $RES
     21 	unset COUNT
     22 	unset SUB
     23 	unset RES
     24 }
     25 
     26 # Function to parse the number of background processes
     27 function bg_process() {
     28 	NUM_BG=$(jobs -l | grep "\[[0-9]\+\]" | wc -l)
     29 	if [ ! "$NUM_BG" -eq "0" ]; then
     30 		echo "[bg: $NUM_BG]"
     31 	fi
     32 	unset NUM_BG
     33 }
     34 
     35 # Git
     36 
     37 parse_git_branch() {
     38 # Show Git branch/tag, or name-rev if on detached head
     39 ( git symbolic-ref -q HEAD || git name-rev --name-only --no-undefined --always HEAD ) 2> /dev/null
     40 }
     41 
     42 parse_git_state() {
     43   # Show different symbols as appropriate for various Git repository states
     44   # Compose this value via multiple conditional appends.
     45   local GIT_STATE=""
     46   local NUM_AHEAD="$(git log --oneline @{u}.. 2> /dev/null | wc -l | tr -d ' ')"
     47   if [ "$NUM_AHEAD" -gt 0 ]; then
     48     GIT_STATE=$GIT_STATE${GIT_PROMPT_AHEAD//NUM/$NUM_AHEAD}
     49   fi
     50   local NUM_BEHIND="$(git log --oneline ..@{u} 2> /dev/null | wc -l | tr -d ' ')"
     51   if [ "$NUM_BEHIND" -gt 0 ]; then
     52     GIT_STATE=$GIT_STATE${GIT_PROMPT_BEHIND//NUM/$NUM_BEHIND}
     53   fi
     54   local GIT_DIR="$(git rev-parse --git-dir 2> /dev/null)"
     55   if [ -n $GIT_DIR ] && test -r $GIT_DIR/MERGE_HEAD; then
     56     GIT_STATE=$GIT_STATE$GIT_PROMPT_MERGING
     57   fi
     58   if [[ -n $(git ls-files --other --exclude-standard 2> /dev/null) ]]; then
     59     GIT_STATE=$GIT_STATE$GIT_PROMPT_UNTRACKED
     60   fi
     61   if ! git diff --quiet 2> /dev/null; then
     62     GIT_STATE=$GIT_STATE$GIT_PROMPT_MODIFIED
     63   fi
     64   if ! git diff --cached --quiet 2> /dev/null; then
     65     GIT_STATE=$GIT_STATE$GIT_PROMPT_STAGED
     66   fi
     67   if [[ -n $GIT_STATE ]]; then
     68     echo "$GIT_PROMPT_PREFIX$GIT_STATE$GIT_PROMPT_SUFFIX"
     69   fi
     70 }
     71 
     72 git_prompt_string() {
     73   local git_where="$(parse_git_branch)"
     74   
     75   # If inside a Git repository, print its branch and state
     76   [ -n "$git_where" ] && echo "$GIT_PROMPT_SYMBOL$(parse_git_state)$GIT_PROMPT_PREFIX%{$fg[green]%}${git_where#(refs/heads/|tags/)}$GIT_PROMPT_SUFFIX"
     77   
     78   # If not inside the Git repo, do nothing
     79   [ ! -n "$git_where" ] && echo ""
     80 }
     81 
     82 n()
     83 {
     84     # The default behaviour is to cd on quit (nnn checks if NNN_TMPFILE is set)
     85     # To cd on quit only on ^G, export NNN_TMPFILE after the call to nnn
     86 
     87     nnn "$@"
     88 
     89     export NNN_TMPFILE=${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.lastd
     90 
     91     if [ -f $NNN_TMPFILE ]; then
     92             . $NNN_TMPFILE
     93             rm $NNN_TMPFILE
     94     fi
     95 }