#!/bin/sh

# A cantrip to clear the user's current input lines in the terminal, including
#  the terminal prompt.
#
# Cast this spell to erase the user's current input lines, leaving the terminal
# ready for a new command.

# todo: modify this to use our cursor file

clear_input_lines() {
  # Save the current cursor position
  printf '\033[s'

  # Move the cursor to the beginning of the line
  printf '\033[1G'

  # Clear the current line
  printf '\033[2K'

  # Calculate the number of lines used by the user's input
  lines="$(current_input_lines)"

  # Clear the remaining lines
  i=1
  while [ "$i" -lt "$lines" ]; do
    # Move the cursor down one line
    printf '\033[1B'

    # Clear the current line
    printf '\033[2K'

    # Increment the counter
    i=$(( i + 1 ))
  done

  # Restore the cursor position
  printf '\033[u'
}

# Check if the script is being executed or sourced
if [ "$0" = "$BASH_SOURCE" ]; then
  # Script is being executed, call the clear_input_lines function
  clear_input_lines
fi