Skip to content

Commit befb296

Browse files
committed
feat(profile): 添加自定义别名配置并优化别名设置逻辑
- 将用户别名配置提取为全局变量$userAlias - 支持通过command字段创建函数别名 - 在Show-MyProfileHelp中显示函数别名信息 - 调整Set-AliasProfile调用位置至Initialize-Environment末尾
1 parent 0eba197 commit befb296

1 file changed

Lines changed: 57 additions & 45 deletions

File tree

profile/profile.ps1

Lines changed: 57 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,51 @@ if ($loadProfile) {
2323
Set-Content -Path $profile -Value ". $PSCommandPath"
2424
return
2525
}
26+
# 自定义别名配置
27+
$userAlias = @(
28+
[PSCustomObject]@{
29+
cliName = 'dust'
30+
aliasName = 'du'
31+
aliasValue = 'dust'
32+
description = 'dust 是一个用于清理磁盘空间的命令行工具。它可以扫描指定目录并显示占用空间较大的文件和目录,以便用户确定是否删除它们。'
33+
}
34+
[PSCustomObject]@{
35+
cliName = 'duf'
36+
aliasName = 'df'
37+
aliasValue = 'duf'
38+
description = 'df 是 du 的别名,用于显示目录内容。'
39+
}
40+
[PSCustomObject]@{
41+
cliName = 'zoxide'
42+
aliasName = 'zq'
43+
aliasValue = ''
44+
description = 'zoxide query 用于查询zoxide的数据库,显示最近访问的目录。'
45+
command = 'zoxide query'
2646

47+
}
48+
[PSCustomObject]@{
49+
cliName = 'zoxide'
50+
aliasName = 'za'
51+
aliasValue = ''
52+
description = 'zoxide add 用于将当前目录添加到zoxide的数据库中,以便下次快速访问。'
53+
command = 'zoxide add'
54+
}
55+
[PSCustomObject]@{
56+
cliName = 'zoxide'
57+
aliasName = 'zr'
58+
aliasValue = 'zoxide'
59+
description = '如果你不希望某个目录再出现在 zoxide 的候选项中'
60+
command = 'zoxide remove'
61+
62+
}
63+
# scoop下载下来就是btm,不用设置别名
64+
# [PSCustomObject]@{
65+
# cliName = 'bottom'
66+
# aliasName = 'btm'
67+
# aliasValue = 'bottom'
68+
# description = 'bottom 是一个用于显示系统资源使用情况的命令行工具。它可以实时显示CPU、内存、磁盘和网络等系统资源的使用情况,帮助用户监控系统性能。'
69+
# }
70+
)
2771

2872
<#
2973
.SYNOPSIS
@@ -71,6 +115,10 @@ function Show-MyProfileHelp {
71115
Write-Host "`n[自定义别名]" -ForegroundColor Yellow
72116
Get-CustomAlias -AliasDespPrefix $AliasDespPrefix | Format-Table -AutoSize
73117

118+
# 1.5 函数别名
119+
Write-Host "`n[自定义函数别名]" -ForegroundColor Yellow
120+
$userAlias | Where-Object { $_.PSObject.Properties.Name -contains 'command' } | Select-Object @{N = '函数名'; E = 'aliasName' }, @{N = '底层命令'; E = 'command' }, @{N = '描述'; E = 'description' } | Format-Table -AutoSize
121+
74122
# 2. 显示此 Profile 文件中定义的函数
75123
Write-Host "`n[自定义函数]" -ForegroundColor Yellow
76124
# 假设你的自定义函数都在一个模块里,或者你可以用其他方式过滤
@@ -99,58 +147,24 @@ function Show-MyProfileHelp {
99147
function Set-AliasProfile {
100148
[CmdletBinding()]
101149
param (
102-
150+
[PSCustomObject]$userAlias = $userAlias
103151
)
104-
105152
begin {
106-
107153
}
108154

109155
process {
110156
# 设置PowerShell别名
111157
Write-Verbose "设置PowerShell别名"
112158
Set-CustomAlias -Name ise -Value powershell_ise -AliasDespPrefix $AliasDespPrefix -Scope Global
113159
Set-CustomAlias -Name ipython -Value Start-Ipython -AliasDespPrefix $AliasDespPrefix -Scope Global
114-
$userAlias = @(
115-
[PSCustomObject]@{
116-
cliName = 'dust'
117-
aliasName = 'du'
118-
aliasValue = 'dust'
119-
description = 'dust 是一个用于清理磁盘空间的命令行工具。它可以扫描指定目录并显示占用空间较大的文件和目录,以便用户确定是否删除它们。'
120-
}
121-
[PSCustomObject]@{
122-
cliName = 'duf'
123-
aliasName = 'df'
124-
aliasValue = 'duf'
125-
description = 'df 是 du 的别名,用于显示目录内容。'
126-
}
127-
[PSCustomObject]@{
128-
cliName = 'zoxide'
129-
aliasName = 'zq'
130-
aliasValue = 'zoxide query'
131-
description = 'zoxide query 用于查询zoxide的数据库,显示最近访问的目录。'
132-
}
133-
[PSCustomObject]@{
134-
cliName = 'zoxide'
135-
aliasName = 'za'
136-
aliasValue = 'zoxide add'
137-
description = 'zoxide add 用于将当前目录添加到zoxide的数据库中,以便下次快速访问。'
138-
}
139-
[PSCustomObject]@{
140-
cliName = 'zoxide'
141-
aliasName = 'zr'
142-
aliasValue = 'zoxide remove'
143-
description = '如果你不希望某个目录再出现在 zoxide 的候选项中'
144-
}
145-
# scoop下载下来就是btm,不用设置别名
146-
# [PSCustomObject]@{
147-
# cliName = 'bottom'
148-
# aliasName = 'btm'
149-
# aliasValue = 'bottom'
150-
# description = 'bottom 是一个用于显示系统资源使用情况的命令行工具。它可以实时显示CPU、内存、磁盘和网络等系统资源的使用情况,帮助用户监控系统性能。'
151-
# }
152-
)
153160
foreach ($alias in $userAlias) {
161+
if ($alias.command) {
162+
Write-Verbose "别名 $($alias.aliasName) 已设置函数,执行函数创建"
163+
$scriptBlock = [scriptblock]::Create("$($alias.command) `$args")
164+
New-Item -Path "Function:Global:$($alias.aliasName)" -Value $scriptBlock -Force | Out-Null
165+
Write-Verbose "已创建函数: $($alias.name)"
166+
continue
167+
}
154168
if (Test-ExeProgram -Name $alias.cliName) {
155169
Set-CustomAlias -Name $alias.aliasName -Value $alias.aliasValue -Description $alias.description -AliasDespPrefix $AliasDespPrefix -Scope Global
156170
Write-Verbose "已设置别名: $($alias.aliasName) -> $($alias.aliasValue)"
@@ -221,9 +235,6 @@ function Initialize-Environment {
221235
}
222236

223237

224-
225-
226-
Set-AliasProfile
227238
if (Test-ExeProgram -Name 'conda') {
228239
Add-CondaEnv
229240
}
@@ -289,6 +300,7 @@ function Initialize-Environment {
289300
}
290301
}
291302

303+
Set-AliasProfile
292304
# 载入conda环境(如果环境变量中没有conda命令)
293305
if (-not (Test-EXEProgram -Name conda)) {
294306
Write-Verbose "尝试加载Conda环境"

0 commit comments

Comments
 (0)