Skip to content

Instantly share code, notes, and snippets.

@eddiezato
Last active November 5, 2024 06:59
Show Gist options
  • Save eddiezato/014d3081a2141fe3c4693193db3ff8c1 to your computer and use it in GitHub Desktop.
Save eddiezato/014d3081a2141fe3c4693193db3ff8c1 to your computer and use it in GitHub Desktop.
PowerShell: script to update FAR Manager
function Select-Button {
param(
[string]$Prompt,
[string[]]$Buttons,
[uint]$Position
)
"$Prompt " | Write-Host -NoNewline -ForegroundColor Magenta
$cursor = $Host.UI.RawUI.CursorPosition
$Panel = {
$panel_ = ''
foreach ($i in 0..($Buttons.Count - 1)) { if ($i -eq $Position) { $panel_ += "`e[7m $($Buttons[$i]) `e[0m" } else { $panel_ += " $($Buttons[$i]) " }}
$panel_ | Write-Host -NoNewline
$Host.UI.RawUI.CursorPosition = $cursor
}
[Console]::CursorVisible = $false
& $Panel
$continue = $true
$Host.UI.RawUI.FlushInputBuffer();
do {
$key = $Host.UI.RawUI.ReadKey("NoEcho, IncludeKeyDown")
switch ($key.VirtualKeyCode) {
37 { if ($Position -ne 0) { $Position --; & $Panel }}
39 { if ($Position -ne ($Buttons.Count - 1)) { $Position ++; & $Panel }}
13 { $continue = $false }
}
} while ($continue)
' ' * (($Buttons | Measure-Object -Property Length -Sum).Sum + 2 * $Buttons.Count) | Write-Host -NoNewline
$Host.UI.RawUI.CursorPosition = $cursor
$Buttons[$Position] | Write-Host -ForegroundColor Cyan
[Console]::CursorVisible = $true
return $Position
}
$location = Get-Location
$path_to_FAR = "$Env:USERPROFILE\Portable\FarManager"
"script to update FAR Manager" | Write-Host -ForegroundColor Blue
$to_remove = @(
"Far.exe.example.ini", "File_id.diz",
"Addons", "Documentation",
"Plugins\FarColorer", "PluginSDK",
"VisualElements", "Far.VisualElementsManifest.xml",
"SaveOldPluginSettings.cmd", "RestoreOldPluginSettings.cmd",
"*bel.hlf", "*bel.lng", "*cze.hlf", "*cze.lng", "*ger.hlf", "*ger.lng", "*hun.hlf",
"*hun.lng", "*ita.hlf", "*ita.lng", "*lit.hlf", "*lit.lng", "*pol.hlf", "*pol.lng",
"*sky.hlf", "*sky.lng", "*spa.hlf", "*spa.lng", "*ukr.hlf", "*ukr.lng"
)
$local_version = (Get-Item -Path "$path_to_FAR\Far.exe").VersionInfo.FileVersion
" local version: `e[93mv$local_version`e[0m" | Write-Host
"remote version: " | Write-Host -NoNewLine
$json = Invoke-RestMethod -Uri "https://api.github.com/repos/FarGroup/FarManager/releases/latest"
$json.Name | Write-Host -ForegroundColor Green
Write-Host
$select = Select-Button -Prompt 'update?' -Buttons 'yes', 'no' -Position 1
if ($select -eq 0) {
if ([bool](Get-Process -Name far -ErrorAction SilentlyContinue)) { 'FAR is running, close it and try again' | Write-Host -ForegroundColor Red }
else {
$downloads = ($json.assets | Where-Object { ($_.name -like "Far.x64*.7z") -and ($_.name -notlike "*.pdb.7z") }) -as [array]
if ($downloads.Count -ne 0) {
$temp = New-TemporaryFile
$tempdir = "$Env:TEMP\$($temp.BaseName)"
'downloading...' | Write-Host
curl -#L -o $temp.FullName $downloads[0].browser_download_url
if ($?) {
'extracting...' | Write-Host
7z x -bso0 -bse1 -bsp1 -o"$tempdir" $temp.FullName
'removing junk...' | Write-Host
Set-Location -Path $tempdir
$to_remove | Resolve-Path | Remove-Item -Recurse
if ($?) {
'updating...' | Write-Host
Copy-Item -Path * -Destination $path_to_FAR -Recurse -Force
Copy-Item -Path "$Env:USERPROFILE\Portable\_Path\7z.dll" -Destination "$path_to_FAR\Plugins\ArcLite" -Force
Set-Location -Path $location.Path
$temp.FullName, $tempdir | Remove-Item -Recurse
}
} else { "can't download" | Write-Host -ForegroundColor Red }
} else { 'nothing to download' | Write-Host -ForegroundColor Red }
}
'press any key...' | Write-Host -NoNewLine -ForegroundColor Yellow
[void]$Host.UI.RawUI.ReadKey()
Write-Host
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment