当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 星期五有趣的 PowerShell ASCII 艺术

作者:精品下载站 日期:2024-12-14 07:57:25 浏览:15 分类:玩电脑

星期五有趣的 PowerShell ASCII 艺术


今天的帖子绝对是有趣的。事实上,我提前为你即将玩弄这段代码的下午表示歉意。一定年龄的人会记得拨号调制解调器和公告板。部分体验是视觉的。董事会运营商经常以某种 ASCII 艺术形式显示其网站的名称。

[玩转系统] 星期五有趣的 PowerShell ASCII 艺术

也许您想在模块或个人资料中显示精美的横幅。

[玩转系统] 星期五有趣的 PowerShell ASCII 艺术

这是使用 http://artii.herokuapp.com/ 提供的 API 将一段文本转换为 ASCII 艺术的简单方法。

您可以使用 Invoke-RestMethod 来调用 API。您需要指定文本字符串和字体。您可以通过运行以下命令获取字体列表:

invoke-restmethod https://artii.herokuapp.com/fonts_list

字体名称区分大小写。至于字体是什么样的,好吧,这就是你下午即将去的地方。并非每种字体都能为每个文本样本产生可用的结果。某些字体的数字和符号可能会变得有点奇怪。同样,您可以使用 Invoke-RestMethod 运行 make API,指定文本和字体。

[玩转系统] 星期五有趣的 PowerShell ASCII 艺术

这可能会变得棘手的是短语。你需要逃离空间之类的东西。

[玩转系统] 星期五有趣的 PowerShell ASCII 艺术

您将在 Invoke-RestMethod 调用中使用转义文本。但放松一下,我用一个函数让这件事变得更容易。

#requires -version 5.1
#ConvertTo-ASCIIArt.ps1

<#
font list at https://artii.herokuapp.com/fonts_list
font names are case-sensitive

invoke-restmethod https://artii.herokuapp.com/fonts_list

#>

Function ConvertTo-ASCIIArt {
    [cmdletbinding()]
    [alias("cart")]
    [outputtype([System.String])]
    Param(
        [Parameter(Position = 0, Mandatory, HelpMessage = "Enter a short string of text to convert", ValueFromPipeline)]
        [ValidateNotNullOrEmpty()]
        [string]$Text,
        [Parameter(Position = 1,HelpMessage = "Specify a font from https://artii.herokuapp.com/fonts_list. Font names are case-sensitive")]
        [ValidateNotNullOrEmpty()]
        [string]$Font = "big"
    )

    Begin {
        Write-Verbose "[$((Get-Date).TimeofDay) BEGIN] Starting $($myinvocation.mycommand)"
    } #begin

    Process {
        Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Processing $text with font $Font"
        $testEncode = [uri]::EscapeDataString($Text)
        $url = "http://artii.herokuapp.com/make?text=$testEncode&font=$Font"
        Try {
            Invoke-Restmethod -Uri $url -DisableKeepAlive -ErrorAction Stop
        }
        Catch {
            Throw $_
        }
    } #process
    End {
        Write-Verbose "[$((Get-Date).TimeofDay) END    ] Ending $($myinvocation.mycommand)"
    } #end
}

我什至定义了一个别名,使之变得非常简单。

[玩转系统] 星期五有趣的 PowerShell ASCII 艺术

一旦你有了这个,你就可以用 Write-Host 让它变得有趣。

[玩转系统] 星期五有趣的 PowerShell ASCII 艺术

或者看一下这段代码:

$t = @"
  _____                       _____ _          _ _ 
 |  __ \                     / ____| |        | | |
 | |__) |____      _____ _ __ (___ | |__   ___| | |
 |  ___/ _ \ \ /\ / / _ \ '__\___ \| '_ \ / _ \ | |
 | |  | (_) \ V  V /  __/ |  ____) | | | |  __/ | |
 |_|   \___/ \_/\_/ \___|_| |_____/|_| |_|\___|_|_|
                                                   
"@

for ($i=0;$i -lt $t.length;$i++) {
if ($i%2) {
 $c = "red"
}
elseif ($i%5) {
 $c = "yellow"
}
elseif ($i%7) {
 $c = "green"
}
else {
   $c = "white"
}
write-host $t[$i] -NoNewline -ForegroundColor $c
}

我在控制台创建了艺术文本并将其复制到剪贴板,我可以将其粘贴为此处的字符串。

[玩转系统] 星期五有趣的 PowerShell ASCII 艺术

您可以做的事情没有止境。

[玩转系统] 星期五有趣的 PowerShell ASCII 艺术

所以玩得开心吧。抱歉打扰了你的下午。如果您为此提出一个用例,我很想听听。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