如何将 ASCII 艺术输出到控制台?

12

我想在脚本完成时显示一些 ASCII 艺术品来增加趣味。

对于如何在控制台输出 ASCII 艺术字符,我有两个想法。希望比我更了解的人能指导我们使用哪个命令和哪种方法更好。

  1. 通过多行 Write-Host 输出?我之前尝试过这样做,但没有成功。它抛出了一堆错误。也许我没有使用“多行”版本(如果有的话)。

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

哪种方法更好?如何实现这个效果?


你的输入来自哪里?还是你正在内联输入?Here-String 是内联输入的最佳方法。 - Matt
@Matt:实际上,在PowerShell中,我很少需要使用here-strings,因为普通字符串基本上可以完成相同的工作。 - Joey
输入是 ASCII 艺术。输入可以嵌入到脚本内部,也可以在外部 .txt 文件中,需要检索和读取。 - jward01
@Joey 当我需要进行格式化或正则表达式匹配时,我会更多地使用它们。将它们保存在脚本中仍然是一种简单的方法,但并非必需。 - Matt
@joey,这里的字符串处理引号更好。 ASCII艺术中可能会有引号。 - Matt
@Matt 我认为Get-Content -Raw是最好的方法。谢谢你的帮助!我已经让它工作了 :) - jward01
5个回答

14

如果你有一个源文件,请使用一个以换行分隔的字符串、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 的展开语法,因此您可以轻松控制颜色。您甚至可以有一些参数强制使用其中一个颜色或禁用某个颜色等。以下是一些示例输出:

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

花式心形图片

asciiworld.com 上找到的心形ASCII艺术。


10
这是我的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;

只是一个参考。


1
我会完全将其转换为单个多行字符串。这样你只需要更改一次颜色! - Matt
编写模式@'blabla'@更易于阅读,我只是从互联网上复制了它。 :) - Chenry Lee

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 $_ }

2
以下是使用Write-Host实现多行输出的示例:
Write-Host "  ***   ** ** ** ** * ********
>>               **** ******* ********* *                     
>>                **   *****   ********  *                    
>>                *     ***     ******    *                   
>>                       *       ****     *                   
>>                                **                         
>>                                 *                          
>> "

更新: 删除反斜杠,因为在PowerShell中不需要它们。


那里的反斜杠有什么作用呢? - Joey
继续下一行的代码... 我猜你也可以不用它。 - vmachan
2
在PowerShell字符串中,反斜杠不是转义字符,甚至不需要转义。 - Ansgar Wiechers

2

Here-strings(这里字符串)也是传递给 Write-Host 的完美参数:

$multiplelines = @"
Multi-lines?!

Well ... Why
not?

"@

Write-Host $multiplelines

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接