#!/bin/sh # bg_log: a script to output a message from a background process, while taking # into account the user's current input and preserving it. # # Cast this spell to send a message from a background process to the terminal, # without interrupting or overwriting the user's input. # todo: refactor this to use the cursor file bg_log() { # Save the current cursor position printf '\033[s' # Calculate the number of lines used by the user's input lines=$(current_input_lines) # Move the cursor up the number of lines used by the user's input printf '\033[%dA' "$lines" # Clear the current line printf '\033[2K' # Output the message printf '%s\n' "$@" # Clear the current line printf '\033[2K' # Move the cursor down the number of lines used by the user's input printf '\033[%dB' "$lines" # 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 bg_log function to output a message bg_log $1 fi