aboutsummaryrefslogtreecommitdiffstats
path: root/.zsh.functions/02_utility.zsh
blob: c08ce10270a2c5872c39e12e26dbb49c464da484 (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
101
102
103
104
# Makes a directory and changes to it.
function mkdcd {
  [[ -n "$1" ]] && mkdir -p "$1" && builtin cd "$1"
}

# Changes to a directory and lists its contents.
function cdls {
  builtin cd "$argv[-1]" && ls "${(@)argv[1,-2]}"
}

# Pushes an entry onto the directory stack and lists its contents.
function pushdls {
  builtin pushd "$argv[-1]" && ls "${(@)argv[1,-2]}"
}

# Pops an entry off the directory stack and lists its contents.
function popdls {
  builtin popd "$argv[-1]" && ls "${(@)argv[1,-2]}"
}

# Prints columns 1 2 3 ... n.
function slit {
  awk "{ print ${(j:,:):-\$${^@}} }"
}

# Finds files and executes a command on them.
function find-exec {
  find . -type f -iname "*${1:-}*" -exec "${2:-file}" '{}' \;
}

# Displays user owned processes status.
function psu {
  ps -U "${1:-$LOGNAME}" -o 'pid,%cpu,%mem,command' "${(@)argv[2,-1]}"
}

# Highlights diff output.
function diff {
  if (( $+commands[colordiff] )); then
    command diff --unified "$@" | colordiff --difftype diffu
  elif (( $+commands[git] )); then
    git --no-pager diff --color=auto --no-ext-diff --no-index "$@"
  else
    command diff --unified "$@"
  fi
}

# Highlights make output.
function make {
  if (( $+commands[colormake] )); then
    colormake "$@"
  else
    command make "$@"
  fi
}

# Highlights wdiff output.
function wdiff {
  if (( $+commands[wdiff] )); then
    command wdiff \
      --avoid-wraps \
      --start-delete="$(print -n $FG[red])" \
      --end-delete="$(print -n $FG[none])" \
      --start-insert="$(print -n $FG[green])" \
      --end-insert="$(print -n $FG[none])" \
      "$@" \
    | sed 's/^\(@@\( [+-][[:digit:]]*,[[:digit:]]*\)\{2\} @@\)$/;5;6m\10m/g'
  elif (( $+commands[git] )); then
    git --no-pager diff --color=auto --no-ext-diff --no-index --color-words "$@"
  else
    command wdiff "$@"
  fi
}

# Lists the contents of archives.
function lsarchive {
  while (( $# > 0 )); do
    if [[ ! -s "$1" ]]; then
      print "$0: file not valid: $1" >&2
      shift
      continue
    fi
    case "$1:l" in
      (*.tar.gz|*.tgz) tar t${verbose:+v}vzf "$1" ;;
      (*.tar.bz2|*.tbz|*.tbz2) tar t${verbose:+v}jf "$1" ;;
      (*.tar.xz|*.txz) tar --xz --help &> /dev/null \
        && tar --xz -t${verbose:+v}f "$1" \
        || xzcat "$1" | tar t${verbose:+v}f - ;;
      (*.tar.zma|*.tlz) tar --lzma --help &> /dev/null \
        && tar --lzma -t${verbose:+v}f "$1" \
        || lzcat "$1" | tar x${verbose:+v}f - ;;
      (*.tar) tar t${verbose:+v}f "$1" ;;
      (*.zip) unzip -l${verbose:+v} "$1" ;;
      (*.rar) unrar &> /dev/null \
        && unrar ${${verbose:+v}:-l} "$1" \
        || rar ${${verbose:+v}:-l} "$1" ;;
      (*.7z) 7za l "$1" ;;
      (*)
        print "$0: cannot list: $1" >&2
        success=1
      ;;
    esac
    shift
  done
}