Files
dotfiles/chezmoi/dot_config/powershell/profile.ps1
T
Dmitry Dementiev 9b66fc70fc feat(windows): добавлены proxy-хелперы и упрощен git baseline
- Зачем:
  - нужно перенести привычные proxy_on/proxy_off/myip из bash в pwsh и убрать устаревший codevpn.
  - нужно сделать Windows Git baseline проще и предсказуемее, чтобы effective значения и проверки не путались.
- Что:
  - в managed profile PowerShell 7 добавлены `proxy_on`, `proxy_off`, `myip` и удалён `codevpn`.
  - Git baseline перенесён в `~/.gitconfig`, а `~/.gitconfig.local` оставлен только для локальных и персональных override-настроек.
  - README уточнён: legacy-статус Windows PowerShell 5.1, новые helper-функции и корректная команда проверки Git-конфига.
- Проверка:
  - `chezmoi -S "$HOME\dotfiles\chezmoi" apply --force`
  - `pwsh -NoLogo -Command ''Get-Command proxy_on,proxy_off,myip,Reload-Profile,wt-here; Test-Path function:codevpn''`
  - `git config --global --includes --list --show-origin`
2026-03-22 15:58:10 +03:00

185 lines
5.0 KiB
PowerShell

# Keep PowerShell 7 console text I/O on UTF-8 for predictable native tool behavior.
if ($IsWindows -and $PSVersionTable.PSEdition -eq 'Core' -and $Host.Name -eq 'ConsoleHost') {
chcp 65001 > $null
$Utf8NoBom = New-Object System.Text.UTF8Encoding($false)
[Console]::InputEncoding = $Utf8NoBom
[Console]::OutputEncoding = $Utf8NoBom
$OutputEncoding = $Utf8NoBom
}
# Keep interactive editing comfortable without depending on extra prompt frameworks.
if (
$Host.Name -eq 'ConsoleHost' -and
-not [Console]::IsOutputRedirected -and
$Host.UI.SupportsVirtualTerminal -and
(Get-Module -ListAvailable -Name PSReadLine)
) {
Import-Module PSReadLine -ErrorAction SilentlyContinue
Set-PSReadLineOption -PredictionSource History -ErrorAction SilentlyContinue
Set-PSReadLineOption -PredictionViewStyle ListView -ErrorAction SilentlyContinue
Set-PSReadLineOption -HistoryNoDuplicates:$true -ErrorAction SilentlyContinue
}
function Reload-Profile {
. $PROFILE
}
function which {
param(
[Parameter(Mandatory = $true)]
[string]$Name
)
Get-Command -Name $Name -All | Select-Object CommandType, Name, Source
}
function touch {
param(
[Parameter(Mandatory = $true, ValueFromRemainingArguments = $true)]
[string[]]$Path
)
foreach ($Item in $Path) {
if (Test-Path -LiteralPath $Item) {
(Get-Item -LiteralPath $Item).LastWriteTime = Get-Date
}
else {
New-Item -ItemType File -Path $Item -Force | Out-Null
}
}
}
function mkcd {
param(
[Parameter(Mandatory = $true)]
[string]$Path
)
New-Item -ItemType Directory -Path $Path -Force | Out-Null
Set-Location -Path $Path
}
function open {
param(
[string]$Path = '.'
)
Invoke-Item -Path $Path
}
function croot {
if (-not (Get-Command git -ErrorAction SilentlyContinue)) {
Write-Error 'git is not available in PATH.'
return
}
$Root = git rev-parse --show-toplevel 2>$null
if ($LASTEXITCODE -ne 0 -or [string]::IsNullOrWhiteSpace($Root)) {
Write-Error 'Not inside a Git repository.'
return
}
Set-Location -Path $Root.Trim()
}
function Get-PublicIp {
$CurlExe = Get-Command curl.exe -ErrorAction SilentlyContinue
if ($null -ne $CurlExe) {
try {
$CurrentIp = & $CurlExe.Source --silent --show-error --max-time 3 https://ifconfig.me/ip
return ([string]$CurrentIp).Trim()
}
catch {
return $null
}
}
try {
return ([string](Invoke-RestMethod -Uri 'https://ifconfig.me/ip' -TimeoutSec 3)).Trim()
}
catch {
return $null
}
}
function proxy_on {
$ProxyHost = 'hproxy.local'
$ProxyPort = 12334
$env:HTTP_PROXY = 'http://{0}:{1}' -f $ProxyHost, $ProxyPort
$env:HTTPS_PROXY = 'http://{0}:{1}' -f $ProxyHost, $ProxyPort
$env:ALL_PROXY = 'socks5://{0}:{1}' -f $ProxyHost, $ProxyPort
$env:NO_PROXY = '{0},localhost,127.0.0.1,.concept-software.ru' -f $ProxyHost
'Proxy enabled. Checking route...'
$CurrentIp = Get-PublicIp
if ([string]::IsNullOrWhiteSpace($CurrentIp)) {
'Current IP: unavailable'
}
else {
'Current IP: {0}' -f $CurrentIp
}
}
function proxy_off {
foreach ($VariableName in 'HTTP_PROXY', 'HTTPS_PROXY', 'ALL_PROXY', 'NO_PROXY') {
Remove-Item -Path ('Env:{0}' -f $VariableName) -ErrorAction SilentlyContinue
}
'Proxy disabled. Checking route...'
$CurrentIp = Get-PublicIp
if ([string]::IsNullOrWhiteSpace($CurrentIp)) {
'Current IP: unavailable'
}
else {
'Current IP: {0}' -f $CurrentIp
}
}
function myip {
$CurrentIp = Get-PublicIp
if ([string]::IsNullOrWhiteSpace($CurrentIp)) {
'IP: unavailable'
}
else {
'IP: {0}' -f $CurrentIp
}
}
if (Get-Command wt -ErrorAction SilentlyContinue) {
function wt-here {
wt -d (Get-Location).ProviderPath
}
function wt-tab-here {
wt -w 0 new-tab -d (Get-Location).ProviderPath
}
function wt-split-here {
wt -w 0 split-pane -d (Get-Location).ProviderPath
}
}
# Keep stable hooks for machine-local overrides and future managed fragments.
$ExtraProfile = Join-Path $HOME '.config\powershell\profile.extra.ps1'
if (Test-Path -LiteralPath $ExtraProfile) {
. $ExtraProfile
}
$ExtraProfileDir = Join-Path $HOME '.config\powershell\profile.d'
if (Test-Path -LiteralPath $ExtraProfileDir) {
foreach ($ProfileFragment in (Get-ChildItem -Path $ExtraProfileDir -Filter '*.ps1' -File | Sort-Object Name)) {
. $ProfileFragment.FullName
}
}
# Import the Chocolatey Profile that contains the necessary code to enable
# tab-completions to function for `choco`.
# Be aware that if you are missing these lines from your profile, tab completion
# for `choco` will not function.
# See https://ch0.co/tab-completion for details.
$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
if (Test-Path -LiteralPath $ChocolateyProfile) {
Import-Module $ChocolateyProfile
}