#!/usr/bin/env sh

# todo: this script doesn't work at alll for some reason. clear_area is being called but the cursor isn't moving around correctly

# This magical spell clears a specific area of the terminal window, leaving behind a clean and clear space.
# Usage: cast_clear_area x y width height
# The area to be cleared is specified by its top left corner (x, y) and its width and height.

# Define the function to clear the specified area
clear_area() {
  x=$1
  y=$2
  width=$3
  height=$4
	
  # Move the cursor to the top left corner of the area to be cleared
  move-cursor $x $y
  #echo -en "\033[$y;$xH"

  # Clear the area by outputting a space and then moving the cursor back
  i=0
  while [ $i -lt $height ]; do
    j=0
    while [ $j -lt $width ]; do
      echo -n " "
      j=$((j + 1))
    done
    # Move the cursor back to the start of the line
    #echo -en "\033[$((y + i));$xH"
    move-cursor $x $((y + i))
    i=$((i + 1))
  done
}

# Unit test--this doesn't work yet, but I tseted clear_area manually and it works
test_clear_area() {
    . assertions
    initial_x=$(fathom-cursor -x)
    initial_y=$(fathom-cursor -y)
    move_cursor 5 5
    clear-area 3 3
    move_cursor 5 5
    assert_equal "$(tput cup 5 5; tput sc; tput cup $initial_y $initial_x)" ""
    move_cursor $initial_x $initial_y
    echo "All tests passed"
}

# Check if the script is being called from another script
if [ "${BASH_SOURCE[0]}" = "$0" ]; then
  # If not, call the function and pass it the command line arguments
  clear_area "$@"
fi