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

[玩转系统] 星期五乐趣:使用 Windows 终端取得进步

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

星期五乐趣:使用 Windows 终端取得进步


我已经使用 Microsoft 的新 Windows 终端有一段时间了。事实上,它已经成为我的 PowerShell 等标准命令行界面。我不确定在什么时候添加了其中一些功能,但我现在可以设置背景图像并指定在终端中显示它的位置。这促使我创建了如下所示的 PowerShell 7 体验:

[玩转系统] 星期五乐趣:使用 Windows 终端取得进步

不过,您可以在任何 Windows 终端配置文件中执行类似的操作。在您的配置文件中,您可以指定一些背景图像属性。

{
    "acrylicOpacity": 0.60000002384185791,
    "closeOnExit": true,
    "colorScheme": "Campbell",
    "commandline": "C:\Program Files\PowerShell\7-preview\preview\pwsh-preview.cmd -nologo",
    "cursorColor": "#FFFFFF",
    "cursorShape": "underscore",
    "fontFace": "Cascadia Code",
    "fontSize": 16,
    "guid": "{18b5ce4a-c242-46f0-980a-ffd888901802}",
    "historySize": 9001,
    "icon": "ms-appx:///ProfileIcons/{574e775e-4f2a-5b96-ac1e-a2962a402336}.png",
    "name": "PowerShell 7 Preview",
    "padding": "0, 0, 0, 0",
    "snapOnInput": true,
    "startingDirectory": "C:\",
    "tabTitle": "PowerShell 7 Preview",
    "backgroundImage": "C:\Users\Jeff\Pictures\Snover-head.jpg",
    "backgroundImageAlignment": "bottomRight",
    "backgroundImageStretchMode": "none",
    "backgroundImageOpacity": 0.5,
    "useAcrylic": true
},

请记住,由于您的设置存储在 JSON 文件中,因此您需要对任何路径值中的斜杠进行转义。我对结果很满意,但决定我可以从中获得更多乐趣。

旋转头

我有一组以 PowerShell 为主题的图形,它们在我的终端中看起来很棒。那么为什么不通过它们进行旋转呢?我建议保持图形缩略图大小,例如 150×150。我编写了一个简单的脚本来随机指定一个新的图形文件。

#Rotatehead.ps1
#rotate the bottom right graphic in a Windows Terminal

#get the path to the WindowsTerminal settings file
$termProfile = (Get-Item -path "$env:localappdata\Packages\Microsoft.WindowsTerminal_*\LocalState\profiles.json").FullName

#define an array of images
$pics = "C:\Users\Jeff\Pictures\Snover-head.png", 
"C:\Users\Jeff\Pictures\blue-robot-ps-thumb.jpg", 
"C:\Users\Jeff\Pictures\JasonH-thumb.jpg", 
"C:\Users\Jeff\Pictures\atomicps-thumb.jpg", 
"C:\Users\Jeff\Pictures\psicon.png", 
"C:\Users\Jeff\Pictures\talkbubble.png"

#get the raw json contents
$raw = Get-Content -path $termProfile -Raw

#create a PowerShell object from the raw data
$json  = Convertfrom-json -InputObject $raw

#find the current background image in the specified profile
$current = ($json.profiles).where({ $_.name -eq 'PowerShell 7 Preview' }).backgroundImage.replace("\","\")

#get a random new graphic
$new = ($pics | Get-Random).replace("\", "\")

#replace the old file with the new.
#There is an assumption that you aren't using the same image in multiple profiles
$raw.Replace($current,$new) | Set-Content -Path $termProfile

对简短的脚本进行了评论,这应该解释我在做什么。假设您已经在 Windows 终端配置文件中设置了图形文件之一。

我暂时使用 ConvertFrom-Json 将配置文件转换为对象。这样可以更轻松地从我的 PowerShell 7 预览配置文件中获取 BackgroundImage 值。从 json 转换时,PowerShell 在路径中使用单斜杠。当我定义 $current 时,我再次转义它们。这是因为我在当前路径上使用简单的字符串替换为新路径。有一个重要的假设:该图片未在任何其他配置文件中使用。

我最初的计划是将json文件转换为对象,更改对象,将其转换回json并将其写入配置文件。但是,当配置文件数据从 json 转换时,您最终会得到一个自定义对象。这通常不是一件坏事。但是,所有属性都是 NoteProperties,这意味着它们是只读的。因此,我最终得到了一个简单的查找和替换解决方案。

顺便说一句,我意识到我当前的代码并不能保证文件的唯一性。我可以随机选择我已经使用的文件。如果你有动力的话,我会把这个问题留给你来解决。

利用 PowerShell 配置文件

任务的最后一部分是每 X 分钟自动旋转一次头部。我本可以使用 PowerShell 计划作业。但决定调整我的提示功能。您可以通过运行(get-item function:prompt).scriptblock来获取当前提示函数的内容。您的代码将类似于函数提示 {}。这是我正在使用的简单提示功能。

function prompt {

$dt = Get-Date -Format "dd-MM-yy HH:mm:ss"
write-host "[$dt] " -ForegroundColor yellow -NoNewline
"PS$($PSVersionTable.PSversion.major).$($PSVersionTable.PSversion.minor) $($executionContext.SessionState.Path.CurrentLocation.path)$('>' * ($nestedPromptLevel + 1)) ";

#if this is the first time
if (-Not $global:lastChanged) {
  $global:lastChanged = Get-Date
}

$min = ((Get-Date) -$global:lastChanged).totalMinutes
#rotate grapichs every 11 minutes
if ($min -ge 11) {
    C:\scripts\RotateHead.ps1
    $global:lastChanged = Get-Date
}

} #close function

在我的版本中,我将时间戳和 PowerShell 版本显示为提示的一部分。我还使用全局变量来跟踪自上次更改以来的时间。我大约每 11 分钟调用一次 RoateHead 脚本,具体取决于我按下 Enter 的时间。这是我稍后的会议的样子。

[玩转系统] 星期五乐趣:使用 Windows 终端取得进步

现在,我的终端有了一些视觉变化。

如果您想尝试此操作,我强烈建议您首先制作 Windows 终端 profile.json 文件的备份副本。如果您想尝试我的图形,可以下载此 zip 文件。享受!

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

取消回复欢迎 发表评论:

关灯