all repos — dotfiles-base @ 03882a3f6b43542b45f5b8edfbf37809a1a467d0

base important configs that can safely be used almost anywhere

.config/fish/bashrc_functions

 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
############################## user functions ##############################

# find-keyword [directory path] [keyword]: find and list files with keyword in
# the filename
find-keyword() {
    if [ "$#" -ne 2 ]; then                         # if not given 2 arguments
        echo "Usage: find-keyword [directory path] [keyword]"
    else
        find $1 -name "*$2*" -ls
    fi
}

# find-string [string] [directory path]: find and list files containing the
# provided string (and list all occurences/line numbers of the string)
find-string() {
    if [ "$#" -ne 2 ]; then                         # if not given 2 arguments
        echo "Usage: find-string [string] [directory path]"
    else
        grep -nR "$1" "$2"
    fi
}

# img_loop_here: $1 = seconds until next photo  displayed
# BUG: currently, after running this function, the user needs to manually run
# 'reset' to unscrew the terminal
img-loop-here() {
    if [ "$1" = "" ]; then
        echo "Usage: img_loop_here [seconds until next photo displayed]"
    else
        while true; do
            for g in $(ls .); do
                timeout "$1" chafa -c full "$g"
            done
        done
    fi
}

# rainbow-shell: print out a looping rainbow in the shell. Happy pride month!
# For best results, run in full-screened terminal (Mod+f in i3)
rainbow-shell() {
    yes "$(seq 231 -1 16)" | while read i; do
        printf "\x1b[48;5;${i}m\n"
        sleep .02
    done
}

# colors - print out current color values set in terminal
colors() {
    local fgc bgc vals seq0

    printf "Color escapes are %s\n" '\e[${value};...;${value}m'
    printf "Values 30..37 are \e[33mforeground colors\e[m\n"
    printf "Values 40..47 are \e[43mbackground colors\e[m\n"
    printf "Value  1 gives a  \e[1mbold-faced look\e[m\n\n"

    # foreground colors
    for fgc in {30..37}; do
        # background colors
        for bgc in {40..47}; do
            fgc=${fgc#37} # white
            bgc=${bgc#40} # black

            vals="${fgc:+$fgc;}${bgc}"
            vals=${vals%%;}

            seq0="${vals:+\e[${vals}m}"
            printf "  %-9s" "${seq0:-(default)}"
            printf " ${seq0}TEXT\e[m"
            printf " \e[${vals:+${vals+$vals;}}1mBOLD\e[m"
        done
        echo; echo
    done
}

# term-color-vals: print all 256-bit color values in their associated color
term-color-vals() {
    for i in {0..255}; do
        printf "\x1b[38;5;${i}mcolor%-5i\x1b[0m" $i
        if ! (( ($i + 1 ) % 8 )); then
            echo
        fi
    done
}

# ex - archive extractor
# usage: ex <file>
ex() {
  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 ex()" ;;
    esac
  else
    echo "'$1' is not a valid file"
  fi
}

# prune_bash_history - remove certain frequently-used/"clutter" command entries
# from .bash_history
prune_bash_history() {
sed 's/^l$//g ; s/^ll$//g ; s/^la$//g ; s/^lla$//g ; s/^cl$//g ; s/^range$//g ; s/^e$//g ; s/^q$//g ; /^$/d ' \
                                        "$HOME"/.bash_history > \
                                        "$HOME"/.bash_history-BAK
    if [ -f "$HOME"/.bash_history-BAK ]; then
		mv "$HOME"/.bash_history-BAK "$HOME"/.bash_history
	else
		echo "[ERROR] .bash_history-BAK was not created. Aborting..."
	fi
}

# public-ip: fetch current public IP address
public-ip() {
    curl 'http://icanhazip.com/'
    #curl 'https://j3s.sh/ip.html'
}

# scan-for-printers: look for printers on the connected LAN
scan-for-printers() {
    sudo lpinfo -v
}

# wifi-strength: get some WiFi statistics in real time
wifi-strength() {
    watch -n 1 cat /proc/net/wireless
}

sync-src-to-dest() {
    if [ ! "$#" = 2 ]; then
		echo "A useful rsync helper, semi-unsafe if used thoughtlessly"
        echo "usage: sync-src-to-dest [src-dir/] [dest-dir/]"
        return 1
    fi

    # for syncing to Android phone
    #rsync -rlgo -vhcz -e ssh --stats --progress --delete --force "$1" "$2"
    rsync -avhcz -e ssh --stats --progress --delete --force "$1" "$2"
}

# STILL has one edge case (if argument contains '..' AND goes through symlink)
mkcd() {
    [ -z "$1" ] && return 1
    case "$1" in /*) :;; *) set -- "./$1";; esac # for weird directories "-xx/"
    mkdir "$1" && cd "$1"
}

# create executable file containing 'sh' shebang
touch-script() {
    if [ "$#" = 0 ]; then
        echo "usage: $0 [path(s) to one or more script file(s) to init...] "
        return 1
    else
        touch "$@"
        chmod +x "$@"
        for f in "$@"; do
            printf '#!/bin/sh\n\n' > "$f"
        done
    fi
}

# pick file path with fzf, open in $EDITOR
fzvim() {
    file_path="$(fzf)"
    if [ -e "$file_path" ]; then
        echo "Editing $file_path..."
        "$EDITOR" "$file_path"
    elif [ "$file_path" = "" ]; then  # no entries chosen from fzf
        :                             # do nothing
    else
        echo "$file_path not found."
    fi
}

# Note: has a limit of like 10k or something, I forget
hex2bin() {
    if [ "$#" = 0 ]; then
        echo "usage: $0 [path of file containing hex digits to bindump]"
        return 1
    fi
    sed 's/\([0-9A-F]\{2\}\)/\\\\\\x\1/gI' "$1" | xargs printf
}

bin2hex() {
    if [ "$#" = 0 ]; then
        echo "usage: $0 [path of file to hexdump]"
        return 1
    fi
    hexdump -v -e '1/1 "%02x"' "$1"
}