Advertisement
uspiritwolf

FastInstall.ps1

Feb 12th, 2022 (edited)
1,621
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $ESC = [char]0x1b
  2. $ALTC = "$ESC[?1049h"
  3. $MAINC = "$ESC[?1049l"
  4.  
  5. $global:CACHELIST = $null
  6.  
  7. [Int] $MainPad = 75
  8. [Int] $NamePad = 40
  9. [Int] $StatePad = $MainPad - $NamePad
  10.  
  11. $ListApps =
  12. (
  13.     "Google.Chrome",
  14.     "Mozilla.Firefox",
  15.     "Spotify.Spotify",
  16.     "Discord.Discord",
  17.     "BraveSoftware.BraveBrowser",
  18.     "Valve.Steam",
  19.     "VideoLAN.VLC",
  20.     "EpicGames.EpicGamesLauncher",
  21.     "Telegram.TelegramDesktop",
  22.     "BlenderFoundation.Blender",
  23.     "Microsoft.VisualStudio.2022.Community",
  24.     "UnityTechnologies.UnityHub"
  25. )
  26.  
  27. function GetAppList
  28. {
  29.     if($null -eq $global:CACHELIST)
  30.     {
  31.         $global:CACHELIST = winget list
  32.     }
  33.     return $global:CACHELIST
  34. }
  35.  
  36. function IsAppInstalled([String] $Id)
  37. {
  38.     $list = GetAppList
  39.     $selected = $list | Select-String -Pattern "$Id " -CaseSensitive -SimpleMatch
  40.     return $selected.Length -gt 0
  41. }
  42.  
  43. function GetStringStatus
  44. {
  45.     param($IsAppInstalled, $IsAppSelected)
  46.  
  47.     if($IsAppSelected)
  48.     {
  49.         if($IsAppInstalled)
  50.         {
  51.             return "$ESC[31mTo Uninstall".PadRight($StatePad + 4)
  52.         }
  53.         else
  54.         {
  55.             return "$ESC[38;2;155;135;12mTo Install".PadRight($StatePad + 17)
  56.         }
  57.     }
  58.     else
  59.     {
  60.         if($IsAppInstalled)
  61.         {
  62.             return "$ESC[32mInstalled".PadRight($StatePad + 4)
  63.         }
  64.         else
  65.         {
  66.             return "$ESC[38;2;100;100;100mNot Installed".PadRight($StatePad + 18)
  67.         }
  68.     }
  69. }
  70.  
  71. function MakeAppsInfo
  72. {
  73.     $result = @()
  74.     for ($IndexApp=0; $IndexApp -lt $ListApps.Length; $IndexApp+=1 )
  75.     {
  76.         $app = [string]$ListApps[$IndexApp]
  77.         $IsInstalled = IsAppInstalled($app)
  78.  
  79.         $result += [PSCustomObject]@{
  80.             Index = $IndexApp
  81.             Name = $app
  82.             IsInstalled=$IsInstalled
  83.             IsSelected=$false
  84.         }
  85.     }
  86.     return , $result
  87. }
  88.  
  89. function PrintApps
  90. {
  91.     Param($Appsinfo, $CurIndex)
  92.  
  93.     foreach ($info in $Appsinfo)
  94.     {
  95.         if($info.Index -Eq $CurIndex)
  96.         {
  97.             $host.ui.Write("$ESC[47;30m")
  98.         }
  99.         else
  100.         {
  101.             $host.ui.Write("$ESC[0m")
  102.         }
  103.  
  104.         $name = $info.Name.PadRight($NamePad)
  105.         $StringStatus = GetStringStatus -IsAppInstalled $info.IsInstalled -IsAppSelected $info.IsSelected
  106.  
  107.         Write-Host "$name $StringStatus"
  108.     }
  109.     Write-Host "$ESC[0m"
  110. }
  111.  
  112. function PrintInfoNavigation {
  113.     $info = "$ESC[39m$ESC[48;5;8m Up/Down Arrows - $ESC[36mMove$ESC[39m; Enter -  $ESC[36mApply$ESC[39m; Spacebar - $ESC[36mSelect$ESC[39m; ESC - $ESC[36mExit"
  114.     Write-Host $info.PadRight($MainPad + 49)
  115.     $host.ui.Write("$ESC[0m")
  116. }
  117.  
  118. function ApplyChanges
  119. {
  120.     param ($Appsinfo)
  121.     foreach ($info in $Appsinfo)
  122.     {
  123.         if($info.IsSelected)
  124.         {
  125.             if($info.IsInstalled)
  126.             {
  127.                 winget uninstall --id $info.Name
  128.             }
  129.             else
  130.             {
  131.                 winget install --id $info.Name
  132.             }
  133.         }
  134.     }
  135. }
  136.  
  137. function main()
  138. {
  139.     Write-Host("Gathering information...")
  140.  
  141.     $Appsinfo = MakeAppsInfo
  142.     $CurIndex = 0
  143.  
  144.     [bool] $isFinished = $false
  145.     [bool] $GoApply = $false
  146.     while($isFinished -eq $false)
  147.     {
  148.         $CurIndex = [Math]::Max(0, [Math]::Min($Appsinfo.Length - 1, $CurIndex))
  149.  
  150.         $Host.UI.RawUI.CursorPosition = @{ X = 0; Y = 0 }
  151.         PrintApps -Appsinfo $Appsinfo -CurIndex $CurIndex
  152.         PrintInfoNavigation
  153.         $key = [System.Console]::ReadKey($true).Key
  154.         switch ($key)
  155.         {
  156.             "UpArrow"   { $CurIndex -= 1 }
  157.             "DownArrow" { $CurIndex += 1 }
  158.             "Enter"     { $isFinished = $true; $GoApply = $true }
  159.             "Spacebar"  { $Appsinfo[$CurIndex].IsSelected = !$Appsinfo[$CurIndex].IsSelected }
  160.             "Escape"    { $isFinished = $true; }
  161.             Default {}
  162.         }
  163.     }
  164.  
  165.     $Host.UI.RawUI.CursorPosition = @{ X = 0; Y = 0 }
  166.     Clear-Host
  167.  
  168.     if($GoApply)
  169.     {
  170.         ApplyChanges -Appsinfo $Appsinfo
  171.  
  172.         Write-Output "Press any key..."
  173.         [System.Console]::ReadKey($true) | Out-Null
  174.     }
  175. }
  176.  
  177.  
  178.  
  179. $host.ui.Write($ALTC)
  180. $Host.UI.RawUI.CursorPosition = @{ X = 0; Y = 0 }
  181. Clear-Host
  182.  
  183. main;
  184.  
  185. $host.ui.Write($MAINC)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement