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

[玩转系统] 添加 PowerShell 配置文件日历

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

添加 PowerShell 配置文件日历


你们中的一些人可能知道我的 PSCalendar 模块,您可以从 PowerShell 库安装该模块。该模块包含可用于显示基于控制台的日历的命令。日历命令允许您指定要突出显示的日期。这些日子可能会有特殊活动或约会。我通常使用 Show-Calendar 命令,因为它会写入主机并对输出进行着色。

[玩转系统] 添加 PowerShell 配置文件日历

此命令还有一个参数,可让您指定控制台中的位置。换句话说,您可以告诉 PowerShell 在哪里显示日历。我最近修复了该命令的一个错误,该错误产生的结果不太理想。现在,我可以使用 PowerShell 提示功能来显示日历。您需要做的第一件事是至少安装 1.10.0 版本的 PSCalendar 模块。确保它有效。

[玩转系统] 添加 PowerShell 配置文件日历

现在您可以通过提示功能将日历添加到控制台。下面是默认的 PowerShell 提示函数以及用于显示日历的附加代码。

#requires -modules @{ModuleName="PSCalendar";ModuleVersion="1.10.0"}  

Function prompt {

  #define a buffercell fill
  $fill = [system.management.automation.host.buffercell]::new(" ",$host.ui.RawUI.BackgroundColor,$host.ui.RawUI.BackgroundColor,"complete")
 
  #define a rectangle with an upper left corner X distance from the edge
  $left =$host.ui.RawUI.WindowSize.width - 42

  #need to adjust positioning based on buffer size of the console
  #is the cursor beyond the window size, ie have we scrolled down?
    if ($host.UI.RawUI.CursorPosition.Y -gt $host.UI.RawUI.WindowSize.Height) {
        $top = $host.ui.RawUI.CursorPosition.Y - $host.UI.RawUI.WindowSize.Height
    }
    else {
        $top = 0
    }
  #    System.Management.Automation.Host.Rectangle new(int left, int top, int right, int bottom)
  $r = [System.Management.Automation.Host.Rectangle]::new($left, 0, $host.ui.rawui.windowsize.width,$top+10)

  #clear the area for the calendar display
  $host.ui.rawui.SetBufferContents($r,$fill)

  #show the calendar in the upper right corner of the console
  $pos = [system.management.automation.host.coordinates]::new($left,0)
  Show-Calendar -Position $pos

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

# .Link
# https://go.microsoft.com/fwlink/?LinkID=225750
# .ExternalHelp System.Management.Automation.dll-help.xml

}

我将其另存为单独的 PowerShell 脚本文件,并在我的配置文件脚本中点源它。此函数不是为 PowerShell ISE 设计的,可能无法在 VS Code PowerShell 终端中正常工作。我什至不能保证它能在您的 PowerShell 控制台中工作。将此代码作为概念验证,而不是生产就绪。

该功能在控制台的右上角显示当前月份的日历。提示功能会清除该区域并重写日历。如果您的控制台至少有 120 个字符宽,则此方法效果最佳。否则,您将需要使用代码。

每次按 Enter 时都会运行提示功能。

[玩转系统] 添加 PowerShell 配置文件日历

我在 Windows 终端中运行,但此函数应该在传统的 PowerShell 控制台中运行,您可能有一个较大的滚动缓冲区。

日历将以绿色突出显示日期。我在个人资料中使用 $PSDefaultParameterValues 设置了一些值。

$PSDefaultParameterValues["*-*calendar:Highlightdate"]="1/18","1/8","1/16"

您可以找出从哪里获取对您重要的日期并填充哈希表。我希望你能让我知道你对这一切的看法。现在我认为我已经掌握了控制台缓冲区和矩形的句柄,我可能会重新访问一些旧的提示函数。希望我能给你带来更多有趣的东西。与此同时,不要错过下一个活动。享受!

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

取消回复欢迎 发表评论:

关灯