如何将ASCII Art输出到控制台?

jwa*_*d01 9 console powershell ascii

我想用一些ASCII艺术为我的脚本增添趣味,以便在完成一个过程时显示.

我有两个关于如何将ASCII艺术输出到控制台的想法.希望知道比我更多的人可以指导我们了解命令是什么以及什么方法是"更好".

  1. 输出多行Write-Host?我试图提前做到这一点并没有奏效.它引发了一堆错误.也许我没有做"多线"版本(如果有多行)版本.

  2. 将ASCII art保存为.txt文件,然后在我的脚本中以某种方式获取并读取该.txt文件的内容,然后将内容写入指定区域中的控制台.

哪种方式更好?我该如何实现呢?

Mat*_*att 12

只需使用换行符分隔的字符串,here-string或者Get-Content -Raw如果您有源文件.两者都会给你一个单线多字符串而不用大惊小怪.here-string的一个好处是不必担心你使用的引号(这可能是ASCII艺术的一个明显的可能性).使用here-string的示例如下:

$text = @"
 "ROFL:ROFL:ROFL:ROFL"
         _^___
 L    __/   [] \    
LOL===__        \ 
 L      \________]
         I   I
        --------/
"@
Run Code Online (Sandbox Code Playgroud)

或者,如果您选择采用源文件方法:

$text = Get-Content -Raw $path
Run Code Online (Sandbox Code Playgroud)

或者,如果您只有PowerShell 2.0 $text = Get-Content $path | Out-String

无论哪种方式,您都可以跟进Write-Host或者随后想要的任何内容.


如果我知道,获取颜色时尚需要一些特殊的逻辑.制作彩色输出随机数发生器非常简单.为了得到我提出的基本想法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}

        } 
    }
}
Run Code Online (Sandbox Code Playgroud)

这将采用换行符分隔的字符串并使用随机主机颜色来显示输出.我们使用splatting,$writeHostOptions因此您可以轻松控制颜色.您甚至可以使用强制其中一种颜色的参数或禁用一种颜色等等.以下是一些示例输出:

$art = " .:::.   .:::.`n:::::::.:::::::`n:::::::::::::::
':::::::::::::'`n  ':::::::::'`n    ':::::'`n      ':'"
Get-Funky $art 
Run Code Online (Sandbox Code Playgroud)

时髦的心

心脏ascii艺术在asciiworld.com找到


小智 7


这是我的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;
Run Code Online (Sandbox Code Playgroud)

只是一个参考.