nixos-dotfiles

nixos-dotfiles

https://git.tonybtw.com/nixos-dotfiles.git git://git.tonybtw.com/nixos-dotfiles.git
3,642 bytes raw
1
#!/usr/bin/env bash
2
3
# In case the system uses a non-POSIX shell, like fish or nushell,
4
# we want to ensure run also our forked processes in a bash environment.
5
SHELL="bash"
6
7
# === Change keybinds or add more here ===
8
9
declare -a INDEXES=(
10
    "nixpkgs ctrl-n"
11
    "home-manager ctrl-h"
12
13
    # you can add any indexes combination here,
14
    # like `nixpkgs,nixos`
15
16
    "all ctrl-a"
17
)
18
19
SEARCH_SNIPPET_KEY="ctrl-w"
20
OPEN_SOURCE_KEY="ctrl-s"
21
OPEN_HOMEPAGE_KEY="ctrl-o"
22
NIX_SHELL_KEY="ctrl-i"
23
PRINT_PREVIEW_KEY="ctrl-p"
24
25
OPENER="xdg-open"
26
27
if [[ "$(uname)" == 'Darwin' ]]; then
28
    SEARCH_SNIPPET_KEY="alt-w"
29
    OPEN_SOURCE_KEY="alt-s"
30
    OPEN_HOMEPAGE_KEY="alt-o"
31
    NIX_SHELL_KEY="alt-i"
32
    PRINT_PREVIEW_KEY="alt-p"
33
34
    OPENER="open"
35
fi
36
37
# ========================================
38
39
# for debug / development
40
CMD="${NIX_SEARCH_TV:-nix-search-tv}"
41
42
# bind_index binds the given $key to the given $index
43
bind_index() {
44
    local key="$1"
45
    local index="$2"
46
47
    local prompt=""
48
    local indexes_flag=""
49
    if [[ -n "$index" && "$index" != "all" ]]; then
50
        indexes_flag="--indexes $index"
51
        prompt=$index
52
    fi
53
54
    local preview="$CMD preview $indexes_flag"
55
    local print="$CMD print $indexes_flag"
56
57
    echo "$key:change-prompt($prompt> )+change-preview($preview {})+reload($print)"
58
}
59
60
STATE_FILE="/tmp/nix-search-tv-fzf"
61
62
# save_state saves the currently displayed index
63
# to the $STATE_FILE. This file serves as an external script state
64
# for communication between "print" and "preview" commands
65
save_state() {
66
    local index="$1"
67
68
    local indexes_flag=""
69
    if [[ -n "$index" && "$index" != "all" ]]; then
70
        indexes_flag="--indexes $index"
71
    fi
72
73
    echo "execute(echo $indexes_flag > $STATE_FILE)"
74
}
75
76
HEADER="$OPEN_HOMEPAGE_KEY - open homepage
77
$OPEN_SOURCE_KEY - open source
78
$SEARCH_SNIPPET_KEY - search github for snippets
79
$NIX_SHELL_KEY - nix-shell
80
$PRINT_PREVIEW_KEY - print preview
81
"
82
83
FZF_BINDS=""
84
for e in "${INDEXES[@]}"; do
85
    index=$(echo "$e" | awk '{ print $1 }')
86
    keybind=$(echo "$e" | awk '{ print $2 }')
87
88
    fzf_bind=$(bind_index "$keybind" "$index")
89
    fzf_save_state=$(save_state "$index")
90
    FZF_BINDS="$FZF_BINDS --bind '$fzf_bind+$fzf_save_state'"
91
92
    newline=$'\n'
93
    HEADER="$HEADER$keybind - $index$newline"
94
done
95
96
# reset the state
97
echo "" >/tmp/nix-search-tv-fzf
98
99
SEARCH_SNIPPET_CMD=$'echo "{}"'
100
# fzf surrounds the matched package with ', trim them
101
SEARCH_SNIPPET_CMD="$SEARCH_SNIPPET_CMD | tr -d \"\'\" "
102
# if it's multi-index search, then we need to remote the prefix
103
SEARCH_SNIPPET_CMD="$SEARCH_SNIPPET_CMD | awk \'{ if (\$2) { print \$2 } else print \$1 }\' "
104
SEARCH_SNIPPET_CMD="$SEARCH_SNIPPET_CMD | xargs printf \"https://github.com/search?type=code&q=lang:nix+%s\" \$1 "
105
106
NIX_SHELL_CMD='nix-shell --run $SHELL -p $(echo "{}" | sed "s:nixpkgs/::g"'
107
NIX_SHELL_CMD="$NIX_SHELL_CMD | tr -d \"\'\")"
108
109
PREVIEW_WINDOW="wrap"
110
[ "$(tput cols)" -lt 90 ] && PREVIEW_WINDOW="$PREVIEW_WINDOW,up"
111
112
eval "$CMD print | fzf \
113
    --preview '$CMD preview \$(cat $STATE_FILE) {}' \
114
    --bind '$OPEN_SOURCE_KEY:execute($CMD source \$(cat $STATE_FILE) {} | xargs $OPENER)' \
115
    --bind '$OPEN_HOMEPAGE_KEY:execute($CMD homepage \$(cat $STATE_FILE) {} | xargs $OPENER)' \
116
    --bind $'$SEARCH_SNIPPET_KEY:execute($SEARCH_SNIPPET_CMD | xargs $OPENER)' \
117
    --bind $'$NIX_SHELL_KEY:become($NIX_SHELL_CMD)' \
118
    --bind $'$PRINT_PREVIEW_KEY:become($CMD preview \$(cat $STATE_FILE) {})' \
119
    --layout reverse \
120
    --scheme history \
121
    --preview-window='$PREVIEW_WINDOW' \
122
    --header '$HEADER' \
123
    --header-first \
124
    --header-border \
125
    --header-label \"Help\" \
126
    $FZF_BINDS
127
"