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

时间:2016-01-26 19:20:27

标签: powershell console ascii

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

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

  1. 输出多行std::string?我试图提前做到这一点并没有奏效。它引发了一堆错误。也许我没有做“多行”版本(如果有多行)版本。

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

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

5 个答案:

答案 0 :(得分:12)

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

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

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

$text = Get-Content -Raw $path

或者,如果您只有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}

        } 
    }
}

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

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

Funky heart

asciiworld.com

找到心脏ascii艺术品

答案 1 :(得分: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;

只是一个参考。

答案 2 :(得分:3)

PowerShell可以执行多行字符串(包含各种字符串;这不仅限于here-strings),因此以下内容应该可以实现:

Write-Host 'first line
second line
third line'

当然,如果您不在某个功能中并且不关心任何返回值,则可以完全省略Write-Host。如果您正在从外部文件中读取内容,那么Get-Content就可以正常工作了:

Get-Content ascii.txt

如果你真的需要Write-Host,那么只需使用

Get-Content | % { Write-Host $_ }

答案 3 :(得分:2)

以下是使用Write-Host

执行多行的示例
Write-Host "  ***   ** ** ** ** * ********
>>               **** ******* ********* *                     
>>                **   *****   ********  *                    
>>                *     ***     ******    *                   
>>                       *       ****     *                   
>>                                **                         
>>                                 *                          
>> "

更新:删除了PowerShell中不需要的反斜杠

答案 4 :(得分:2)

Here-字符串也是Write-Host的完美参数:

$multiplelines = @"
Multi-lines?!

Well ... Why
not?

"@

Write-Host $multiplelines