-
Notifications
You must be signed in to change notification settings - Fork 0
/
work.zsh
executable file
·200 lines (168 loc) · 6.55 KB
/
work.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
work() {
# This is a script to get a specific line from ~/.hw.txt using the second argument
# as a substring to search for in the file. If the second argument is not provided,
# the script will use the first line in the file.
#
# Then, it will cd to "~/Documents/university/2_year/2_semester/*substring*"`
# Then it will open the file in neovim using `nvim *substring*`, and finally
# It will perform <leader>la to compile the file
#
# Usage:
# hw [-c] [substring]
# Define the base directory
base_dir=~/Documents/university/2_year/2_semester
# if there is a second arugment and it's data, ssh to server
if [ "$1" = "grr" ]; then
if [ "$2" = "borg" ]; then
elif [ "$2" = "mc" ]; then
else
fi
return
fi
# Get the substring from the second argument or the first line of the file
# if there are 3 arguments, the first one is -c, so the substring is the second one
# if no arguments are provided, the substring is the first line of the file
if [ -z "$1" ]; then
substring=$(head -n 1 ~/.hw.txt)
elif [ "$1" = "-c" ]; then
substring=$(grep -i "$2" ~/.hw.txt)
else
substring=$(grep -i "$1" ~/.hw.txt)
fi
# echo "Substring: $substring"
# If the substring is not found, exit
if [ -z "$substring" ]; then
echo "No substring found"
return
fi
# Assuming the filename can be directly inferred from substring, adjust as needed
# if the substring is lab, open lab
if [[ $substring == *"lab"* ]]; then
full_path="${base_dir}/ma366/${substring}/"
else
full_path="${base_dir}/${substring}/hw/"
fi
if [ ! -d "$full_path" ]; then
echo "Directory does not exist: $full_path"
return
fi
cd "$full_path"
# Out of all the homweork folders in the form of "hw1", "hw2", etc., open the latest one
# if the substring is lab, check last lab folder instead
if [[ $substring == *"lab"* ]]; then
latest_hw=$(ls -d lab* | sort -n | tail -n 1)
else
latest_hw=$(ls -d hw* | sort -n | tail -n 1)
fi
if [ -z "$latest_hw" ]; then
echo "No homework folders found"
return
fi
filename="content.tex"
if [ "$1" = "-c" ]; then
# If the first argument is -c, create a new homework folder with number one greater than the latest one
# the number is always a 2 digit number (01, 02, etc.)
# if there are no homework folders, create hw01
previous_hw="$latest_hw"
echo $previous_hw
if [ -z "$latest_hw" ]; then
latest_hw="hw01"
if [[ $substring == *"lab"* ]]; then
latest_hw="lab01"
fi
else
if [[ $substring == *"lab"* ]]; then
latest_hw=$(echo $latest_hw | sed 's/lab//')
latest_hw=$(printf "lab%02d" $((10#$latest_hw + 1)))
else
latest_hw=$(echo $latest_hw | sed 's/hw//')
latest_hw=$(printf "hw%02d" $((10#$latest_hw + 1)))
fi
fi
# echo "Creating new homework folder: $latest_hw"
mkdir "$latest_hw"
cd "$latest_hw"
# if the hw to create is hw1, copy the template from .template.tex
if [[ $latest_hw = "hw01" ]]; then
cp ~/.template.tex main.tex
else
# otherwise, copy the main.tex from the previous homework folder
# need to go ../ to get to the previous homework folder
echo "Copying main.tex from $previous_hw"
cp "../$previous_hw/main.tex" main.tex
# find the \title in the main.tex and replace the number of the homework
# the title is in the form of \title{"Course name" Homework/Lab 1}
# so we just need the last number in the title and replace it with the number of the homework
# The file name
file="main.tex"
# Pattern to match the title line
pattern="title{.* Homework .*}"
matched_line=$(grep -o "$pattern" "$file")
# if matched line is empty, then the pattern is for lab
if [ -z "$matched_line" ]; then
pattern="title{.* Lab .*}"
matched_line=$(grep -o "$pattern" "$file")
fi
# echo "Matched line: $matched_line"
if [ ! -z "$matched_line" ]; then
# Extract the last number in the line
# Extract the number that is followed by }
last_number=$(echo "$matched_line" | grep -o '[0-9]\+' | tail -n 1)
if [ ! -z "$last_number" ]; then
# Increment the number
new_number=$((last_number + 1))
# echo "Incrementing the number in the title from $last_number to $new_number"
# Reverse the line, replace the first occurrence (which is originally the last) of the number, and reverse back
reversed_matched_line=$(echo "$matched_line" | rev)
reversed_new_line=$(echo "$reversed_matched_line" | sed "s/$(echo $last_number | rev)/$(echo $new_number | rev)/" | rev)
# Now, $reversed_new_line contains the original line but with the last occurrence of the number incremented
new_line=$reversed_new_line
# Escape special characters in the original and new lines for sed replacement
escaped_matched_line=$(echo "$matched_line" | sed 's/[\/&]/\\&/g')
escaped_new_line=$(echo "$new_line" | sed 's/[\/&]/\\&/g')
# Use sed to replace the line in the file. Since we are on macOS, remember the '' after -i to avoid backup
sed -i '' "s/${escaped_matched_line}/${escaped_new_line}/" "$file"
# echo "Replaced '$matched_line' with '$new_line' in $file."
else
echo "No number found to increment."
fi
else
echo "The pattern was not found in the file."
fi
fi
# echo "Creating $filename"
touch $filename
pdflatex main.tex
# Open the file in neovim with the necessary commands
nvim "$filename" \
-c ':set filetype=tex' \
-c ':VimtexCompile' \
-c ':term source ~/.zshrc && pdf' \
-c ':bnext' \
#-c ':term' \
return
fi
# if there is no argument -c, but there is an argument $2, open the homework with that number
if [ ! -z "$2" ] && [ "$1" != "-c" ] ; then
latest_hw=$(printf "hw%02d" $2)
if [[ $substring == *"lab"* ]]; then
latest_hw=$(printf "lab%02d" $2)
fi
fi
# echo "Opening homework folder: $latest_hw"
cd "$latest_hw"
# Check if the file exists
if [ ! -f "$filename" ]; then
echo "File does not exist: $filename"
return
fi
# Open the file in neovim with the necessary commands
nvim "$filename" \
-c ':set filetype=tex' \
-c ':VimtexCompile' \
-c ':term source ~/.zshrc && pdf' \
-c ':bnext' \
}