Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I output ASCII Art to console?

I would like to spice up my scripts with some ASCII art to be displayed on completion of a process.

I have 2 thoughts on how to output ASCII art to console. Hopefully someone who knows more than I do can direct us to what the command is and what method is 'Better'.

  1. Output a multi-line Write-Host? I tried to do this earlier and it didnt work. It threw a bunch of errors. Maybe I wasnt doing the 'multi-line' version (if a multi-line) version exists.

  2. Save the ASCII art to a .txt file, then in my script somehow grab and read the contents of that .txt file and then write the contents to the console in the specified area.

Which way is better? How can I implement this?

like image 939
jward01 Avatar asked Jan 26 '16 19:01

jward01


3 Answers


It's a piece of my PowerShell $PROFILE:

# Personalize the console
$Host.UI.RawUI.WindowTitle = "Windows Powershell " + $Host.Version;

# Draw welcome screen
Write-Host -ForegroundColor DarkYellow "                       _oo0oo_"
Write-Host -ForegroundColor DarkYellow "                      o8888888o"
Write-Host -ForegroundColor DarkYellow "                      88`" . `"88"
Write-Host -ForegroundColor DarkYellow "                      (| -_- |)"
Write-Host -ForegroundColor DarkYellow "                      0\  =  /0"
Write-Host -ForegroundColor DarkYellow "                    ___/`----'\___"
Write-Host -ForegroundColor DarkYellow "                  .' \\|     |// '."
Write-Host -ForegroundColor DarkYellow "                 / \\|||  :  |||// \"
Write-Host -ForegroundColor DarkYellow "                / _||||| -:- |||||- \"
Write-Host -ForegroundColor DarkYellow "               |   | \\\  -  /// |   |"
Write-Host -ForegroundColor DarkYellow "               | \_|  ''\---/''  |_/ |"
Write-Host -ForegroundColor DarkYellow "               \  .-\__  '-'  ___/-. /"
Write-Host -ForegroundColor DarkYellow "             ___'. .'  /--.--\  `. .'___"
Write-Host -ForegroundColor DarkYellow "          .`"`" '<  `.___\_<|>_/___.' >' `"`"."
Write-Host -ForegroundColor DarkYellow "         | | :  `- \`.;`\ _ /`;.`/ - ` : | |"
Write-Host -ForegroundColor DarkYellow "         \  \ `_.   \_ __\ /__ _/   .-` /  /"
Write-Host -ForegroundColor DarkYellow "     =====`-.____`.___ \_____/___.-`___.-'====="
Write-Host -ForegroundColor DarkYellow "                       `=---='"


# Create frequent commands
New-Alias -Name vsc -Value "D:\Program Files\VSCode\Code.exe";
$HOSTS = "$env:SystemRoot\system32\drivers\etc\hosts";
$Desktop = "$env:USERPROFILE\Desktop"
$Documents = "$env:USERPROFILE\Documents"
$TimestampServer = "http://timestamp.digicert.com"
Set-Location D:\Scripts;

Just a reference.

like image 64
Chenry Lee Avatar answered Nov 09 '22 15:11

Chenry Lee


Just use a newline delimited string, here-string or Get-Content -Raw if you have a source file. Both will give you a single multiline string without fuss. One thing the here-string is good for is not having to worry about the quotes you use (which could be a distinct possibility with ASCII art). An example using a here-string would be the following:

$text = @"
 "ROFL:ROFL:ROFL:ROFL"
         _^___
 L    __/   [] \    
LOL===__        \ 
 L      \________]
         I   I
        --------/
"@

Or if you choose to go the source file approach:

$text = Get-Content -Raw $path

Or if you only have PowerShell 2.0 $text = Get-Content $path | Out-String

Either way you can follow up with Write-Host or whatever you want after that.


Getting funky with colours would require some special logic that, as far as I know, does not currently exist. Making a colourized output randomizer is simple enough. To get a basic idea I present Get-Funky

function Get-Funky{
    param([string]$Text)

    # Use a random colour for each character
    $Text.ToCharArray() | ForEach-Object{
        switch -Regex ($_){
            # Ignore new line characters
            "`r"{
                break
            }
            # Start a new line
            "`n"{
                Write-Host " ";break
            }
            # Use random colours for displaying this non-space character
            "[^ ]"{
                # Splat the colours to write-host
                $writeHostOptions = @{
                    ForegroundColor = ([system.enum]::GetValues([system.consolecolor])) | get-random
                    # BackgroundColor = ([system.enum]::GetValues([system.consolecolor])) | get-random
                    NoNewLine = $true
                }
                Write-Host $_ @writeHostOptions
                break
            }
            " "{Write-Host " " -NoNewline}

        } 
    }
}

That will take a newline delimited string and use random host colours for displaying the output. We use splatting with $writeHostOptions so you could easily control the colours. You could even have parameters that force one of the colours or disabled colourizing of one etc. Here is some sample output:

$art = " .:::.   .:::.`n:::::::.:::::::`n:::::::::::::::
':::::::::::::'`n  ':::::::::'`n    ':::::'`n      ':'"
Get-Funky $art 

Funky heart

Heart ascii art found at asciiworld.com

like image 14
Matt Avatar answered Nov 09 '22 15:11

Matt


PowerShell can do multi-line strings (with all kinds of strings; this isn't limited to here-strings), so the following should work, actually:

Write-Host 'first line
second line
third line'

Of course, if you're not in a function and don't care about any return values, you can just omit Write-Host completely. If you're reading from an external file, then in the same vein, Get-Content would just work:

Get-Content ascii.txt

if you really need Write-Host, then just use

Get-Content | % { Write-Host $_ }
like image 3
Joey Avatar answered Nov 09 '22 14:11

Joey