Skip to content

Instantly share code, notes, and snippets.

@kidpixo
Last active January 13, 2026 21:51
Show Gist options
  • Select an option

  • Save kidpixo/0efa5b04f4e9a1ffd4a57d7296e11eff to your computer and use it in GitHub Desktop.

Select an option

Save kidpixo/0efa5b04f4e9a1ffd4a57d7296e11eff to your computer and use it in GitHub Desktop.
A versatile Bash utility for terminal dividers. Features auto-width detection via tput, robust ANSI color support using printf %b, and a built-in help menu. Ideal for organizing long log outputs or CLI tool reports.
# ==============================================================================
# Function: separator
# Description: Prints a repeating line of characters to the terminal.
# Usage: separator [char] [length/color] [color]
# ==============================================================================
separator() {
if [ -z "$1" ]; then
echo "Usage: separator [char] [length/color] [color]"
return 1
fi
local char="$1"
local num=""
local color_input=""
# --- Smart Logic ---
# Check if $2 is a positive integer
if [[ "$2" =~ ^[0-9]+$ ]]; then
num="$2"
color_input="$3"
else
# If $2 is not a number, it's treated as the color
num=$(tput cols 2>/dev/null || echo 80)
color_input="$2"
fi
# Define Colors
local color_name=$(echo "$color_input" | tr '[:upper:]' '[:lower:]')
local color_code=""
case "$color_name" in
red) color_code="\033[31m" ;;
green) color_code="\033[32m" ;;
yellow) color_code="\033[33m" ;;
blue) color_code="\033[34m" ;;
magenta) color_code="\033[35m" ;;
cyan) color_code="\033[36m" ;;
*) color_code="" ;;
esac
local reset="\033[0m"
# Generate and print
local line=$(printf "%.s$char" $(seq 1 "$num"))
printf "%b%s%b\n" "$color_code" "$line" "$reset"
}
# # --- Screenshot Demo ---
# echo "TEST 1: Default (Full Width)"
# separator "="
# echo -e "\nTEST 2: Specific Length & Color"
# separator "*" 40 green
# echo -e "\nTEST 3: Full Width & Color"
# separator "~" "" cyan
# echo -e "\nTEST 4: Loop Demonstration"
# for c in red yellow green; do
# echo $c
# separator "-" 30 "$c"
# done
# echo -e "\nTEST 5: Help Menu (Run without args)"
# separator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment