Files
notes/resource/配置/PowerShell 配置文件.md
Docker7530 6b50219f55 1776654103
2026-04-20 11:01:47 +08:00

154 lines
5.1 KiB
Markdown

```PowerShell
#----------------------------------------------------------------------
# 控制台输出编码设置为 UTF-8
#----------------------------------------------------------------------
[Console]::InputEncoding = [System.Text.Encoding]::UTF8
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
#----------------------------------------------------------------------
# PSReadLine 配置
#----------------------------------------------------------------------
if ($Host.Name -eq 'ConsoleHost' -and $Host.UI.SupportsVirtualTerminal) {
try {
# --- 预测与补全 ---
Set-PSReadLineOption -PredictionSource HistoryAndPlugin
Set-PSReadLineOption -PredictionViewStyle ListView
Set-PSReadLineKeyHandler -Key Tab -Function MenuComplete
}
catch {
Write-Verbose "PSReadLine 配置未生效: $($_.Exception.Message)"
}
}
#----------------------------------------------------------------------
# 自定义快捷命令
#----------------------------------------------------------------------
function ep { code $PROFILE }
function vim { nvim $args }
#----------------------------------------------------------------------
# 查找并可选择性删除当前目录及子目录下的所有空文件夹。
# - 会自动忽略 .git 目录。
# - 解决了嵌套空目录的删除顺序问题。
# - 支持 -WhatIf 和 -Confirm 安全参数。
# - 遵循 PowerShell 最佳实践。
#----------------------------------------------------------------------
function Get-EmptyDirectory {
[CmdletBinding(SupportsShouldProcess = $true)]
param (
[Parameter(Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[string]$Path = ".",
[Switch]
[Alias('d')]
$Delete
)
# 要忽略的目录名称列表
$ignoreDirs = @(".git", ".idea", ".vscode", "node_modules", "dist")
# 1. 获取所有目录,递归排除 ignoreDirs
$emptyDirs = Get-ChildItem -Path $Path -Recurse -Directory -ErrorAction SilentlyContinue |
Where-Object {
# 当前目录是否属于忽略名单
$name = $_.Name.ToLower()
if ($ignoreDirs -contains $name) { return $false }
$lowerPath = $_.FullName.ToLower()
if ($lowerPath -match 'node_modules|dist') { return $false }
# 是否为空目录
-not $_.GetFileSystemInfos()
}
# 2. 按路径长度降序排列,保证先删除子目录
$sortedEmptyDirs = $emptyDirs | Sort-Object { $_.FullName.Length } -Descending
foreach ($dir in $sortedEmptyDirs) {
# 输出管道对象
Write-Output $dir
if ($Delete) {
if ($PSCmdlet.ShouldProcess($dir.FullName, "Delete Empty Directory")) {
Remove-Item -LiteralPath $dir.FullName -Force
Write-Host "Deleted empty directory: $($dir.FullName)" -ForegroundColor Green
}
}
}
}
#----------------------------------------------------------------------
# 函数: Get-GitCodeLines
#----------------------------------------------------------------------
#
# 用法 1(默认统计当前 Git 仓库,当前用户,最近 30 天):
# Get-GitCodeLines
#
# 用法 2(指定具体日期范围):
# Get-GitCodeLines -Author "zhangsan" -Since "2025-01-01" -Until "2025-06-30"
#
function Get-GitCodeLines {
param (
[string]$Author = "$(git config user.name)",
[string]$Since, # yyyy-MM-dd
[string]$Until # yyyy-MM-dd
)
# 检查是否在 Git 仓库中
if (-not (Test-Path ".git")) {
Write-Host "❌ 当前目录不是 Git 仓库。" -ForegroundColor Red
return
}
# 若未提供日期,则使用最近 30 天
if (-not $Since) {
$Since = (Get-Date).AddDays(-30).ToString("yyyy-MM-dd")
}
if (-not $Until) {
$Until = (Get-Date).ToString("yyyy-MM-dd")
}
Write-Host "📊 正在统计 $Author$Since$Until 的代码变动..." -ForegroundColor Cyan
$log = git log `
--since=$Since `
--until=$Until `
--author=$Author `
--pretty=tformat: `
--numstat
if ($LASTEXITCODE -ne 0) {
Write-Host "❌ git log 执行失败,请检查参数。" -ForegroundColor Red
return
}
if (-not $log) {
Write-Host "⚠️ 该时间段内无提交记录。" -ForegroundColor Yellow
return
}
$added = 0
$deleted = 0
foreach ($line in $log) {
if ($line -match "^\d+\s+\d+") {
$cols = $line -split "\s+", 3
$added += [int]$cols[0]
$deleted += [int]$cols[1]
}
}
$net = $added - $deleted
$total = $added + $deleted
Write-Host "✅ 作者: $Author" -ForegroundColor Green
Write-Host "📆 时间范围: $Since ~ $Until"
Write-Host "🟢 增加: $added 行"
Write-Host "🔴 删除: $deleted 行"
Write-Host "🔵 净增: $net 行" -ForegroundColor $(if ($net -gt 0) { "Green" } elseif ($net -lt 0) { "Red" } else { "Gray" })
Write-Host "📈 总变动: $total 行"
}
Set-Alias singbox "D:\MyCode\netbox\sing-box\singbox.ps1"
```