# usage:    uv run win-app-size.py
# requires: Windows as platform, matplotlib 
import subprocess
import json
import matplotlib.pyplot as plt

# call PowerShell to get app sizes
ps_cmd = r"""
  $key = '\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
  Get-ItemProperty "HKLM:$key", "HKCU:$key" |
  Where-Object { $_.DisplayName -and $_.EstimatedSize } |
  Sort-Object EstimatedSize -Descending |
  Select-Object -First 10 DisplayName,
      @{ N='SizeMB'
         E={[math]::Round($_.EstimatedSize/1024)} } |
  ConvertTo-Json
  """
ps = subprocess.run(
  ['powershell', '-Command', ps_cmd],
  capture_output=True, text=True)

# retrieve data from JSON to Python lists
apps  = json.loads(ps.stdout)
names = [a['DisplayName'] for a in apps]
sizes = [a['SizeMB']      for a in apps]

# visualize with Matplotlib
plt.figure(figsize=(10, 6))
plt.barh(names[::-1], sizes[::-1])
plt.xlabel('Size (MB)')
plt.title('Top 10 Installed Software by Size')
plt.tight_layout()
plt.savefig('InstalledApps.png', dpi=150)
