aboutsummaryrefslogtreecommitdiff
path: root/files/.zsh/rc/S50_functions
blob: 86cb63ce5887953803f2fdc6629d2f67b3040e8d (plain) (blame)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
mcd() {
  [ $# = 1 ] && mkdir -p -- "$1" && cd -- "$1"
}
compdef _directories mcd

gclone() {
  git clone --recursive "$1" && cd "$(basename "$1" | sed -e 's/\.git//')"
}

wcc() {
  wc -c "$@" | numfmt --to=si
}

wcl() {
  wc -l "$@" | numfmt --grouping
}

ppgrep() {
    pgrep "$@" | xargs --no-run-if-empty ps fp
}

setgov() {
    GOVS=($(cpufreq-info -g))

    if [ -z "$1" ] ; then
        cat <<EOM
setgov GOVNAME

Set CPU governor, available are: $GOVS

Current governor is: $(cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor | sort -u | xargs echo)
EOM
        return
    fi

    if [ ${GOVS[(i)$1]} -le ${#GOVS} ] ; then
        for ((i=0; i<$(nproc); i++)) ; do
            sudo cpufreq-set -c $i -r -g $1
        done
    else
        echo "Unsupported governor: $1"
        return 1
    fi
}

open() {
    if [ $# = 0 ] ; then
        xdg-open . >/dev/null 2>&1
    else
        xdg-open "$@" >/dev/null 2>&1
    fi
}

retry() {
    for i in $(seq 5) ; do
        "$@" && break || sleep 5
    done
}

datediff() {
    d1=$(date --date "$1" +%s)
    d2=$(date --date "$2" +%s)
    echo $(( (d1 - d2) / 86300 )) days
}

st() {
    ssh -t "$@" tmux -u new-session -A -s main
}
compdef st=ssh

calc() {
    echo "scale=3;$@" | bc -l
}
alias calc="noglob calc"

# Take from @garybernhardt
activate_virtualenv() {
  local found_location=""
  local default_name=venv
  for name in $default_name env ; do
    for up in . ../.. ../../.. ; do
      if [ -f "$up/$name/bin/activate" ] ; then
        found_location="$up/$name/bin/activate"
        break
      fi
    done
    [ -n "$found_location" ] && break
  done

  if [ -z "$found_location" ] ; then
    # create
    read -r "answer?Virtualenv not found, create? "
    if [[ "$answer" =~ ^[Yy]$ ]] ; then
      if command -v virtualenv>/dev/null ; then
        virtualenv $default_name
      else
        python3 -m venv $default_name
      fi
      activate_virtualenv
    fi
  else
    . "$found_location"
  fi
}

edit-which() {
    command=$(which "$@")
    [ -x "$command" ] && $EDITOR "$command"
}
compdef edit-which=which

which-package() {
    whence -p "$1" | xargs --no-run-if-empty apt-file search --fixed-string
}
compdef which-package=which

ipinfo() {
    curl ipinfo.io/json
}

docker-images-clean() {
  docker images --format "table {{.ID}}\t{{.Repository}}\t{{.Tag}}\t{{.Size}}" |
    fzf --multi --header-lines=1 |
    awk '{print $1}' |
    xargs -r docker rmi -f
}

transfer() {
    curl --progress-bar --upload-file "$1" https://transfer.sh/$(basename "$1") | tee /dev/null;
    echo
}

web-bookmarks() {
    local bookmarks_path="$HOME/.config/chromium/Default/Bookmarks"
    local jq_script='def ancestors: while(. | length >= 2; del(.[-1,-2])); . as $in | paths(.url?) as $key | $in | getpath($key) | {name,url, path: [$key[0:-2] | ancestors as $a | $in | getpath($a) | .name?] | reverse | join("/") } | .path + "/" + .name + "\t" + .url'
    jq -r $jq_script < "$bookmarks_path" |
        sed -E $'s/(.*)\t(.*)/\\1\t\x1b[36m\\2\x1b[m/g' |
        fzf --ansi --multi |
        cut -d$'\t' -f2 |
        xargs xdg-open >/dev/null 2>&1
}

vpn-up() {
    name="$1"
    nmcli connection up "$name" >/dev/null
}

vpn-down() {
    name="$1"
    if nmcli connection show "$name" | grep 'GENERAL.STATE' >/dev/null ; then
        nmcli connection down "$name" >/dev/null
    fi
}

vpn-toggle() {
    name="$1"
    if nmcli connection show "$name" | grep 'GENERAL.STATE' >/dev/null ; then
        vpn-down "$name"
    else
        for vpn in "WG Home" "wgDe" "riga.bobov.name" "bobov.name" ; do
            if [ "$name" != "$vpn" ] ; then
                vpn-down "$vpn"
            fi
        done
        vpn-up "$1"
    fi
}

# vim: ft=zsh :