# 1. ログの保存先を設定 $LogPath = "$env:USERPROFILE\pslog.txt" $MaxLines = 1000 # 2. 既存のログがある場合、最新の1000行だけ残してファイルを上書き(トリミング) if (Test-Path $LogPath) { # ファイルを読み込み、後ろから1000行を取得して上書き $OldContent = Get-Content $LogPath -ErrorAction SilentlyContinue if ($OldContent.Count -gt $MaxLines) { $OldContent | Select-Object -Last $MaxLines | Out-File $LogPath -Encoding utf8 } } # 3. 記録を開始(-Appendで追記モード) # 既に開いているセッションがある場合のエラーを避けるため、SilentlyContinueを指定 # Start-Transcript -Path $LogPath -Append -Force -ErrorAction SilentlyContinue | Out-Null function openlog { nvim + "$env:USERPROFILE\pslog.txt" } function prompt { # 1. 直前のコマンドの実行時間を取得 $lastCommand = Get-History -Count 1 $elapsedStr = "" if ($lastCommand) { $duration = $lastCommand.EndExecutionTime - $lastCommand.StartExecutionTime if ($duration.TotalSeconds -ge 1) { $elapsedStr = "[{0:N2}s]" -f $duration.TotalSeconds } else { $elapsedStr = "[{0:N0}ms]" -f $duration.TotalMilliseconds } } # 2. Gitブランチの取得(追加部分) $branchStr = "<->" # gitコマンドが存在する場合のみ実行 if (Get-Command git -ErrorAction SilentlyContinue) { # 現在のブランチ名を取得。エラー出力(リポジトリ外など)は捨てる $gitBranch = git branch --show-current 2>$null # コマンドが成功し、かつブランチ名が取得できた場合 if ($LASTEXITCODE -eq 0 -and $gitBranch) { $branchStr = "<$gitBranch>" } } # 3. 色の設定 $dirColor = "Cyan" $branchColor = "Green" # ブランチ用の色を追加 $timeColor = "Yellow" $promptCharColor = "Magenta" # 4. 1行目:空行 + ディレクトリ階層 + ブランチ + 実行時間 Write-Host "" # 前のコマンドとの区切り Write-Host "$pwd " -NoNewline -ForegroundColor $dirColor Write-Host "$branchStr" -NoNewline -ForegroundColor $branchColor Write-Host " $elapsedStr" -ForegroundColor $timeColor # 5. 2行目:入力ライン(プロンプト記号) # Write-Host "PS" -NoNewline -ForegroundColor $promptCharColor Write-Host ">>" -NoNewline # 最後に「何も出力しない(空文字を返す)」ことで、 # PowerShell標準のパス表示が差し込まれるのを防ぎます。 return " " } Set-Alias vi nvim function poweroff { shutdown /s /t 0 } function reboot { shutdown /r /t 0 } function e { explorer . } function vimconfig { nvim $HOME/Appdata/Local/nvim/init.lua } function psconfig { nvim $PROFILE } function gettex { $texs = Get-ChildItem *.tex if ($texs.Count -eq 1) { return $texs[0] } else { for ($i = 0; $i -lt $texs.Count; $i++) { Write-Host $i -NoNewline Write-Host ": " -NoNewline Write-Host $texs[$i].Name } $num = Read-Host "いずれかの .texファイルを指定してください.(default: 0) " if ($num -eq "") { $num = 0 } return $texs[$num] } } function edittex { $tex = gettex nvim $tex.Name } Set-Alias et edittex function runtex { $tex = gettex $pdf = $tex.BaseName + ".pdf" latexmk -lualatex $tex.Name $args # $argsは自動変数 } Set-Alias rt runtex function touch { param ($file) if (Test-Path $file) { Set-ItemProperty -Path $file -Name LastWriteTime -Value (Get-Date) } else { New-Item -Path $file -ItemType File } } function fdls { # 1. 引数がない場合はカレントディレクトリ (".") をデフォルトにする param ( [string]$dir = "." ) if (Test-Path $dir) { # 2. 第1引数に「全件マッチ」を意味する "." を置き、第2引数に検索先パスを指定する fd -d 1 . $dir } else { Write-Warning "パスが存在しません: $dir" } } # 3. 既存の ls エイリアスを強制的に削除してから再登録する Remove-Item Alias:ls -Force -ErrorAction SilentlyContinue Set-Alias ls fdls function spdf { start $HOME\spdf122\sPDF.exe } function _class { start-process https://class.admin.tus.ac.jp/uprx/ShibbolethAuthServlet } function _letus { start-process https://letus.ed.tus.ac.jp/auth/shibboleth/index.php } # 開いているすべてのエクスプローラーウィンドウを安全に閉じる関数 function Close-AllExplorers { $app = New-Object -ComObject Shell.Application @($app.Windows() | Where-Object { $_.FullName -imatch 'explorer.exe' }) | ForEach-Object { $_.Quit() } } # 短く呼び出せるようにエイリアスを設定 Set-Alias ce Close-AllExplorers function wezconfig { nvim $HOME\.wezterm.lua }