Skip to content

Instantly share code, notes, and snippets.

@hetima
Last active May 31, 2026 23:58
Show Gist options
  • Select an option

  • Save hetima/aa08d968af81f1d0ed025fa5eafc53a7 to your computer and use it in GitHub Desktop.

Select an option

Save hetima/aa08d968af81f1d0ed025fa5eafc53a7 to your computer and use it in GitHub Desktop.
$PSDefaultParameterValues['Out-File:Encoding'] = 'utf8'
$PSDefaultParameterValues['*:Encoding'] = 'utf8'
$global:OutputEncoding = [System.Text.Encoding]::UTF8
[console]::OutputEncoding = [System.Text.Encoding]::UTF8
# 環境判定
if ($env:CODEX_SHELL) {
return
}
$isWindowsTerminal = [bool]$env:WT_SESSION
$isVSCode = $env:TERM_PROGRAM -eq 'vscode'
# 履歴ファイル設定
$historyFile = if ($isWindowsTerminal) {
"WindowsTerminal_history.txt"
} elseif ($isVSCode) {
"VSCode_history.txt"
} else {
"ConsoleHost_history.txt"
}
$historyPath = Join-Path $env:APPDATA "Microsoft\Windows\PowerShell\PSReadLine\$historyFile"
Set-PSReadLineOption -HistorySavePath $historyPath
Set-PSReadLineOption -PredictionSource History
# 追加のprofile
if ($isWindowsTerminal) {
. "~\source\pwsh\wt_profile.ps1"
}elseif ($isVSCode){
. "~\source\pwsh\vscode_profile.ps1"
}
# ワークスペースごとにコマンド履歴を分ける
# 履歴ファイルを保存するベースディレクトリ
$HistoryBaseDir = Join-Path $env:APPDATA "Microsoft\Windows\PowerShell\PSReadLine\vscodeTerminal"
$global:LastHistoryPath = ""
function Set-DirectoryHistory {
# 1. パスが変わっていない場合は何もせずリターン
if ($PWD.Path -eq $global:LastHistoryPath) { return }
if (-not (Test-Path -Path $HistoryBaseDir)) {
New-Item -ItemType Directory -Path $HistoryBaseDir | Out-Null
}
# 2. パスが変わったときだけハッシュ計算と設定更新を行う
$md5 = [System.Security.Cryptography.MD5]::Create()
$bytes = [System.Text.Encoding]::UTF8.GetBytes($PWD.Path)
$hash = ($md5.ComputeHash($bytes) | ForEach-Object { $_.ToString("x2") }) -join ""
$shortHash = $hash.Substring(0, 8)
$folderName = Split-Path $PWD.Path -Leaf
if ($folderName -eq "") { $folderName = "root" }
$fileName = "${shortHash}_${folderName}_history.txt"
$historyPath = Join-Path $HistoryBaseDir $fileName
# 設定を更新して通知
Set-PSReadLineOption -HistorySavePath $historyPath
Write-Host " [History switched to: $fileName]" -ForegroundColor DarkGreen
# 最後に現在のパスを記憶
$global:LastHistoryPath = $PWD.Path
}
# プロンプト表示のタイミングで履歴パスを更新する場合
# $PROMPT_COMMAND = { Set-DirectoryHistory }
# PowerShell起動時に実行
Set-DirectoryHistory
# --------------------------------------------------------------------
# .venvがあればアクティベート
function Invoke-VenvActivate {
$current = Get-Location
$locations = @($current, (Split-Path $current -Parent))
foreach ($path in $locations) {
$activateScript = Join-Path $path ".venv\Scripts\Activate.ps1"
if (Test-Path $activateScript) {
Write-Host "Activating venv: $activateScript" -ForegroundColor Green
. $activateScript
return
}
}
}
Invoke-VenvActivate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment