-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew
More file actions
executable file
·66 lines (54 loc) · 1.2 KB
/
new
File metadata and controls
executable file
·66 lines (54 loc) · 1.2 KB
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
#!/usr/bin/env bash
test $UID -eq 0 &&
template_lib="$XDG_DATA_DIRS/new" ||
template_lib="$XDG_DATA_HOME/new"
extension() { basename "$1" | awk -F '.' '{print $NF}';}
nextension() {
basename "$1" |
awk -F '.' '{
if($1 != "")
{ print $1 }
else
{ print $2 }
}'
}
ldir() { find "$1" -maxdepth 1 ! -path "$1";}
find_template() {
template="$(dirname "$1")/.TEMP"
#pick a file from the template library with a matching extension
test -e "$template" || template="$(
ldir "$template_lib" |
grep "$(extension $1)$" |
umenu -s
)"
#pick any file from the template library
test -e "$template" || template="$(
ldir "$template_lib" |
umenu -s
)"
test -e "$template" && echo "$template" || {
echo "Error, no template files found" >&2
exit 1
}
}
edit_template() {
$EDITOR "$(find_template $1)"
}
make_new() {
template=$(find_template $1)
test -e "$1" || {
cp $template $1 #ensures that permissions are copied
sub="$(nextension $1)"
sed -i "s/TEMP/$sub/g" "$1"
}
$EDITOR "$1"
}
if [ "$1" = '-e' ]; then
edit_template "$2"
elif [ -n "$1" ]; then
make_new "$1"
elif [ -n "`xclip -selection clipboard -o`" ]; then
make_new "`xclip -selection clipboard -o`"
else
make_new "test"
fi