|
| 1 | +# redterm.ps1 — Windows self‑logging PowerShell shell |
| 2 | +# ───────────────────────────────────────────────────────── |
| 3 | +# Version 1.3‑hotfix‑2 2025‑07‑04 |
| 4 | +# |
| 5 | +# GOALS / ROAD‑MAP |
| 6 | +# 1. Spawn **one** child console that records a rolling transcript. |
| 7 | +# 2. Transcript trimmed to user‑chosen size (MB). |
| 8 | +# 3. Optional clipboard “tail” (last 120 log lines) for instant paste. |
| 9 | +# 4. One‑shot environment snapshot written beside the log. |
| 10 | +# 5. Mild visual cue (red prompt + red title) — no intrusive BG colours. |
| 11 | +# 6. CLI params: |
| 12 | +# ‑LogSizeMB <int> rolling log size (default 20) |
| 13 | +# ‑LogPath <dir> directory for logs (default ~/.redterm) |
| 14 | +# ‑Clipboard [bool] enable/disable clipboard tail (default On) |
| 15 | +# 7. Future: settings export / update‑check / monetisation hooks 😉 |
| 16 | +# ----------------------------------------------------------- |
| 17 | +param( |
| 18 | + [int] $LogSizeMB = 20, |
| 19 | + [string]$LogPath = (Join-Path $HOME '.redterm'), |
| 20 | + [switch]$Clipboard = $true, |
| 21 | + [switch]$NoSpawn |
| 22 | +) |
| 23 | + |
| 24 | +$childFlag = 'REDTERM_CHILD' # env‑flag to avoid infinite spawn |
| 25 | +$scriptSelf = $MyInvocation.MyCommand.Path |
| 26 | + |
| 27 | +# ── Phase 1 : parent spawns ONE child & exits ────────────────────────── |
| 28 | +if (-not $env:REDTERM_CHILD -and -not $NoSpawn) { |
| 29 | + # rebuild the same CLI params for the child |
| 30 | + $paramStr = @() |
| 31 | + if ($PSBoundParameters.ContainsKey('LogSizeMB')) { $paramStr += "-LogSizeMB $LogSizeMB" } |
| 32 | + if ($PSBoundParameters.ContainsKey('LogPath')) { $paramStr += "-LogPath `"$LogPath`"" } |
| 33 | + if (-not $Clipboard) { $paramStr += '-Clipboard:$false' } |
| 34 | + |
| 35 | + $cmd = "$env:REDTERM_CHILD=1; & '$scriptSelf' $($paramStr -join ' ')" |
| 36 | + |
| 37 | + Start-Process -FilePath powershell -ArgumentList @( |
| 38 | + '-NoLogo','-NoExit','-NoProfile','-ExecutionPolicy','Bypass', |
| 39 | + '-Command', $cmd |
| 40 | + ) -WindowStyle Normal |
| 41 | + return |
| 42 | +} |
| 43 | + |
| 44 | +# ── Phase 2 : we *are* the child – set up logging / visuals ──────────── |
| 45 | +$null = New-Item -ItemType Directory -Path $LogPath -Force |
| 46 | +$logFile = Join-Path $LogPath 'session.log' |
| 47 | +$envFile = Join-Path $LogPath 'environment.txt' |
| 48 | + |
| 49 | +# Trim existing transcript if oversized |
| 50 | +if (Test-Path $logFile -and (Get-Item $logFile).Length -gt ($LogSizeMB * 1MB)) { |
| 51 | + Get-Content $logFile -Tail 5000 | Set-Content $logFile |
| 52 | +} |
| 53 | + |
| 54 | +# One‑time environment snapshot |
| 55 | +if (-not (Test-Path $envFile)) { |
| 56 | + try { systeminfo 2>$null | Out-File -Encoding UTF8 -Width 200 $envFile } |
| 57 | + catch { Get-ComputerInfo | Out-File -Encoding UTF8 -Width 200 $envFile } |
| 58 | +} |
| 59 | + |
| 60 | +# Start / append transcript |
| 61 | +Stop-Transcript 2>$null |
| 62 | +Start-Transcript -Path $logFile -Append | Out-Null |
| 63 | + |
| 64 | +# ── Prompt + clipboard tail ─────────────────────────────────────────── |
| 65 | +function prompt { |
| 66 | + $esc = [char]27 |
| 67 | + $loc = Get-Location |
| 68 | + Write-Host "${esc}[91m$([Environment]::UserName) PS $loc>${esc}[0m " -NoNewline |
| 69 | + |
| 70 | + if ($Clipboard) { |
| 71 | + try { Get-Content $logFile -Tail 120 | Set-Clipboard } |
| 72 | + catch { Get-Content $logFile -Tail 120 | clip 2>$null } |
| 73 | + } |
| 74 | + return '' |
| 75 | +} |
| 76 | + |
| 77 | +# ── Mild visual cue ─────────────────────────────────────────────────── |
| 78 | +$host.UI.RawUI.WindowTitle = "🔴 redterm (log ≤ $LogSizeMB MB)" |
| 79 | +Clear-Host |
| 80 | +Write-Host "`nredterm started — transcript: $logFile`n" |
0 commit comments