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

[玩转系统] 星期五乐趣 - PowerShell 圣诞节提示

作者:精品下载站 日期:2024-12-14 08:00:36 浏览:13 分类:玩电脑

星期五乐趣 - PowerShell 圣诞节提示


是时候写一篇新的周五有趣文章了。这些文章用“有趣”的方式教你如何使用PowerShell。最终目标可能很愚蠢,但希望这些技术和概念有用。今天我更新了提示功能。您可以通过创建一个名为“prompt”的函数并将其加载到 PowerShell 会话中来自定义 PowerShell 提示符。

提示功能仅在您的 PowerShell 会话运行时持续。跑步:

get-content function:prompt

查看您当前的提示。

因为圣诞节即将到来,并且考虑到 2020 年的情况,我们可能想要一些东西来点亮我们的一天。我有一个 PowerShell 提示功能,可以为您提供圣诞节倒计时,并包含表情符号和颜色。

<#
display a colorful Christmas countdown prompt

?? Christmas in 17.09:44:21 ⛄✨ PS C:\scripts>

this prompt requires a TrueType font
you would put this in your profile script so that it
only runs in December 1-24

#>

if ((Get-Date).Month -eq 12 -AND (Get-Date).Day -lt 25) {
  #dot source the emoji script
  . C:\scripts\PSEmoji.ps1

  #load the Christmas prompt

  Function Prompt {
    #get current year
    $year = (Get-Date).year
    #get a timespan between Christmas for this year and now

    $time = [datetime]"25 December $year" - (Get-Date)
    #turn the timespan into a string and strip off the milliseconds
    $timestring = $time.ToString().Substring(0, 11)

    #get random string of decorative characters
    #they can be pasted emojis or created from values
    $Snow = "❄️"
    # $snow = ConvertTo-Emoji 0x2744
    # $sparkles = "✨"
    $sparkles = ConvertTo-Emoji 0x2728
    #$snowman = "⛄"
    $snowman = ConvertTo-Emoji 0x26C4
    $santa = ConvertTo-Emoji 0x1F385
    $mrsClaus = ConvertTo-Emoji 0x1F936
    $tree = ConvertTo-Emoji 0x1F384
    $present = ConvertTo-Emoji 0x1F381
    $notes = ConvertTo-Emoji 0x1F3B5
    $bow = ConvertTo-Emoji 0x1F380
    $star = ConvertTo-Emoji 127775
    $shootingStar = ConvertTo-Emoji 127776
    $myChars = $santa, $mrsClaus, $tree, $present, $notes, $bow, $star, $shootingStar, $snow, $snowman,$sparkles
    #get a few random elements for the prompt
    $front = -join ($myChars | Get-Random -Count 2)
    $back = -join ($myChars | Get-Random -Count 2)

    #the text to display
    $text = "Christmas is coming in $timestring"

    #get each character in the text and randomly assign each a color using an ANSI sequence
    $colorText = $text.tocharArray() | ForEach-Object {
      $i = Get-Random -Minimum 1 -Maximum 50
      switch ($i) {
        { $i -le 50 -AND $i -ge 45 } { $seq = "$([char]0x1b)[1;5;38;5;199m" }
        { $i -le 45 -AND $i -ge 40 } { $seq = "$([char]0x1b)[1;5;38;11;199m" }
        { $i -le 40 -AND $i -ge 30 } { $seq = "$([char]0x1b)[1;38;5;50m" }
        { $i -le 20 -and $i -gt 15 } { $seq = "$([char]0x1b)[1;5;38;5;1m" }
        { $i -le 16 -and $i -gt 10 } { $seq = "$([char]0x1b)[1;38;5;47m" }
        { $i -le 10 -and $i -gt 5 }  { $seq = "$([char]0x1b)[1;5;38;5;2m" }
        default { $seq = "$([char]0x1b)[1;37m" }
      }
      "$seq$_$([char]0x1b)[0m"
    } #foreach

    #write the prompt text to the host on its own line
    Write-Host "$front $($colortext -join '') $back" #-NoNewline #-foregroundcolor $color

    #the function needs to write something to the pipeline
    "PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "

  } #end function

} #If December

您可以点源脚本文件,如果日期在 12 月 12 日和圣诞节之间,它将加载新的提示。提示将计算一个时间跨度,指示距离圣诞节还有多长时间。

    $year = (Get-Date).year
    #get a timespan between Christmas for this year and now

    $time = [datetime]"25 December $year" - (Get-Date)
    #turn the timespan into a string and strip off the milliseconds
    $timestring = $time.ToString().Substring(0, 11)

