-
Notifications
You must be signed in to change notification settings - Fork 2
/
hued.sh
47 lines (42 loc) · 1.24 KB
/
hued.sh
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
#/usr/bin/env bash
prefix="content/posts/" # the directory from where to start
RED="\033[0;31m"
GREEN="\033[0;32m"
NC="\033[0m"
_hued () {
# Ref: https://askubuntu.com/a/707643
local cur
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
k=0
for j in $( compgen -f "${prefix}/${cur}" ); do # loop trough the possible completions
[ -d "${j}" ] && j="${j}/" || j="${j} " # if its a dir add a shlash, else a space
COMPREPLY[k++]=${j#$prefix/} # remove the directory prefix from the array
done
return 0
}
hued () {
if [[ $1 = "--help" ]] || [[ $# -eq 0 ]]; then
echo -e "usage: hued <filename>."
echo -e "The <filename> need under \"${prefix}\" dir."
echo -e "\ne.g, hued ouo.md"
return 0
fi
if [ ! -d "./${prefix}" ]; then
echo -e "${RED}error: \"${prefix}\" dir not found${NC}"
return 1
fi
filename=$@
# Add prefix directory name
# Ref: https://stackoverflow.com/a/38558776/6734174
set -- "${@/#/${prefix}}"
if [ ! -f $@ ]; then
echo -e "${GREEN}hugo new posts/${filename}${NC}"
hugo new "posts/${filename}"
fi
if [[ -n $@ ]]; then
vim "$@"
echo "< vim" $@ " >"
fi
}
complete -o default -F _hued hued