-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoxide-config-finder
executable file
·136 lines (117 loc) · 3.75 KB
/
voxide-config-finder
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
#!/usr/bin/env bash
shopt -s nullglob
# v c
# -> show things that could be completed with <v c ...>
# -> if there are ~/.fooconfig and ~/.config/foo, show both
# v c foo
# -> find first of ~/.fooconfig, ~/.config/foo/config, etc
# -> if doesn't exist, fzf -1 "v c" with "foo" as initial search, choosing first if only 1
# v c foo -
# -> fzf files in foo, like "v cf" does now
# v c foo bar
# -> find first of ~/.fooconfig/bar, ~/.config/foo/bar, etc
# -> fzf -1 "~/.fooconfig/*, ~/.config/foo/config/*, etc" starting with "bar"
# -> if no matches, but ~/.config/foo etc dir exist, open new file in that dir
# -> otherwise, error
#
# This could all be generalized into a generic "paths" script.
# Maybe write in some other language since it's so complicated.
# Maybe build it into voxide itself (generalizing fzf so it's not a strict dependency)
# -> TODO at least, we could probably use with filter (fzf -m01) in voxide
EXTENSIONS=("" ".json" ".yml" ".yaml" ".toml")
find_all_configs_with_inside_paths_with_fzf() {
name="$1"
filterstr="$2"
readarray -t paths < <(
cd
for i in ".${name}" ".config/${name}/config" ".config/${name}/${name}" ".config/${name}" ".${name}/config" ".${name}config"; do
[[ -e "$i" ]] && find -H "$i" -type f -iname "*$filterstr*"
# ^ -H -- follow symlinks only in args
done | fzf --multi -0 -1 | while read -r line; do
echo "$HOME"/"$line"
done)
if [[ -z "$paths" ]]; then
for i in "$HOME/.${name}" "$HOME/.config/${name}/config" "$HOME/.config/${name}/${name}" "$HOME/.config/${name}" "$HOME/.${name}/config" "$HOME/.${name}config"; do
# Make new file if config dir exists -- e.g. "v c fish waz" -> ~/.config/fish/waz
[[ -d "$i" ]] && echo "$HOME"/"$i"/"$filterstr" && return 0
done
return 1
else
for i in "${paths[@]}"; do
echo $i
done
return 0
fi
}
find_all_configs_with_fzf() {
filterstr=$1
# This might be more complicated than necessary
# no args -- use fzf
readarray -t paths < <(
(cd && for i in .* .config/*/config .config/* .config/* .*/config .*config; do
printf '%s\n' "$i"
done) | sort | uniq | grep -v '^[.]*$' | grep "${filterstr:-.}" | fzf --multi -0 -1 --query "$fzfinitial" | while read -r line; do
echo "$HOME"/"$line"
done
)
# TODO is this necessary?
if [[ -z "$paths" ]]; then
return 1
else
for i in "${paths[@]}"; do
echo $i
done
return 0
fi
}
find_all_using_standard_paths() {
name="$1"
suffix="$2"
findall="$3"
found=1
suffix="$2"
if [[ -n "$suffix" ]]; then
suffix="/$suffix"
fi
STANDARD_PATHS=("$HOME/.${name}" "$HOME/.config/${name}/config" "$HOME/.config/${name}/${name}" "$HOME/.config/${name}" "$HOME/.${name}/config" "$HOME/.${name}config")
for path in "${STANDARD_PATHS[@]}"; do
for ext in "${EXTENSIONS[@]}"; do
if [ -f "${path}${ext}${suffix}" ]; then
echo "${path}${ext}${suffix}"
found=0
[[ -z "$findall" ]] && return 0
fi
done
done
for path in "${STANDARD_PATHS[@]}"; do
for ext in "${EXTENSIONS[@]}"; do
if [ -d "${path}${ext}${suffix}" ]; then
echo "${path}${ext}${suffix}"
found=0
[[ -z "$findall" ]] && return 0
fi
done
done
return $found
}
find_first_using_standard_paths() {
find_all_using_standard_paths "$1" "$2" "" && return 0
}
run() {
local name=$1
if [[ "$name" == "-" ]]; then
name=""
fi
if [[ -z "$name" ]]; then
find_all_configs_with_fzf "$2"
return $?
fi
# find_first
find_first_using_standard_paths "$name" "$2" && return 0
if [[ -n "$suffix" ]]; then
find_all_configs_with_inside_paths_with_fzf "$name" "$2"
else
run "" "$name"
fi
}
run "$@"