internal/functions/convertto-asciiart.ps1


<#
    .SYNOPSIS
        Converts a given string into ASCII art representation.
         
    .DESCRIPTION
        The `ConvertTo-AsciiArt` function takes an input string and converts each character into its corresponding ASCII art representation.
        It uses a predefined hashtable to map characters (e.g., numbers) to multi-line ASCII art patterns.
         
    .PARAMETER str
        The input string to be converted into ASCII art. This parameter is mandatory.
         
    .EXAMPLE
        ConvertTo-AsciiArt -String "123"
         
        This example converts the string "123" into its ASCII art representation.
         
    .NOTES
        - Only characters defined in the `$chars` hashtable will be converted.
        - Characters not present in the hashtable will be ignored.
#>

function ConvertTo-AsciiArt {
    Param(
        [parameter(position = 0)]
        [string]$String = ""
    )
    $chars = @{
'0' = @'
  ___
 / _ \
| | | |
| | | |
| |_| |
 \___/
'@
.Split("`n")
'1' = @'
 __
/_ |
 | |
 | |
 | |
 |_|
'@
.Split("`n")
'2' = @'
 ___
|__ \
   ) |
  / /
 / /_
|____|
'@
.Split("`n")
'3' = @'
 ____
|___ \
  __) |
 |__ <
 ___) |
|____/
'@
.Split("`n")
'4' = @'
 _ _
| || |
| || |_
|__ _|
   | |
   |_|
'@
.Split("`n")
'5' = @'
 _____
| ____|
| |__
|___ \
 ___) |
|____/
'@
.Split("`n")
'6' = @'
   __
  / /
 / /_
| '_ \
| (_) |
 \___/
'@
.Split("`n")
'7' = @'
 ______
|____ |
    / /
   / /
  / /
 /_/
'@
.Split("`n")
'8' = @'
  ___
 / _ \
| (_) |
 > _ <
| (_) |
 \___/
'@
.Split("`n")
'9' = @'
  ___
 / _ \
| (_) |
 \__, |
   / /
  /_/
'@
.Split("`n")
'.' = @'
    
    
    
    
 _
(_)
'@
.Split("`n")
' ' = @'
    
    
    
    
    
    
'@
.Split("`n")
'v' = @'
    
    
__ __
\ \ / /
 \ V /
  \_(_)
'@
.Split("`n")
'F' = @"
 ______
| ____|
| |__
| __|
| |
|_|
"@
.Split("`n")
'S' = @"
  _____
 / ____|
| (___
 \___ \
 ____) |
|_____/
"@
.Split("`n")
'C' = @"
  _____
 / ____|
| |
| |
| |____
 \_____|
"@
.Split("`n")
'P' = @"
 ______
| __ \
| |__) |
| ____/
| |
|_|
"@
.Split("`n")
}    
    # Output the final ASCII art
    if ($PSVersionTable.PSVersion.Major -lt 6) {
        # PowerShell 5 or earlier
        # Initialize an array to hold the ASCII art lines
        0..5 | ForEach-Object {
            $line = $_
            $String.ToCharArray() | ForEach-Object {
                $ch = $chars."$_"
                if ($ch) {
                    Write-Host -noNewline $ch[$line]
                }
            }
            Write-Host
        }

    } else {
        # Determine the maximum width of any ASCII art character
        $maxWidth = ($chars.Values | ForEach-Object {
            $_ | ForEach-Object { $_.Length }
        } | Measure-Object -Maximum).Maximum
        
        # Ensure $maxWidth is an integer
        $maxWidth = [int]$maxWidth
        
        # Initialize an array to hold the ASCII art lines
        $asciiArtLines = @()
        
        # Process each character in the input string
        foreach ($char in $String.ToCharArray()) {
            $char = $char.ToString().ToUpper() # Convert to uppercase for case insensitivity
            if ($chars.ContainsKey($char)) {
                $asciiArt = $chars[$char]
                # Pad each line of the ASCII art to the maximum width
                $asciiArt = $asciiArt | ForEach-Object { 
                    if ([string]::IsNullOrEmpty($_)) { 
                        " ".PadRight($maxWidth) 
                    } else { 
                        $_.PadRight($maxWidth) 
                    } 
                }
                # Add each line of the ASCII art to the corresponding line in the output
                for ($i = 0; $i -lt $asciiArt.Count; $i++) {
                    if ($asciiArtLines.Count -le $i) {
                        $asciiArtLines += $asciiArt[$i]
                    } else {
                        $asciiArtLines[$i] += " " + $asciiArt[$i]
                    }
                }
            }
        }
        return $asciiArtLines -join "`n"
    }
}