函数的其余部分是围绕此文本进行修饰:

    #the text to display
    $text = "Christmas is coming in $timestring"

我将显示从一组字符中随机选择的表情符号。您可以将表情符号粘贴到代码中,也可以使用我的 ConvertTo-Emoji 函数来转换十六进制值。我正在使用的粘贴表情符号可能无法在此处正确显示,因此我添加了等效表情符号。这是提示脚本中点源的脚本。

#These commands work best in a PowerShell session running in Windows Terminal.

# http://www.unicode.org/emoji/charts/full-emoji-list.html

Function ConvertTo-Emoji {
    [cmdletbinding()]
    [alias("cte")]
    Param(
        [parameter(Position = 0, Mandatory, ValueFromPipeline, HelpMessage = "Specify a value like 0x1F499 or 128153")]
        [int]$Value
    )
    Process {
        if ($env:wt_Session -OR ($host.name -match "studio")) {
            [char]::convertfromutf32($Value)
        }
        else {
            Write-Warning 'This command is only supported when running in Windows Terminal at this time.'
        }
    }
}
Function Show-Emoji {
    [cmdletbinding()]
    Param(
    [parameter(Position = 0, Mandatory,HelpMessage = "Enter the starting Unicode value")]
    [int32]$Start,
    [Parameter(HelpMessage = "How many items do you want to display?")]
    [int]$Count = 20
    )
    Write-verbose "Starting at $Start and getting $count items"
    $counter = 1

    Do {
        for ($i=1;$i -le 5;$i++) {
             write-verbose "Counter = $counter i = $i"
            $item = "{0} {1}  " -f (ConvertTo-Emoji ($start)),$start
            if ($counter -le $count) {
                write-host $item -NoNewline
                $counter++
                $start++
            }
        }
        write-host "`r"
    } until ($counter -ge $count)
}

提示的最后一部分是获取字符串并使用 ANSI 转义序列显示每个字符。我已经编写了该序列,因此它可以在 Windows PowerShell 和 PowerShell 7 中运行。

   #get each character in the text and randomly assign each a color using an ANSI sequence
    $colorText = $text.tocharArray() | ForEach-Object {
      $i = Get-Random -Minimum 1 -Maximum 50
      switch ($i) {
        { $i -le 50 -AND $i -ge 45 } { $seq = "$([char]0x1b)[1;5;38;5;199m" }
        { $i -le 45 -AND $i -ge 40 } { $seq = "$([char]0x1b)[1;5;38;11;199m" }
        { $i -le 40 -AND $i -ge 30 } { $seq = "$([char]0x1b)[1;38;5;50m" }
        { $i -le 20 -and $i -gt 15 } { $seq = "$([char]0x1b)[1;5;38;5;1m" }
        { $i -le 16 -and $i -gt 10 } { $seq = "$([char]0x1b)[1;38;5;47m" }
        { $i -le 10 -and $i -gt 5 }  { $seq = "$([char]0x1b)[1;5;38;5;2m" }
        default { $seq = "$([char]0x1b)[1;37m" }
      }
      "$seq$_$([char]0x1b)[0m"
    } #foreach

一些序列包括闪烁的字符。整行文本被写入主机。

Write-Host "$front $($colortext -join '') $back"

提示使用默认位置代码结束。

"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "

最初,我将 Write-Host 与 -NoNewLine 一起使用,以便文本位于位置之前。但我喜欢它独立的外观。想看吗?

将文件保存到同一目录,然后点源提示脚本。

. c:\scripts\christmasprompt.ps1

[玩转系统] 星期五乐趣 - PowerShell 圣诞节提示

由于 ANSI 颜色是随机的,因此每次按 Enter 键都会得到不同的结果。闪烁的字符使您的提示闪闪发光。

是的,这很愚蠢,没有任何实际目的,除非你是一个严重的拖延症患者并且需要提醒!但看看 PowerShell 技术:点源、开关、函数、子表达式、时间跨度、If 语句。这些都是您在实际 PowerShell 工作中将使用的所有内容。如果您仍在学习 PowerShell,有时查看以不同方式演示这些内容会有所帮助。

我希望你至少能尝试一下代码。该提示仅在 PowerShell 运行时持续,因此不存在风险。享受!

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

取消回复欢迎 发表评论:

关灯