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