-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconf.plugin.zsh
executable file
·67 lines (60 loc) · 1.47 KB
/
conf.plugin.zsh
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
#!/bin/zsh
typeset -Ag conf_locations
local _conf_editor() {
if ! [[ -w ${1} ]]; then
sudoedit ${1}
else
$EDITOR ${1}
fi
}
conf() {
if [[ $1 == -r ]]; then
local confconf=$ZDOTDIR/confs
if [[ -e $confconf ]]; then
conf_locations[conf]=$confconf
conf_locations+=( $(<$confconf) )
fi
return
fi
if [[ -z $1 ]]; then
echo "Available configs:"
for k v in ${(kv)conf_locations}; do
if [[ -e ${(e)v} ]]; then
printf "%-20s %s\n" ${k}: ${(e)v}
fi
done
return 1
fi
local target=${(e)conf_locations[${1}]}
if [[ -d ${target} ]]; then
cd $target
elif [[ -f ${target} ]]; then
_conf_editor $target
elif [[ -n ${target} ]]; then
echo "Conf target for $1 missing: $target"
elif [[ -d $XDG_CONFIG_HOME/$1 ]]; then
if [[ -f $XDG_CONFIG_HOME/$1 ]]; then
_conf_editor $XDG_CONFIG_HOME/$1
else
targetfiles=($XDG_CONFIG_HOME/$1/*(N))
if [[ $#targetfiles == 1 ]]; then
_conf_editor $targetfiles[1]
else
cd $XDG_CONFIG_HOME/$1
fi
fi
else
targetfiles=($XDG_CONFIG_HOME/$1.*(.N))
if [[ $#targetfiles == 1 ]]; then
_conf_editor $targetfiles[1]
elif [[ $#targetfiles -gt 1 ]]; then
echo "Multiple possible matches:"
printf " - %s\n" ${targetfiles[@]}
echo "Please add an entry for it in conf's config (use \`conf conf\` to edit it)"
else
echo "Unknown conf target and no matching files in XDG_CONFIG_HOME: $1"
echo "Please add an entry for it in conf's config (use \`conf conf\` to edit it)"
fi
fi
}
conf -r