{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ITerm2 schemes for the Terminal\n",
    "\n",
    "This is a little helper script to gather up all the color schemes in [mbadolato/iTerm2-Color-Schemes](https://github.com/mbadolato/iTerm2-Color-Schemes), and put them in a single fragment extension for the Terminal. \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "pwsh"
    },
    "polyglot_notebook": {
     "kernelName": "pwsh"
    }
   },
   "outputs": [],
   "source": [
    "# Clone the repo into the temp directory\n",
    "\n",
    "cd \"$env:TEMP\"\n",
    "if (Test-Path -Path \"$env:TEMP\\iTerm2-Color-Schemes\") {\n",
    "    Remove-Item -Recurse -Force \"$env:TEMP\\iTerm2-Color-Schemes\"\n",
    "}\n",
    "git clone --depth 1 https://github.com/mbadolato/iTerm2-Color-Schemes.git\n",
    "\n",
    "cd \"$env:TEMP\\iTerm2-Color-Schemes\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "dotnet_interactive": {
     "language": "pwsh"
    },
    "polyglot_notebook": {
     "kernelName": "pwsh"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Importing schemes from 'C:\\Users\\migrie\\AppData\\Local\\Temp\\iTerm2-Color-Schemes'\n",
      "Found 319 schemes\n",
      "\u001b[K  Importing C:\\Users\\migrie\\AppData\\Local\\Temp\\iTerm2-Color-Schemes\\windowsterminal\\zenwritten_light.json                         Imported json for 319 schemes\n",
      "\n",
      "    Directory: C:\\Users\\migrie\\AppData\\Local\\Microsoft\\Windows Terminal\\Fragments\n",
      "\n",
      "\u001b[32;1mMode   \u001b[0m\u001b[32;1m              LastWriteTime\u001b[0m \u001b[32;1;3m        Length\u001b[0m\u001b[32;1m Name\u001b[0m\n",
      "\u001b[32;1m----   \u001b[0m \u001b[32;1m             -------------\u001b[0m \u001b[32;1m        ------\u001b[0m \u001b[32;1m----\u001b[0m\n",
      "d----           3/28/2024  6:30 AM                \u001b[44;1mAllColorSchemes\u001b[0m\n",
      "Fragment json written to C:\\Users\\migrie\\AppData\\Local\\Microsoft\\Windows Terminal\\Fragments\\AllColorSchemes\\schemes.json\n",
      "\n"
     ]
    }
   ],
   "source": [
    "cd \"$env:TEMP\\iTerm2-Color-Schemes\"\n",
    "\n",
    "Write-Host \"Importing schemes from '$env:TEMP\\iTerm2-Color-Schemes'\"\n",
    "\n",
    "# Iterate over all the files in the `windowsterminal` directory\n",
    "\n",
    "$allSchemesFiles = Get-ChildItem -Path \"$env:TEMP\\iTerm2-Color-Schemes\\windowsterminal\" -Filter *.json\n",
    "Write-host \"Found $($allSchemesFiles.Count) schemes\"\n",
    "\n",
    "$allSchemeJsons = @()\n",
    "\n",
    "$allSchemesFiles | ForEach-Object {\n",
    "\n",
    "    Write-Host \"`r`e[K  Importing $_           \" -NoNewline\n",
    "    $json = Get-Content $_.FullName -Raw | ConvertFrom-Json\n",
    "    $allSchemeJsons += $json\n",
    "\n",
    "}\n",
    "Write-Host \"\"\n",
    "Write-Host \"Imported json for $($allSchemeJsons.Count) schemes\"\n",
    "\n",
    "# Create a new fragment json in the temp directory with all the schemes added to a \"schemes\" array\n",
    "\n",
    "$fragmentJson = @{\n",
    "    \"schemes\" = $allSchemeJsons\n",
    "} | ConvertTo-Json\n",
    "\n",
    "# Remove the existing fragment json if it exists\n",
    "$fragmentDir = $env:LOCALAPPDATA + \"\\Microsoft\\Windows Terminal\\Fragments\\AllColorSchemes\"\n",
    "$fragmentPath = $fragmentDir + \"\\schemes.json\"\n",
    "if (Test-Path -Path $fragmentPath) {\n",
    "    Remove-Item -Path $fragmentPath\n",
    "}\n",
    "# make sure the directory exists\n",
    "New-Item -Path $fragmentDir -ItemType Directory -Force\n",
    "\n",
    "# Write the fragment json to the fragments directory\n",
    "Write-Output $fragmentJson | Out-File $fragmentPath -Encoding Utf8\n",
    "\n",
    "write-host \"Fragment json written to $fragmentPath\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "After running this notebook, all the color schemes will be available in the Terminal. You'll probably need to go touch the settings to get them reloaded. "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".NET (C#)",
   "language": "C#",
   "name": ".net-csharp"
  },
  "language_info": {
   "name": "polyglot-notebook"
  },
  "polyglot_notebook": {
   "kernelInfo": {
    "defaultKernelName": "csharp",
    "items": [
     {
      "aliases": [],
      "name": "csharp"
     },
     {
      "aliases": [],
      "languageName": "pwsh",
      "name": "pwsh"
     }
    ]
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
