#!/bin/bash

# Title......: line
# Description: draws a line on the terminal
# Author.....: Mitchell Johnston - uid 0
# Contact....: mitch@crn.hopto.org
# Updated....: Fri 21 Feb 2025 01:47:42 PM CST
#----------------------------------

: ' Changes
Fri Feb 21 2025 Changed select characters for better terminal support
Sat Feb 15 2025 Changed system for random character
Sat Feb 15 2025 Added in version feature
Sat Feb 15 2025 Added random color
'

# variables
#----------------------------------
[ "$1" == "-D" ] && DEBUG=1 && shift 1     # -D to turn on debug mode
PS4='$SECONDS $LINENO: '                   # debug prompt
DOW=$(date +%a)                            # day of week: Thu
TODAY=$(date +%m/%d)                       # month/day: 03/25
DOM=$(date +%d)                            # day of month: 25
OS=$(uname -s)                             # OS type: SunOS Linux
NAME=${0##*/}                              # name of the script
my_string="+=-#:~βž–βž•βž—πŸ”ΈπŸ”ΆπŸ”˜πŸ”΄πŸŸ€πŸŸ£πŸŸ‘πŸ”΅πŸŸ’γ€°πŸ”…β¬œπŸ”³πŸŒŸπŸ’₯πŸ§ΏπŸ’ "  # Characters to make the line

# Colors - uncomment if needed
R=$(tput setaf 1)                          # red
BR=$(tput setaf 1; tput bold)              # bold red
G=$(tput setaf 2)                          # green
BG=$(tput setaf 2; tput bold)              # bold green
Y=$(tput setaf 3)                          # yellow
BY=$(tput setaf 3; tput bold)              # bold yellow
B=$(tput setaf 4)                          # blue
BM=$(tput setaf 5; tput bold)              # bold magenta
BC=$(tput setaf 6; tput bold)              # bold cyan
C=$(tput setaf 6)                          # cyan
BL=$(tput setaf 7; tput bold)              # bold light grey
BLD=$(tput bold)                           # bold
N=$(tput sgr0)                             # normal
SIT=$(tput sitm)                           # italics
RIT=$(tput ritm)                           # remove italics
UL=$(tput smul)                            # turn underline on
NL=$(tput rmul)                            # turn underline off
RV=$(tput rev)                             # turn on reverse mode
ROWS=$(tput lines)
COLS=$(tput cols)

# functions
#----------------------------------

bl(){ ## write a blank line
    # Use: bl
    [ "$DEBUG" == "1" ] && set -x
    echo ""
}

center(){ ## center test on a line
    # Use: center "string" {optional fill character}
    [ "$DEBUG" == "1" ] && set -x 
    [[ $# == 0 ]] && return 1;
    declare -i TERM_COLS="$(tput cols)";
    declare -i str_len="${#1}";
    [[ $str_len -ge $TERM_COLS ]] && {
        echo "$1";
        return 0
    };
    declare -i filler_len="$(( (TERM_COLS - str_len) / 2 ))";
    [[ $# -ge 2 ]] && ch="${2:0:1}" || ch=" ";
    filler="";
    for ((i = 0; i < filler_len; i++ ))
    do
        filler="${filler}${ch}";
    done;
    printf "%s%s%s" "$filler" "$1" "$filler";
    [[ $(( (TERM_COLS - str_len) % 2 )) -ne 0 ]] && printf "%s" "${ch}";
    printf "\n";
    return 0
}


html(){ ## mark up code
    vim -f +"syn on" +"set nonu" +"set foldenable!" +"set nospell" +"run! syntax/2html.vim" +"wq" +"q" $1
}

log(){ ## creates a basic log entry $LOG must be defined
    # Use: log {entry}  
    [ "$DEBUG" == "1" ] && set -x
    logger -i -t "$NAME" "$*"
}

pause(){ ## simple pause routine
    # Use: pause  {optional number of seconds} or "-nt" for no time out 
    [ "$DEBUG" == "1" ] && set -x
    [ "$1" == "-nt" ] && TMOUT="" && shift
    echo "$BY";
    if [ $# -gt 0 ]
    then
        read -t $1 -r -p "${C}Hit any key (${BY}$1${C} second timeout)${N}" -n 1 FOO;
    else
        read -r -p "${C}Hit any key${N}" -n 1 FOO;
    fi;
    bl
}

rcf ()
{
    tput setaf $((RANDOM % 256))
}


strip-tags(){ ## remove HTML tags
  # Use: curl https://linuxhint.com/bash_cut_command/ --silent | strip-tags
  sed -e 's/<[^>]*.//g' -
}

xtitle(){ ## set window title
    # Use: xtitle "Text to display"
    printf "\033]0;%s\007" "$*"
}

version(){ ## display version and change history
    grep -E '^# Updated' $0
    bl
    sed -n "/' Changes/,/^ *$/p" <$0 |grep  -E -v 'sed -n|exit 0|}'
    exit 0
}

# setup
#----------------------------------

# this provides a quick way to edit all my scripts on the fly
if [ "$1" == "-E" ]
then
    vim $0
    sed -i -e "7s/.*/# Updated....: $(date)/" $0
    log "Updated $0"
    html $0
    cp $0 /var/www/unix
    mv $0.html /var/www/unix
    exit
fi

# display help if needed
if [ "$1" == "-h" ] || [ "$1" == "--help" ]
then
fmt -s -w $(tput cols) <<END
${BC}$NAME${N} {string} {count}

Draw a simple line terminal width:

$(line)

Options:

{string} is the characters you want to use
{count}  is the number of iterations 

- If no count is given then terminal width is used.
- If no character is given then a random one is used.

Use: line "=" (Create line of "=" terminal wide)
     line "-+"  20   (Create line of "-+" 20 characters wide)

-v # ${BY}Display version and change history${N}
END
exit
fi

# display version and change history
if [ "$1" == "-v" ] || [ "$1" == "--version" ]
then
    version
fi

# listing of required apps
command -v bash >/dev/null || sudo apt install bash -qyy


# main
#--------------------------- 
[ "$DEBUG" == 1 ] && set -x

# Check for string, if none than random
if [ "$#" -eq 0 ]
then
    string_length=${#my_string}
    random_index=$((RANDOM % string_length))
    ASCII="${my_string:$random_index:1}"
else
    ASCII="$1"
fi
CHAR=$(echo -n "$ASCII" |wc -c) # get the number of characters in first argument

# Check for countm if none than use number of columns
CNT="${2:-$(tput cols)}"    # set count to 2nd argument
#    [ "$CHAR" -gt 1 ] && CNT=$(($CNT/$CHAR)) # divide $CNT if needed
[ "$CHAR" -gt 1 ] && CNT=$(($CNT/2)) # divide $CNT if needed

# print line
rcf             # random color
until [ $CNT -eq 0 ]
do
        echo -n "$ASCII"
        CNT=$(($CNT - 1))
done
echo "$N"       # send new line and set color back