summaryrefslogtreecommitdiff
path: root/dotfiles/system/.bashrc.d/fzf.sh
blob: 9a5a9bdb76e97c56614be1d90aa66a92aa9dadc1 (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
# fzf.sh
# Craig Jennings <c@cjennings.net>
# FZF settings and utility functions

# =============================================================================
# Settings
# =============================================================================
export FZF_DEFAULT_OPTS='--height=70%'
export FZF_DEFAULT_COMMAND='rg --files --no-ignore-vcs --hidden'
export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"

# Directory paths for convenience functions
IF_GAMES_DIR="$HOME/sync/org/text.games"
BOOKS_DIR="$HOME/sync/books"

# =============================================================================
# Navigation
# =============================================================================

# cdff - change directory to where a file resides
cdff() {
    file=$(fzf +m -q "$1")
    dir=$(dirname "$file")
    cd "$dir" || return 1
}

# cdd - cd to directory with fzf
cdd() {
    destdir=$(find "${1:-.}" -path '*/\.*' -prune \
                   -o -type d -print 2>/dev/null | fzf +m) &&
        cd "$destdir"
}

# =============================================================================
# System Admin
# =============================================================================

# Kill process with fzf
kp() {
    pid=$(ps -ef | sed 1d | eval "fzf ${FZF_DEFAULT_OPTS} -m --header='[kill:process]'" | awk '{print $2}')
    if [ -n "$pid" ]; then
        echo "$pid" | xargs kill -"${1:-9}"
        kp
    fi
}

# Install packages with fzf preview
yinstall() {
    yay -Slq | fzf --multi --preview 'yay -Si {1}' | xargs -ro yay -S --noconfirm
}

yinstall-skipverify() {
    yay -Slq | fzf --multi --preview 'yay -Si {1}' | xargs -ro yay -S --noconfirm --mflags --skipinteg
}

# Remove packages with fzf preview
yrm() {
    yay -Qq | fzf --multi --preview 'yay -Qi {1}' | xargs -ro yay -Rns
}

# Find in file with fzf
fif() {
    if [ "$#" -eq 0 ]; then
        echo "Need a string to search for!"
        return 1
    fi
    rg --files-with-matches --no-messages "$1" | \
        fzf --preview "highlight -O ansi -l {} 2>/dev/null | \
        rg --colors 'match:bg:yellow' --ignore-case --pretty --context 10 '$1' || \
        rg --ignore-case --pretty --context 10 '$1' {}"
}

# =============================================================================
# Convenience
# =============================================================================

# Find and read epub book in terminal
bk() {
    bkfile=$(find "$BOOKS_DIR" -iname "*.epub" -print | fzf)
    if [ -n "$bkfile" ]; then
        epr "$bkfile"
    fi
}

# Find and play interactive fiction game
tg() {
    gamefile=$(find "$IF_GAMES_DIR" -type f \( -iname "*.z[1-8]" -o -iname "*.zblorb" -o -iname "*.blorb" \) -print | fzf)
    if [ -n "$gamefile" ]; then
        frotz "$gamefile"
    fi
}

# =============================================================================
# WireGuard
# =============================================================================

wgup() {
    # Shutdown existing connections first
    output=$(sudo wg 2>/dev/null)
    if [ -n "$output" ]; then
        for iface in $(sudo wg show interfaces); do
            sudo wg-quick down "${iface}"
        done
    fi
    # Select and start new connection
    wgfile=$(sudo find /etc/wireguard/ -iname "*.conf" -exec basename -s .conf {} \; | fzf)
    if [ -n "$wgfile" ]; then
        sudo wg-quick up "$wgfile"
        sudo wg
    fi
}

wgdown() {
    output=$(sudo wg 2>/dev/null)
    if [ -n "$output" ]; then
        for iface in $(sudo wg show interfaces); do
            sudo wg-quick down "${iface}"
        done
    fi
}

alias wg=wgup