summaryrefslogtreecommitdiff
path: root/utility.zsh
blob: b35e974ade90386bd8c2d571a8a9c108791befcc (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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
##
## Utility Functions
##
#
### TODO: 245

function _smooth_fzf() {
  local fname
  local current_dir="$PWD"
  cd "${XDG_CONFIG_HOME:-~/.config}"
  fname="$(fzf)" || return
  $EDITOR "$fname"
  cd "$current_dir"
}

function _sudo_replace_buffer() {
  local old=$1 new=$2 space=${2:+ }

  # if the cursor is positioned in the $old part of the text, make
  # the substitution and leave the cursor after the $new text
  if [[ $CURSOR -le ${#old} ]]; then
    BUFFER="${new}${space}${BUFFER#$old }"
    CURSOR=${#new}
  # otherwise just replace $old with $new in the text before the cursor
  else
    LBUFFER="${new}${space}${LBUFFER#$old }"
  fi
}

function _sudo_command_line() {
  # If line is empty, get the last run command from history
  [[ -z $BUFFER ]] && LBUFFER="$(fc -ln -1)"

  # Save beginning space
  local WHITESPACE=""
  if [[ ${LBUFFER:0:1} = " " ]]; then
    WHITESPACE=" "
    LBUFFER="${LBUFFER:1}"
  fi

}
    # If $SUDO_EDITOR or $VISUAL are defined, then use that as $EDITOR
    # Else use the default $EDITOR
    #local EDITOR=${SUDO_EDITOR:-${VISUAL:-$EDITOR}}

    # If $EDITOR is not set, just toggle the sudo prefix on and off
    #if [[ -z "$EDITOR" ]]; then
    #  case "$BUFFER" in
    #    sudo\ -e\ *) _sudo_replace_buffer "sudo -e" "" ;;
    #    sudo\ *) _sudo_replace_buffer "sudo" "" ;;
    #    *) LBUFFER="sudo $LBUFFER" ;;
    #  esac
    #  return
    #fi

    # Check if the typed command is really an alias to $EDITOR

    # Get the first part of the typed command
    #local cmd="${${(Az)BUFFER}[1]}"
    # Get the first part of the alias of the same name as $cmd, or $cmd if no alias matches
    # local realcmd="${${(Az)aliases[$cmd]}[1]:-$cmd}"
    # Get the first part of the $EDITOR command ($EDITOR may have arguments after it)
    # local editorcmd="${${(Az)EDITOR}[1]}"

    # Note: ${var:c} makes a $PATH search and expands $var to the full path
    # The if condition is met when:
    # - $realcmd is '$EDITOR'
    # - $realcmd is "cmd" and $EDITOR is "cmd"
    # - $realcmd is "cmd" and $EDITOR is "cmd --with --arguments"
    # - $realcmd is "/path/to/cmd" and $EDITOR is "cmd"
    # - $realcmd is "/path/to/cmd" and $EDITOR is "/path/to/cmd"
    # or
    # - $realcmd is "cmd" and $EDITOR is "cmd"
    # - $realcmd is "cmd" and $EDITOR is "/path/to/cmd"
    # or
    # - $realcmd is "cmd" and $EDITOR is /alternative/path/to/cmd that appears in $PATH
#    if [[ "$realcmd" = (\$EDITOR|$editorcmd|${editorcmd:c}) \
#      || "${realcmd:c}" = ($editorcmd|${editorcmd:c}) ]] \
#      || builtin which -a "$realcmd" | command grep -Fx -q "$editorcmd"; then
#      _sudo_replace_buffer "$cmd" "sudo -e"
#      return
#    fi

    # Check for editor commands in the typed command and replace accordingly
#    case "$BUFFER" in
#      $editorcmd\ *) _sudo_replace_buffer "$editorcmd" "sudo -e" ;;
#      \$EDITOR\ *) _sudo_replace_buffer '$EDITOR' "sudo -e" ;;
#      sudo\ -e\ *) _sudo_replace_buffer "sudo -e" "$EDITOR" ;;
#      sudo\ *) _sudo_replace_buffer "sudo" "" ;;
#      *) LBUFFER="sudo $LBUFFER" ;;
#    esac
#  } always {
#    # Preserve beginning space
#    LBUFFER="${WHITESPACE}${LBUFFER}"
#    # Redisplay edit buffer (compatibility with zsh-syntax-highlighting)
#    zle redisplay
#  }
#}

function _vi_search_fix() {
  zle vi-cmd-mode
  zle .vi-history-search-backward
}

function toppy() {
    history | awk '{CMD[$2]++;count++;}END { for (a in CMD)print CMD[a] " " CMD[a]/count*100 "% " a;}' | grep -v "./" | column -c3 -s " " -t | sort -nr | nl |  head -n 21
}

#function cd() {
	#builtin cd "$@" && command lsd
#}

function git-svn(){
  if [[ ! -z "$1" && ! -z "$2" ]]; then
          echo "Starting clone/copy ..."
          repo=$(echo $1 | sed 's/\/$\|.git$//')
          svn export "$repo/trunk/$2"
  else
          echo "Use: git-svn <repository> <subdirectory>"
  fi  
}

# vim with auto sudo
function vim () {
    # if [ ! "$@" ]; then
    #     #printf "No arguments passed in, need \$@ - file is required\n"
    #     #printf "\$@ Argument required.\n"
    #     printf "File name argument required.\n"
    #     return 1
    # fi
    if [ ! "$@" ]; then
        if [ ! -w "$(pwd)" ]; then
            printf "Opening vim with sudo...\n"
            sudo $HOME/.local/bin/lvim
            return
        else
            printf "Opening vim...\n"
            $HOME/.local/bin/lvim
            return
        fi
    fi
	if [ ! -e "$@" ]
	then
        printf "$@ doesn't exist\ntouch File '$@'?(y/N)"
        if read -q "choice? "
        then
            echo
		    #DIR=$(dirname "$@")
		    #OWNER=$(stat -c '%U' "$DIR")
            #if [[ "$OWNER" == "root" ]]
            #if [[ "$OWNER" != "$USER" ]]
            if [ ! -w "$(dirname $@)" ]
            then
                printf "Opening vim with sudo...\n"
                sudo $HOME/.local/bin/lvim "$@"
                if [ ! -e "$@" ]
                then
                    #printf "File \"$@\" is Empty, auto deleted...\n"
                    printf "File \"$@\" is isn't saved, auto deleted...\n"
                    return
                fi
                return
            #elif [[ -e "$@" ]]
            else
                printf "Opening vim...\n"
                $HOME/.local/bin/lvim "$@"
                if [ ! -e "$@" ]
                then
                    #printf "File \"$@\" is Empty, auto deleted...\n"
                    printf "File \"$@\" is isn't saved, auto deleted...\n"
                    return
                fi
                return
            fi

        else
            printf '\nExiting...\n'
            return
        fi
	#else
		#OWNER=$(stat -c '%U' "$@")
	fi
    if [ ! -w "$@" ]
	then
        printf "Opening vim with sudo...\n"
		sudo $HOME/.local/bin/lvim "$@"
        return
	elif [[ -e "$@" ]]; then
        printf "Opening vim...\n"
		$HOME/.local/bin/lvim "$@"
        return
	fi
#    if [ -e "$@" ]
#    then
#        if ! test -s $@; then
#            printf "$@ is empty\nrm $1?(y/N)"
#            if read -q "choice? "
#            then
#                echo
#                if [[ "$OWNER" == "root" ]]; then
#                    sudo rm "$@"
#                else
#                    rm "$@"
#                fi
#            fi
#        fi
#    fi
}

#function cd() {
#    if [[ ! -e "$@" && "$@" != "-" ]]; then
#        printf "'$@' doesn't exist\nmkdir '$@'?(y/N)"
#        if read -q "choice? "; then
#            echo
#            if [ ! -w "$(dirname $@)" ]
#            then
#                printf "Making dir with sudo...\n"
#                sudo /usr/bin/mkdir "$@"
#                return
#            #elif [[ -e "$@" ]]
#            else
#                printf "Making dir...\n"
#                /usr/bin/mkdir "$@"
#                return
#            fi
#        fi
#    fi
#    builtin cd "$@"
#
#}

##############################################
### i found out those won't work with pipe ###
##############################################

#function cat () {
#	if [ ! -e "$1" ]
#	then
#		DIR=$(dirname "$1")
#		OWNER=$(stat -c '%U' "$DIR")
#	else
#		OWNER=$(stat -c '%U' "$1")
#	fi
#	if [[ "$OWNER" == "root" ]]
#	then
#		sudo /usr/bin/bat --pager=never "$1"
#	else
#		/usr/bin/bat --pager=never "$1"
#	fi
#}

#function less () {
#	if [ ! -e "$1" ]
#	then
#		DIR=$(dirname "$1")
#		OWNER=$(stat -c '%U' "$DIR")
#	else
#		OWNER=$(stat -c '%U' "$1")
#	fi
#	if [[ "$OWNER" == "root" ]]
#	then
#		sudo /usr/bin/less "$1"
#	else
#		/usr/bin/less "$1"
#	fi
#}

# move applications
function move_to_desktop {
    while [ true ]
    do
        # new apps are spawned on desktop 1
        W=$(bspc query -W -d ^1)
        echo "[$W]"
        if [ -n "$W" ];
        then
            break
        fi
    done
    # $1 is passed in desktop, i.e. ^3
    $(bspc window $W --to-desktop $1)
}

# function unzip ()
# {
#   if [ -f $1 ] ; then
#     case $1 in
#       *.tar.bz2)   mkdir ${1%.tar.bz2} && tar xvjf $1 -C ${1%.tar.bz2}   ;;
#       *.tar.gz)    mkdir ${1%.tar.gz} && tar xvzf $1 -C ${1%.tar.gz}   ;;
#       *.tar.xz)    mkdir ${1%.tar.xz} && xz -d -v $1 ;;
#       *.bz2)       bunzip2 $1   ;;
#       *.rar)       unrar x $1     ;;
#       *.gz)        gunzip $1    ;;
#       *.tar)       mkdir ${1%.*} && tar xvf $1    ;;
#       *.tbz2)      mkdir ${1%.*} && tar xzjf $1   ;;
#       *.tgz)       tar xzf $1   ;;
#       *.zip)       /usr/bin/unzip $1 -d ${1%.*}    ;;
#       *.Z)         uncompress $1;;
#       *.7z)        7z vx $1      ;;
#       *.deb)       ar vx $1 --output ${1%.*}    ;;
#       *)           echo "'$1' cannot be extracted via unzip()" ;;
#     esac
#   else
#     echo "'$1' is not a valid file"
#   fi
# }

function unzip () {
    ouch d --dir ${1%.*} $@
}

function nix-upgrade ()
{
  nix-env -u
}

function nix-install ()
{
  nix-env -iA nixpkgs.$@
}

function nix-search ()
{
    nix search nixpkgs $@
}

function ga ()
{
  if [[ -n "${1+x}" ]]; then
    git add $@ -f
  else
    git add -A -f
  fi
}

function linuxcmd ()
{
    #$(which $@)
    "/usr/bin/$@"
}

function gcco()
{
  if [ -n "${2+x}" ]; then
    gcc -Wall -Wextra -std=c11 -g $1 -o $2
  else
    gcc -Wall -Wextra -std=c11 -g $1 -o ${1%.*}.o
 fi
}

function gccd ()
{
  if [ -n "${2+x}" ]; then
    gcc -Wall -Wextra -std=c11 -g -c $1 -o $2
  else
    gcc -Wall -Wextra -std=c11 -g -c $1 -o ${1%.*}.o
  fi
}

function whatisin ()
{
  cat $(which $@)
}

function path ()
{
  echo "$(pwd)/${1}"
}

function vimake ()
{
  if [ -e "Makefile" ]; then  
    lvim Makefile
  else
    printf "No Makefile"
  fi
}

function vimpkg ()
{
  if [ -e "PKGBUILD" ]; then  
    nvim PKGBUILD
  else
    printf "No PKGBUILD"
  fi
}

function installpipreqs ()
{
    if [ -e "requirements.txt" ]; then
        pip install -r requirements.txt
    else
        echo "There isn't a requirements.txt"
    fi
}

# function compress ()
# {
#   # TODO: MAKE MULTI COMPRESS OPTIONS
#   zip ${1%.*} -r $1
# }

# vim:ft=sh

function txt2ascii ()
{
    # banner [-c ]
    # toilet
    figlet $@
}


function killZombie() {
    pid=$(ps -A -ostat,ppid | awk '/[zZ]/ && !a[$2]++ {print $2}');
    if [ "$pid" = "" ]; then
        echo "No zombie processes found.";
    else
        cmd=$(ps -p $pid -o cmd | sed '1d');
        echo "Found zombie process PID: $pid";
        echo "$cmd";
        echo "Kill it? Return to continue… (ctrl+c to cancel)";
        read -r;
        sudo kill -9 $pid;
    fi
}

function createrepo() {
    gh repo create $1 --add-readme --license gpl-3.0 ${@:2}
    tea repo create --name $1 --init --readme Default --license gpl3 --branch main ${@:2}
}