aboutsummaryrefslogtreecommitdiffstats
path: root/_zsh/40-functions.zsh
blob: cd9ebc946846f87644313b8a53551687dcadfff4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#
# zsh/functions.zsh
#
# execute 'functions <func_name>' to show function details.
#

function exists() {
    which $1 &> /dev/null
}


function zsh_recompile() {
    autoload -U zrecompile
    rm -f ~/.zsh/*.zwc
    [[ -f ~/.zshrc ]] && zrecompile -p ~/.zshrc
    [[ -f ~/.zshrc.zwc.old ]] && rm -f ~/.zshrc.zwc.old

    for f in ~/.zsh/**/*.zsh; do
        [[ -f $f ]] && zrecompile -p $f
        [[ -f $f.zwc.old ]] && rm -f $f.zwc.old
    done

    [[ -f ~/.zcompdump ]] && zrecompile -p ~/.zcompdump
    [[ -f ~/.zcompdump.zwc.old ]] && rm -f ~/.zcompdump.zwc.old

    source ~/.zshrc
}


function extract() {
  echo Extracting $1 ...
  if [ -f "$1" ] ; then
      case "$1" in
          *.tar.bz2)
              tar xjf "$1";;
          *.tar.gz)
              tar xzf "$1";;
          *.bz2)
              bunzip2 "$1";;
          *.rar)
              unrar x "$1";;
          *.gz)
              gunzip "$1";;
          *.tar)
              tar xf "$1";;
          *.tbz2)
              tar xjf "$1";;
          *.tgz)
              tar xzf "$1";;
          *.zip)
              unzip "$1";;
          *.Z)
              uncompress "$1";;
          *.7z)
              7z x "$1";;
          *)
              echo "'$1' cannot be extracted via extract()" ;;
      esac
  else
      echo "'$1' is not a valid file"
  fi
}


function trash() {
    local path
    for path in "$@"; do
        # ignore any arguments
        if [[ "${path}" = -* ]]; then
            :
        else
        local dst="${path##*/}"
        # append the time if necessary
        while [ -e ~/.trash/"${dst}" ]; do
            dst="${dst} "$(date +%H-%M-%S)
        done
        command mv "${path}" ~/.trash/"${dst}"
        fi
    done
}


function strip_diff_leading_symbols() {
    local color_code_regex="(\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K])"

    # simplify the unified patch diff header
    sed -r "s/^($color_code_regex)diff --git .*$//g" | \
        sed -r "s/^($color_code_regex)index .*$/\n\1$(rule)/g" | \
        sed -r "s/^($color_code_regex)\+\+\+(.*)$/\1+++\5\n\1$(rule)\x1B\[m/g" |\
        # actually strips the leading symbols
        sed -r "s/^($color_code_regex)[\+\-]/\1 /g"
}


## Print a horizontal rule
function rule() {
    printf "%$(tput cols)s\n" | tr ' ' '-'
}

# vim: set ts=8 sw=4 tw=0 fenc=utf-8 ft=zsh: #