1 概述
在我的上一篇文章中,我们学会了通过 Scoop 和 WSL 快速搭建各种开发环境,但是有没有发现默认的 powershell
很丑而且不好用,今天教大家怎样安装 Windows Terminal
以及美化,Windows Terminal
是 WSL2
的理想配套,它速度快、可配置、外观漂亮,并且提供了 Windows 和 Linux 开发的所有优点。
不多说,先看下美化后的 Windows Terminal。
2 安装
以管理员权限打开 powershell
并运行下面命令:
# 安装 Windows Terminal 最新版本
scoop install -g extras/windows-terminal
# 安装 powershell 最新版本, windows 默认的 powershell 版本很低
scoop install -g main/pwsh
# 安装字体
scoop install -g nerd-fonts/Cascadia-Code
# 安装openssh
scoop install -g main/openssh
3 windows-terminal 配置
3.1 配置右键菜单
新建 windows-terminal.reg
文件,文件内容如下:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\Background\shell\WindowsTerminal]
@="Windows Terminal Here"
"Icon"="D:\\Softwares\\Scoop\\GlobalApps\\apps\\windows-terminal\\current\\WindowsTerminal.exe"
[HKEY_CLASSES_ROOT\Directory\Background\shell\WindowsTerminal\command]
@="D:\\Softwares\\Scoop\\GlobalApps\\apps\\windows-terminal\\current\\WindowsTerminal.exe"
其中的路径是安装 windows-terminal
的路径,配置完成后双击运行。
3.2 配置 pwsh
-
右键打开
windows-terminal
, 选择设置。 -
选择添加新的配置文件。
-
选择复制配置文件。
-
修改配置文件为
pwsh
的配置文件。 -
修改启动为
pwsh
。 -
选择配色方案
3.3 全局配置
打开配置文件,进行全局配置,全局配置是在 defaults
下配置的,list
下是单独配置每个命令行的,list
下的配置会覆盖全局配置的,如果在 defaults
下已经配置了,list
下相同的配置需要删除,全局配置内容如下:
"defaults": {
// 配置背景图片
"backgroundImage": "D:\\SoftwareConfigs\\Windows-terminal\\background.jpg",
// 配置透明度
"backgroundImageOpacity": 0.1,
// 配置主题
"colorScheme": "One Half Dark",
// 配置字体
"font": {
"face": "Cascadia Mono",
"size": 12
},
// 配置历史记录大小
"historySize": 9001,
// 配置启动时进入的目录,配置为 null时会进入右键打开时所在的目录
"startingDirectory": null,
// 选择文本底色
"selectionBackground": "#EE3A8C"
},
4 pwsh 配置
4.1 安装插件
为了让 pwsh
更好用,我们需要安装一些 powershell
的插件,运行下面命令进行安装:
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
Set-ExecutionPolicy remotesigned
# PowerShell 的彩色文件列表
Install-Module -AllowClobber Get-ChildItemColor -Scope AllUsers
# 增强 git 功能
PowerShellGet\Install-Module -Name posh-git -Scope AllUsers -AllowPrerelease -Force
PowerShellGet\Install-Module posh-sshell -Scope AllUsers
# 美化 powershell
Install-Module -Name oh-my-posh -Scope AllUsers
# git 别名支持
Install-Module -Name git-aliases -Scope AllUsers
# powershell 增强
Install-Module -Name PSReadLine -AllowPrerelease -Scope AllUsers -Force -SkipPublisherCheck
# 系统信息输出
Install-Module windows-screenfetch -Scope AllUsers -AllowClobber
Install-Module -Name InstallModuleFromGitHub -Scope AllUsers
4.2 windows-screenfetch 配置
windows-screenfetch
已经不适用于最新版本的 powershell
,所以需要更改下源码,找到 C:\Program Files\PowerShell\Modules\windows-screenfetch\1.0.2
下的 Data.psm1
文件,将里面所有内容替换为下面内容:
Add-Type -AssemblyName System.Windows.Forms
Function Get-SystemSpecifications()
{
$UserInfo = Get-UserInformation;
$OS = Get-OS;
$Kernel = Get-Kernel;
$Uptime = Get-Uptime;
$Motherboard = Get-Mobo;
$Shell = Get-Shell;
$Displays = Get-Displays;
$WM = Get-WM;
$Font = Get-Font;
$CPU = Get-CPU;
$GPU = Get-GPU;
$RAM = Get-RAM;
$Disks = Get-Disks;
[System.Collections.ArrayList] $SystemInfoCollection =
$UserInfo,
$OS,
$Kernel,
$Uptime,
$Motherboard,
$Shell,
$Displays,
$WM,
$Font,
$CPU,
$GPU,
$RAM;
foreach ($Disk in $Disks)
{
[void]$SystemInfoCollection.Add($Disk);
}
return $SystemInfoCollection;
}
Function Get-LineToTitleMappings()
{
$TitleMappings = @{
0 = "";
1 = "OS: ";
2 = "Kernel: ";
3 = "Uptime: ";
4 = "Motherboard: ";
5 = "Shell: ";
6 = "Resolution: ";
7 = "Window Manager: ";
8 = "Font: ";
9 = "CPU: ";
10 = "GPU ";
11 = "RAM: ";
};
return $TitleMappings;
}
Function Get-UserInformation()
{
return $env:USERNAME + "@" + (Get-CimInstance Win32_OperatingSystem).CSName;
}
Function Get-OS()
{
return (Get-CimInstance Win32_OperatingSystem).Caption + " " +
(Get-CimInstance Win32_OperatingSystem).OSArchitecture;
}
Function Get-Kernel()
{
return (Get-CimInstance Win32_OperatingSystem).Version;
}
Function Get-Uptime()
{
$Uptime = (Get-CimInstance Win32_OperatingSystem).LocalDateTime - (Get-CimInstance Win32_OperatingSystem).LastBootUpTime;
$FormattedUptime = $Uptime.Days.ToString() + "d " + $Uptime.Hours.ToString() + "h " + $Uptime.Minutes.ToString() + "m " + $Uptime.Seconds.ToString() + "s ";
return $FormattedUptime;
}
Function Get-Mobo()
{
$Motherboard = Get-CimInstance Win32_BaseBoard | Select-Object Manufacturer, Product;
return $Motherboard.Manufacturer + " " + $Motherboard.Product;
}
Function Get-Shell()
{
return "PowerShell $($PSVersionTable.PSVersion.ToString())";
}
Function Get-Displays()
{
$Displays = New-Object System.Collections.Generic.List[System.Object];
# This gives the available resolutions
$monitors = Get-CimInstance -N "root\wmi" -Class WmiMonitorListedSupportedSourceModes
foreach($monitor in $monitors)
{
# Sort the available modes by display area (width*height)
$sortedResolutions = $monitor.MonitorSourceModes | sort -property {$_.HorizontalActivePixels * $_.VerticalActivePixels}
$maxResolutions = $sortedResolutions | select @{N="MaxRes";E={"$($_.HorizontalActivePixels) x $($_.VerticalActivePixels) "}}
$Displays.Add(($maxResolutions | select -last 1).MaxRes);
}
return $Displays;
}
Function Get-WM()
{
return "DWM";
}
Function Get-Font()
{
return "Segoe UI";
}
Function Get-CPU()
{
return (((Get-CimInstance Win32_Processor).Name) -replace '\s+', ' ');
}
Function Get-GPU()
{
return (Get-CimInstance Win32_DisplayConfiguration).DeviceName;
}
Function Get-RAM()
{
$FreeRam = ([math]::Truncate((Get-CimInstance Win32_OperatingSystem).FreePhysicalMemory / 1KB));
$TotalRam = ([math]::Truncate((Get-CimInstance Win32_ComputerSystem).TotalPhysicalMemory / 1MB));
$UsedRam = $TotalRam - $FreeRam;
$FreeRamPercent = ($FreeRam / $TotalRam) * 100;
$FreeRamPercent = "{0:N0}" -f $FreeRamPercent;
$UsedRamPercent = ($UsedRam / $TotalRam) * 100;
$UsedRamPercent = "{0:N0}" -f $UsedRamPercent;
return $UsedRam.ToString() + "MB / " + $TotalRam.ToString() + " MB " + "(" + $UsedRamPercent.ToString() + "%" + ")";
}
Function Get-Disks()
{
$FormattedDisks = New-Object System.Collections.Generic.List[System.Object];
$NumDisks = (Get-CimInstance Win32_LogicalDisk).Count;
if ($NumDisks)
{
for ($i=0; $i -lt ($NumDisks); $i++)
{
$DiskID = (Get-CimInstance Win32_LogicalDisk)[$i].DeviceId;
$FreeDiskSize = (Get-CimInstance Win32_LogicalDisk)[$i].FreeSpace
$FreeDiskSizeGB = $FreeDiskSize / 1073741824;
$FreeDiskSizeGB = "{0:N0}" -f $FreeDiskSizeGB;
$DiskSize = (Get-CimInstance Win32_LogicalDisk)[$i].Size;
$DiskSizeGB = $DiskSize / 1073741824;
$DiskSizeGB = "{0:N0}" -f $DiskSizeGB;
$FreeDiskPercent = ($FreeDiskSizeGB / $DiskSizeGB) * 100;
$FreeDiskPercent = "{0:N0}" -f $FreeDiskPercent;
$UsedDiskSizeGB = $DiskSizeGB - $FreeDiskSizeGB;
$UsedDiskPercent = ($UsedDiskSizeGB / $DiskSizeGB) * 100;
$UsedDiskPercent = "{0:N0}" -f $UsedDiskPercent;
$FormattedDisk = "Disk " + $DiskID.ToString() + " " +
$UsedDiskSizeGB.ToString() + "GB" + " / " + $DiskSizeGB.ToString() + "GB " +
"(" + $UsedDiskPercent.ToString() + "%" + ")";
$FormattedDisks.Add($FormattedDisk);
}
}
else
{
$DiskID = (Get-CimInstance Win32_LogicalDisk).DeviceId;
$FreeDiskSize = (Get-CimInstance Win32_LogicalDisk).FreeSpace
$FreeDiskSizeGB = $FreeDiskSize / 1073741824;
$FreeDiskSizeGB = "{0:N0}" -f $FreeDiskSizeGB;
$DiskSize = (Get-CimInstance Win32_LogicalDisk).Size;
$DiskSizeGB = $DiskSize / 1073741824;
$DiskSizeGB = "{0:N0}" -f $DiskSizeGB;
if ($DiskSize -gt 0)
{
$FreeDiskPercent = ($FreeDiskSizeGB / $DiskSizeGB) * 100;
$FreeDiskPercent = "{0:N0}" -f $FreeDiskPercent;
$UsedDiskSizeGB = $DiskSizeGB - $FreeDiskSizeGB;
$UsedDiskPercent = ($UsedDiskSizeGB / $DiskSizeGB) * 100;
$UsedDiskPercent = "{0:N0}" -f $UsedDiskPercent;
$FormattedDisk = "Disk " + $DiskID.ToString() + " " +
$UsedDiskSizeGB.ToString() + "GB" + " / " + $DiskSizeGB.ToString() + "GB " +
"(" + $UsedDiskPercent.ToString() + "%" + ")";
$FormattedDisks.Add($FormattedDisk);
}
else
{
$FormattedDisk = "Disk " + $DiskID.ToString() + " Empty";
$FormattedDisks.Add($FormattedDisk);
}
}
return $FormattedDisks;
}
4.3 启动时加载插件
为了启动时加载插件,我们需要创建一个 Microsoft.PowerShell_profile.ps1
文件,放到 C:\Users\自己的用户名\Documents\PowerShell
目录下,文件内容为:
Import-Module windows-screenfetch
Import-Module posh-git
Import-Module posh-sshell
Import-Module oh-my-posh
Import-Module git-aliases -DisableNameChecking
Import-Module PSReadLine
Import-Module InstallModuleFromGitHub
Import-Module Get-ChildItemColor
Screenfetch
Set-PoshPrompt -Theme Material # 设置主题为 Material
Set-PSReadlineKeyHandler -Key Tab -Function Complete # 设置 Tab 键补全
Set-PSReadLineKeyHandler -Key "Ctrl+d" -Function MenuComplete # 设置 Ctrl+d 为菜单补全和 Intellisense
Set-PSReadLineKeyHandler -Key "Ctrl+z" -Function Undo # 设置 Ctrl+z 为撤销
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward # 设置向上键为后向搜索历史记录
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward # 设置向下键为前向搜索历史纪录
5 总结
Windows Terminal的安装与美化到这里就已经结束了,其中 pwsh 安装的一些插件,如果大家想了解其具体用法,大家可以去官网查看。