Skip to content

Commit

Permalink
feat: enhance input handling script with color support and help option
Browse files Browse the repository at this point in the history
Add dynamic color support to the script for better user feedback,
including info, warning, and success messages. Introduce a help
option (-h/--help) to guide users on script usage. Improve input
handling by integrating `rlwrap` and `bat` for enhanced interactivity
and fallback to `cat` when unavailable. Update exit codes for better
error signaling.

These changes improve the script's usability, interactivity, and
user experience by providing clear feedback and robust input handling.
The help option ensures users can easily understand the script's
functionality.

Signed-off-by: Marcelo Borges <me@marceloborges.dev>
  • Loading branch information
Marcelo Borges committed Feb 10, 2025
1 parent 7eec075 commit c7860be
Showing 1 changed file with 63 additions and 10 deletions.
73 changes: 63 additions & 10 deletions scripts/paste
Original file line number Diff line number Diff line change
@@ -1,19 +1,72 @@
#!/bin/sh

# Use a friendly blue color for messages
echoinfo() { printf "\033[1;36m%s\033[0m\n" "$@" >&2; } # Cyan text
# Check if the terminal supports colors
if tput setaf 1 >/dev/null 2>&1; then
COLOR_INFO="$(tput setaf 4)" # Bright Blue for general info
COLOR_WARN="$(tput setaf 3)" # Yellow for warnings
COLOR_SUCCESS="$(tput setaf 2)" # Green for success
COLOR_RESET="$(tput sgr0)"
else
COLOR_INFO=""
COLOR_WARN=""
COLOR_SUCCESS=""
COLOR_RESET=""
fi

# Function to print messages with appropriate colors
echoinfo() {
printf "%s%s%s\n" "$COLOR_INFO" "$1" "$COLOR_RESET" >&2
}

echowarn() {
printf "%s%s%s\n" "$COLOR_WARN" "$1" "$COLOR_RESET" >&2
}

echosuccess() {
printf "%s%s%s\n" "$COLOR_SUCCESS" "$1" "$COLOR_RESET" >&2
}

# Display help message
show_help() {
printf "Usage: %s [OPTION]\n" "$0"
printf "Capture input and echo it back to stdout.\n\n"
printf "Options:\n"
printf " -h, --help Show this help message and exit\n\n"
printf "Example:\n"
printf " %s # Capture input interactively\n" "$0"
printf " echo 'Hello' | %s # Capture input from a pipe\n" "$0"
exit 0
}

if [ -t 0 ]; then # Check if input is from a terminal (not piped)
echoinfo "📋 Input content and press Enter (Ctrl+D to finish):"
input_content=$(cat) # Capture full multi-line input
# Check for help option
case "$1" in
-h | --help) show_help ;;
esac

if command -v bat >/dev/null 2>&1; then
CAT="bat"
else
CAT="cat"
fi

# Check if rlwrap is available
if command -v rlwrap >/dev/null 2>&1; then
RLWRAP="rlwrap $CAT"
else
input_content=$(cat) # Read from pipe
RLWRAP="$CAT"
fi

if [ -t 0 ]; then # Interactive mode
echoinfo "📋 Enter input and press Enter (Ctrl+D to finish):"
input_content=$($RLWRAP)
else # Piped input
input_content=$($CAT)
fi

if [ -z "$input_content" ]; then
echoinfo "⚠️ No input received!"
exit 0
echowarn "⚠️ No input received!"
exit 1
fi

echoinfo "✅ Echoing the input to stdout!"
echo "$input_content"
echosuccess "✅ Echoing the input to stdout!"
printf "%s\n" "$input_content"

0 comments on commit c7860be

Please sign in to comment.