Update edit.ps1

This commit is contained in:
Markus Fleschutz 2024-03-18 14:31:48 +01:00
parent 19164caf2b
commit 84d421b0bd

View File

@ -1,28 +1,48 @@
<# <#
.SYNOPSIS .SYNOPSIS
Opens an editor to edit a file Opens a text editor
.DESCRIPTION .DESCRIPTION
This PowerShell script opens a text editor to edit the given file. This PowerShell script opens a text editor with the given text file.
.PARAMETER Filename .PARAMETER path
Specifies the path to the filename Specifies the path to the text file (will be queried if none given)
.EXAMPLE .EXAMPLE
PS> ./edit.ps1 C:\MyFile.txt PS> ./edit.ps1 C:\MyDiary.txt
.LINK .LINK
https://github.com/fleschutz/PowerShell https://github.com/fleschutz/PowerShell
.NOTES .NOTES
Author: Markus Fleschutz | License: CC0 Author: Markus Fleschutz | License: CC0
#> #>
param([string]$Filename = "") param([string]$path = "")
function TryEditor { param([string]$editor, [string]$path)
try {
Write-Host -noNewline "$editor... "
& $editor "$path"
if ($lastExitCode -ne "0") {
"⚠️ Can't execute '$editor' - make sure it's installed and available"
exit 1
}
exit 0 # success
} catch {
return
}
}
try { try {
if ($IsLinux) { if ($path -eq "" ) { $path = Read-Host "Enter the path to the text file" }
& vi "$Filename"
if ($lastExitCode -ne "0") { throw "Can't execute 'vi' - make sure vi is installed and available" } Write-Host -noNewline "Trying "
} else { TryEditor "vim" $path
& notepad.exe "$Filename" TryEditor "vi" $path
if ($lastExitCode -ne "0") { throw "Can't execute 'notepad.exe' - make sure notepad.exe is installed and available" } TryEditor "nano" $path
} TryEditor "pico" $path
TryEditor "emacs" $path
TryEditor "notepad.exe" $path
TryEditor "wordpad.exe" $path
Write-Host ""
throw "No text editor found - use 'winget install' to install your favorite text editor."
exit 0 # success exit 0 # success
} catch { } catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"