#!/pkg/bin/ksh
# ---------------------------------------------------------------------
# isis_show_functions - Useful shell functions for show commands
#
# March 2022, Paul Wells
#
# Copyright (c) 2022 by cisco Systems, Inc.
# All rights reserved.
#--------------------------------------------------------------------

# Function: print_char 
# 
# This prints a row of a specified character.  
# 
# Argument: Number of times to print the character
# 
# Argument: Character to print
print_char() {
    __j=1; while [ "$__j" -le "$1" ]; do 
        echo -n "$2"
        __j=$(($__j + 1))
    done
}

# Function: print_heading_internal 
#
# This prints a string centered with a row of a character 
# each side of it with one space between each end of the string and the 
# characters. 
# 
# Argument: String to print
# 
# Argument: Character to print around string
# 
# Note: This function assumes that each line is $__line_length characters long. 
print_heading_internal() {
    __title="$1"
    __title_length=${#__title}
    # As we leave a space each side of the title, we need to subtract two
    # from the line length. 
    __adjusted_line_length=$((__line_length - 2))
    __total_char_length=$(( $__adjusted_line_length - $__title_length))
    __char_length=$(($__total_char_length / 2))
    
    if [[ $__char_length -lt 3 ]]; then
        __char_length=3
    fi
    echo -e "\n"
    print_char $__char_length "$2"
     
    echo -n " $__title "
    # If there are an odd number of dashes in total, print the extra dash
    # after the string.
    if [ $(($__total_char_length % 2)) = 0 ]; then  
         print_char $__char_length "$2"
    else
         print_char $(($__char_length + 1)) "$2"
    fi

    echo -e "\n"
}

# Function: print_command_heading_internal 
# This prints a heading consisting of a centered string surrounded by dashes. 
#
# Argument: String to print
print_command_heading() {
   __title="$1"
   __substituted_title=$(eval "echo $__title")
   print_heading_internal "$__substituted_title" "+"
}
