blob: 862be9951a1b2a0bd204ec820d5e6c31f0b223d7 (
plain)
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
137
138
139
140
141
142
143
144
145
146
147
148
|
#!/bin/sh
help() {
echo "nx - create new script"
echo "USAGE: nx <script_name>"
}
err() { printf '%s: %s\n' "$0" "$@" >&2; exit 1; }
warn() {
printf '%s: %s [Y/n] ' "$0" "$@" >&2; read -r arg
case "$arg" in ''|y|Y) ;; *) exit ;; esac
}
while getopts 'h' o; do case "$o" in
h) help >&2; exit ;;
*) err "invalid option -- '$OPTARG'" ;;
esac done
shift $((OPTIND - 1))
[ "$#" -lt 1 ] && help && exit 1
[ "$#" -gt 1 ] && err "too many arguments. Only one expected"
new_script() {
cat << EOF > "$1"
#!/bin/sh
help() { echo "$1 -
USAGE:
$1 [OPTION]...
OPTIONS:
-h show this help message"; }
err() { printf '$1: %s\n' "\$@" >&2; exit 1; }
while getopts 'h' o; do case "\$o" in
h) help; exit ;;
*) err "invalid option -- '\$OPTARG'" ;;
esac done
shift \$((OPTIND - 1))
[ "\$#" -lt 1 ] && help >&2 && exit 1
EOF
chmod u+x -- "$1" || exit
}
new_c_file() {
cat << EOF > "$1"
#include <stdio.h>
int main()
{
printf("Hello\n");
return 0;
}
EOF
}
new_cpp_file() {
cat << EOF > "$1"
#include <iostream>
using namespace std;
int main()
{
cout << "Hello" << endl;
cin.get();
return 0;
}
EOF
}
new_py_file() {
cat << EOF > "$1"
import timeit
print("Finished in %fs" % timeit.timeit())
EOF
}
new_java_file() {
PATTERN="Scanner"
VIMCMDS="normal!2n\n noh"
cat << EOF > "$1"
import java.util.Scanner;
class ${1%.java} {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Finished!");
sc.close
}
}
EOF
}
file="$1"
new_file() {
case "$file" in
*.c) new_c_file "$file" ;;
*.cpp) new_cpp_file "$file" ;;
*.py) new_py_file "$file" ;;
*.java) new_java_file "$file" ;;
*) new_script "$file" ;;
esac
}
if [ -f "$file" ]; then
[ ! -w "$file" ] && chmod u+rw "$file"
if [ -s "$file" ]; then
warn "'$file' already exists, edit existing file?" || exit
else
warn "an empty file '$file' already exists, fill and edit?" &&
new_file "$file"
fi
elif [ -e "$file" ]; then
err "'$file' already exists and is a non-regular file"
else
new_file "$file"
fi
ls -lF --color -- "$file"
case "$EDITOR" in
'') echo "$0: \$EDITOR variable not set" >&2 && exit 1 ;;
vi|*vim*)
VIMCONF="$(mktemp)"; export VIMCONF
[ -n "$VIOPTS" ] && echo "set $VIOPTS" >> "$VIMCONF"
[ -n "$PATTERN" ] && echo "/$PATTERN" >> "$VIMCONF"
[ -n "$VIMCMDS" ] && echo "$VIMCMDS" >> "$VIMCONF"
$EDITOR -S "$VIMCONF" "$file"
rm -f "$VIMCONF"
;;
*) $EDITOR "$file" ;;
esac; exit
|