defaults.sh 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. _print() {
  2. local total_lines=$(awk 'END {print NR}' $CURRENT_FILE)
  3. echo "[File: $(realpath $CURRENT_FILE) ($total_lines lines total)]"
  4. lines_above=$(jq -n "$CURRENT_LINE - $WINDOW/2" | jq '[0, .] | max | floor')
  5. lines_below=$(jq -n "$total_lines - $CURRENT_LINE - $WINDOW/2" | jq '[0, .] | max | round')
  6. if [ $lines_above -gt 0 ]; then
  7. echo "($lines_above more lines above)"
  8. fi
  9. cat $CURRENT_FILE | grep -n $ | head -n $(jq -n "[$CURRENT_LINE + $WINDOW/2, $WINDOW/2] | max | floor") | tail -n $(jq -n "$WINDOW")
  10. if [ $lines_below -gt 0 ]; then
  11. echo "($lines_below more lines below)"
  12. fi
  13. }
  14. _constrain_line() {
  15. if [ -z "$CURRENT_FILE" ]
  16. then
  17. echo "No file open. Use the open command first."
  18. return
  19. fi
  20. local max_line=$(awk 'END {print NR}' $CURRENT_FILE)
  21. local half_window=$(jq -n "$WINDOW/2" | jq 'floor')
  22. export CURRENT_LINE=$(jq -n "[$CURRENT_LINE, $max_line - $half_window] | min")
  23. export CURRENT_LINE=$(jq -n "[$CURRENT_LINE, $half_window] | max")
  24. }
  25. # @yaml
  26. # signature: open <path> [<line_number>]
  27. # 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
  28. # arguments:
  29. # path:
  30. # type: string
  31. # description: the path to the file to open
  32. # required: true
  33. # line_number:
  34. # type: integer
  35. # description: the line number to move the window to (if not provided, the window will start at the top of the file)
  36. # required: false
  37. open() {
  38. if [ -z "$1" ]
  39. then
  40. echo "Usage: open <file>"
  41. return
  42. fi
  43. # Check if the second argument is provided
  44. if [ -n "$2" ]; then
  45. # Check if the provided argument is a valid number
  46. if ! [[ $2 =~ ^[0-9]+$ ]]; then
  47. echo "Usage: open <file> [<line_number>]"
  48. echo "Error: <line_number> must be a number"
  49. return # Exit if the line number is not valid
  50. fi
  51. local max_line=$(awk 'END {print NR}' $1)
  52. if [ $2 -gt $max_line ]; then
  53. echo "Warning: <line_number> ($2) is greater than the number of lines in the file ($max_line)"
  54. echo "Warning: Setting <line_number> to $max_line"
  55. local line_number=$(jq -n "$max_line") # Set line number to max if greater than max
  56. elif [ $2 -lt 1 ]; then
  57. echo "Warning: <line_number> ($2) is less than 1"
  58. echo "Warning: Setting <line_number> to 1"
  59. local line_number=$(jq -n "1") # Set line number to 1 if less than 1
  60. else
  61. local OFFSET=$(jq -n "$WINDOW/6" | jq 'floor')
  62. local line_number=$(jq -n "[$2 + $WINDOW/2 - $OFFSET, 1] | max | floor")
  63. fi
  64. else
  65. local line_number=$(jq -n "$WINDOW/2") # Set default line number if not provided
  66. fi
  67. if [ -f "$1" ]; then
  68. export CURRENT_FILE=$(realpath $1)
  69. export CURRENT_LINE=$line_number
  70. _constrain_line
  71. _print
  72. elif [ -d "$1" ]; then
  73. echo "Error: $1 is a directory. You can only open files. Use cd or ls to navigate directories."
  74. else
  75. echo "File $1 not found"
  76. fi
  77. }
  78. # @yaml
  79. # signature: goto <line_number>
  80. # docstring: moves the window to show <line_number>
  81. # arguments:
  82. # line_number:
  83. # type: integer
  84. # description: the line number to move the window to
  85. # required: true
  86. goto() {
  87. if [ $# -gt 1 ]; then
  88. echo "goto allows only one line number at a time."
  89. return
  90. fi
  91. if [ -z "$CURRENT_FILE" ]
  92. then
  93. echo "No file open. Use the open command first."
  94. return
  95. fi
  96. if [ -z "$1" ]
  97. then
  98. echo "Usage: goto <line>"
  99. return
  100. fi
  101. if ! [[ $1 =~ ^[0-9]+$ ]]
  102. then
  103. echo "Usage: goto <line>"
  104. echo "Error: <line> must be a number"
  105. return
  106. fi
  107. local max_line=$(awk 'END {print NR}' $CURRENT_FILE)
  108. if [ $1 -gt $max_line ]
  109. then
  110. echo "Error: <line> must be less than or equal to $max_line"
  111. return
  112. fi
  113. local OFFSET=$(jq -n "$WINDOW/6" | jq 'floor')
  114. export CURRENT_LINE=$(jq -n "[$1 + $WINDOW/2 - $OFFSET, 1] | max | floor")
  115. _constrain_line
  116. _print
  117. }
  118. # @yaml
  119. # signature: scroll_down
  120. # docstring: moves the window down {WINDOW} lines
  121. scroll_down() {
  122. if [ -z "$CURRENT_FILE" ]
  123. then
  124. echo "No file open. Use the open command first."
  125. return
  126. fi
  127. export CURRENT_LINE=$(jq -n "$CURRENT_LINE + $WINDOW - $OVERLAP")
  128. _constrain_line
  129. _print
  130. }
  131. # @yaml
  132. # signature: scroll_down
  133. # docstring: moves the window down {WINDOW} lines
  134. scroll_up() {
  135. if [ -z "$CURRENT_FILE" ]
  136. then
  137. echo "No file open. Use the open command first."
  138. return
  139. fi
  140. export CURRENT_LINE=$(jq -n "$CURRENT_LINE - $WINDOW + $OVERLAP")
  141. _constrain_line
  142. _print
  143. }
  144. # @yaml
  145. # signature: create <filename>
  146. # docstring: creates and opens a new file with the given name
  147. # arguments:
  148. # filename:
  149. # type: string
  150. # description: the name of the file to create
  151. # required: true
  152. create() {
  153. if [ -z "$1" ]; then
  154. echo "Usage: create <filename>"
  155. return
  156. fi
  157. # Check if the file already exists
  158. if [ -e "$1" ]; then
  159. echo "Error: File '$1' already exists."
  160. open "$1"
  161. return
  162. fi
  163. # Create the file an empty new line
  164. printf "\n" > "$1"
  165. # Use the existing open command to open the created file
  166. open "$1"
  167. }
  168. # @yaml
  169. # signature: submit
  170. # docstring: submits your current code and terminates the session
  171. submit() {
  172. cd $ROOT
  173. # Check if the patch file exists and is non-empty
  174. if [ -s "/root/test.patch" ]; then
  175. # Apply the patch in reverse
  176. git apply -R < "/root/test.patch"
  177. fi
  178. git add -A
  179. git diff --cached > model.patch
  180. echo "<<SUBMISSION||"
  181. cat model.patch
  182. echo "||SUBMISSION>>"
  183. }