[英]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. 我想用一些ASCII艺术为我的脚本增添趣味,以便在完成一个过程时显示。
I have 2 thoughts on how to output ASCII art to console. 我有两个关于如何将ASCII艺术输出到控制台的想法。 Hopefully someone who knows more than I do can direct us to what the command is and what method is 'Better'.
希望知道比我更多的人可以指导我们了解命令是什么以及什么方法是“更好”。
Output a multi-line Write-Host
? 输出多行
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.
也许我没有做“多线”版本(如果有多行)版本。
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. 将ASCII art保存为.txt文件,然后在我的脚本中以某种方式获取并读取该.txt文件的内容,然后将内容写入指定区域的控制台。
Which way is better? 哪种方式更好? How can I implement this?
我该如何实现呢?
Just use a newline delimited string, here-string or Get-Content -Raw
if you have a source file. 如果您有源文件,只需使用换行符分隔的字符串,here-string或
Get-Content -Raw
。 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).
here-string的一个好处是不必担心你使用的引号(这可能是ASCII艺术的一个明显的可能性)。 An example using a here-string would be the following:
使用here-string的示例如下:
$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
或者,如果您只有PowerShell 2.0
$text = Get-Content $path | Out-String
$text = Get-Content $path | Out-String
Either way you can follow up with Write-Host
or whatever you want after that. 无论哪种方式,您都可以跟进
Write-Host
或之后想要的任何内容。
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
为了得到一个基本的想法,我提出了
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. 我们使用
$writeHostOptions
以便您可以轻松控制颜色。 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
Heart ascii art found at asciiworld.com 心脏ascii艺术在asciiworld.com找到
It's a piece of my PowerShell $PROFILE
: 这是我的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. 只是一个参考。
PowerShell can do multi-line strings (with all kinds of strings; this isn't limited to here-strings), so the following should work, actually: PowerShell可以执行多行字符串(包含各种字符串;这不仅限于here-strings),因此以下内容应该可行,实际上:
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. 当然,如果您不在函数中并且不关心任何返回值,则可以完全省略
Write-Host
。 If you're reading from an external file, then in the same vein, Get-Content
would just work: 如果您正在读取外部文件,那么
Get-Content
就可以正常工作:
Get-Content ascii.txt
if you really need Write-Host, then just use 如果你真的需要Write-Host,那就用吧
Get-Content | % { Write-Host $_ }
Below is an example that does multi-line using Write-Host 下面是使用Write-Host执行多行的示例
Write-Host " *** ** ** ** ** * ********
>> **** ******* ********* *
>> ** ***** ******** *
>> * *** ****** *
>> * **** *
>> **
>> *
>> "
UPDATED : Removed the backslashes as they are not needed in PowerShell 更新 :删除了PowerShell中不需要的反斜杠
Here-strings are also perfectly good arguments to Write-Host
: Here-strings也是
Write-Host
完美参数:
$multiplelines = @"
Multi-lines?!
Well ... Why
not?
"@
Write-Host $multiplelines
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.