-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
defaults.sh
195 lines (183 loc) · 5.72 KB
/
defaults.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
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
_print() {
local total_lines=$(awk 'END {print NR}' $CURRENT_FILE)
echo "[File: $(realpath $CURRENT_FILE) ($total_lines lines total)]"
lines_above=$(jq -n "$CURRENT_LINE - $WINDOW/2" | jq '[0, .] | max | floor')
lines_below=$(jq -n "$total_lines - $CURRENT_LINE - $WINDOW/2" | jq '[0, .] | max | round')
if [ $lines_above -gt 0 ]; then
echo "($lines_above more lines above)"
fi
cat $CURRENT_FILE | grep -n $ | head -n $(jq -n "[$CURRENT_LINE + $WINDOW/2, $WINDOW/2] | max | floor") | tail -n $(jq -n "$WINDOW")
if [ $lines_below -gt 0 ]; then
echo "($lines_below more lines below)"
fi
}
_constrain_line() {
if [ -z "$CURRENT_FILE" ]
then
echo "No file open. Use the open command first."
return
fi
local max_line=$(awk 'END {print NR}' $CURRENT_FILE)
local half_window=$(jq -n "$WINDOW/2" | jq 'floor')
export CURRENT_LINE=$(jq -n "[$CURRENT_LINE, $max_line - $half_window] | min")
export CURRENT_LINE=$(jq -n "[$CURRENT_LINE, $half_window] | max")
}
# @yaml
# signature: open <path> [<line_number>]
# docstring: opens the file at the given path in the editor. If line_number is provided, the window will be move to include that line
# arguments:
# path:
# type: string
# description: the path to the file to open
# required: true
# line_number:
# type: integer
# description: the line number to move the window to (if not provided, the window will start at the top of the file)
# required: false
open() {
if [ -z "$1" ]
then
echo "Usage: open <file>"
return
fi
# Check if the second argument is provided
if [ -n "$2" ]; then
# Check if the provided argument is a valid number
if ! [[ $2 =~ ^[0-9]+$ ]]; then
echo "Usage: open <file> [<line_number>]"
echo "Error: <line_number> must be a number"
return # Exit if the line number is not valid
fi
local max_line=$(awk 'END {print NR}' $1)
if [ $2 -gt $max_line ]; then
echo "Warning: <line_number> ($2) is greater than the number of lines in the file ($max_line)"
echo "Warning: Setting <line_number> to $max_line"
local line_number=$(jq -n "$max_line") # Set line number to max if greater than max
elif [ $2 -lt 1 ]; then
echo "Warning: <line_number> ($2) is less than 1"
echo "Warning: Setting <line_number> to 1"
local line_number=$(jq -n "1") # Set line number to 1 if less than 1
else
local OFFSET=$(jq -n "$WINDOW/6" | jq 'floor')
local line_number=$(jq -n "[$2 + $WINDOW/2 - $OFFSET, 1] | max | floor")
fi
else
local line_number=$(jq -n "$WINDOW/2") # Set default line number if not provided
fi
if [ -f "$1" ]; then
export CURRENT_FILE=$(realpath $1)
export CURRENT_LINE=$line_number
_constrain_line
_print
elif [ -d "$1" ]; then
echo "Error: $1 is a directory. You can only open files. Use cd or ls to navigate directories."
else
echo "File $1 not found"
fi
}
# @yaml
# signature: goto <line_number>
# docstring: moves the window to show <line_number>
# arguments:
# line_number:
# type: integer
# description: the line number to move the window to
# required: true
goto() {
if [ $# -gt 1 ]; then
echo "goto allows only one line number at a time."
return
fi
if [ -z "$CURRENT_FILE" ]
then
echo "No file open. Use the open command first."
return
fi
if [ -z "$1" ]
then
echo "Usage: goto <line>"
return
fi
if ! [[ $1 =~ ^[0-9]+$ ]]
then
echo "Usage: goto <line>"
echo "Error: <line> must be a number"
return
fi
local max_line=$(awk 'END {print NR}' $CURRENT_FILE)
if [ $1 -gt $max_line ]
then
echo "Error: <line> must be less than or equal to $max_line"
return
fi
local OFFSET=$(jq -n "$WINDOW/6" | jq 'floor')
export CURRENT_LINE=$(jq -n "[$1 + $WINDOW/2 - $OFFSET, 1] | max | floor")
_constrain_line
_print
}
# @yaml
# signature: scroll_down
# docstring: moves the window down {WINDOW} lines
scroll_down() {
if [ -z "$CURRENT_FILE" ]
then
echo "No file open. Use the open command first."
return
fi
export CURRENT_LINE=$(jq -n "$CURRENT_LINE + $WINDOW - $OVERLAP")
_constrain_line
_print
}
# @yaml
# signature: scroll_up
# docstring: moves the window down {WINDOW} lines
scroll_up() {
if [ -z "$CURRENT_FILE" ]
then
echo "No file open. Use the open command first."
return
fi
export CURRENT_LINE=$(jq -n "$CURRENT_LINE - $WINDOW + $OVERLAP")
_constrain_line
_print
}
# @yaml
# signature: create <filename>
# docstring: creates and opens a new file with the given name
# arguments:
# filename:
# type: string
# description: the name of the file to create
# required: true
create() {
if [ -z "$1" ]; then
echo "Usage: create <filename>"
return
fi
# Check if the file already exists
if [ -e "$1" ]; then
echo "Error: File '$1' already exists."
open "$1"
return
fi
# Create the file an empty new line
printf "\n" > "$1"
# Use the existing open command to open the created file
open "$1"
}
# @yaml
# signature: submit
# docstring: submits your current code and terminates the session
submit() {
cd $ROOT
# Check if the patch file exists and is non-empty
if [ -s "/root/test.patch" ]; then
# Apply the patch in reverse
git apply -R < "/root/test.patch"
fi
git add -A
git diff --cached > model.patch
echo "<<SUBMISSION||"
cat model.patch
echo "||SUBMISSION>>"
}