diff --git a/.vscode/launch.json b/.vscode/launch.json index 9ad95c5..43c7bb3 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -5,7 +5,7 @@ "version": "0.2.0", "configurations": [ { - "name": "PSEdit: Launch with Example Theme", + "name": "PSEdit (Pwsh): Launch with Example Theme", "type": "coreclr", "request": "launch", "preLaunchTask": "build", @@ -13,14 +13,29 @@ "args": [ "-NoExit", "-Command", - "& { Import-Module .\\psedit.psd1; Show-PSEditor -ConfigurationFile ..\\psedit.json }" + "& { Import-Module .\\psedit.psd1; Show-PSEditor -ConfigurationFile ${workspaceFolder}\\example.psedit.json }" ], "cwd": "${workspaceFolder}\\bin\\Debug\\netstandard2.0\\publish", "console": "externalTerminal", "stopAtEntry": false }, { - "name": ".NET Core Launch with Example Theme", + "name": "PSEdit (PowerShell): Launch with Example Theme", + "type": "clr", + "request": "launch", + "preLaunchTask": "build", + "program": "powershell", + "args": [ + "-NoExit", + "-Command", + "& { Import-Module .\\psedit.psd1; Show-PSEditor -ConfigurationFile ${workspaceFolder}\\example.psedit.json }" + ], + "cwd": "${workspaceFolder}\\bin\\Debug\\netstandard2.0\\publish", + "console": "externalTerminal", + "stopAtEntry": false + }, + { + "name": "PSEdit (Pwsh): Launch (console)", "type": "coreclr", "request": "launch", "preLaunchTask": "build", @@ -28,14 +43,14 @@ "args": [ "-NoExit", "-Command", - "& { Import-Module .\\psedit.psd1; Show-PSEditor -ConfigurationFile ${workspaceFolder}\\example.psedit.json }" + "& { Import-Module .\\psedit.psd1; Show-PSEditor}" ], "cwd": "${workspaceFolder}\\bin\\Debug\\netstandard2.0\\publish", "console": "externalTerminal", "stopAtEntry": false }, { - "name": ".NET Launch (console)", + "name": "PSEdit (PowerShell): Launch (console)", "type": "clr", "request": "launch", "preLaunchTask": "build", diff --git a/README.md b/README.md index dc904c3..746f0b2 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,9 @@ -## PSEdit +## PSEdit -Edit PowerShell scripts directly in your terminal. +Edit PowerShell scripts & JSON/YAML files directly in your terminal. -![](./screenshot.png) +![](./psedit_demo.gif) -- IntelliSense -- Syntax Higlighting -- Format on Save -- Script Execution -- Error View -- Syntax Error View ## Installation @@ -18,7 +12,6 @@ This module is available on the [PowerShell Gallery](https://www.powershellgalle ```powershell Install-Module psedit ``` - ## Editing To start the editor, you can simply call `Show-PSEditor` in a terminal. @@ -33,21 +26,53 @@ You can open a file by using the `-Path` parameter. Show-PSEditor -Path .\file.path ``` -### Syntax Errors +You can also use the `psedit` alias. +```powershell +psedit +``` -Syntax errors will be shown in the editor by a red highlight. To view the text of the syntax error, click View \ Syntax Errors. -### Formatting +## Features + +- IntelliSense +- Syntax Higlighting +- Format on Save +- Script Execution +- Error View +- Syntax Error View + +## Language support -You can format your code in the editor if you have `PSScriptAnalyzer` installed. To format a script, either press `Ctrl+Shift+R` or click Edit \ Format. If you don't have `PSScriptAnalyzer` installed, you can do so with the command below. +| Language | IntelliSense | Syntax Highlighting | Formatting | Run Code | +| ------------- | ------------ | ------------------- | ---------- | -------- | +| PowerShell | ✔ | ✔ | ✔* | ✔ | +| JSON | ✖️ | ✔ | ✔ | | +| YAML | ✖️ | ✔ | ✔ | | + +All other text based files are supported, and will be treated as plain text files. + +(* Requires `PSScriptAnalyzer` to be installed) + +You can only format your PowerShell code in the editor if you have `PSScriptAnalyzer` installed. To format a script, either press `Ctrl+Shift+R` or click Edit \ Format. If you don't have `PSScriptAnalyzer` installed, you can do so with the command below. ```powershell Install-Module PSScriptAnalyzer ``` -## Theme Support +### Syntax Errors + +Syntax errors will be shown in the editor by a red highlight. To view the text of the syntax error, click View \ Syntax Errors. + +### Theme Support + +PSEdit supports customizable themes via a `psedit.json` file in My Documents folder, or in the working directory. + +You can also provide a ConfigurationFile parameter to `psedit` to specify a custom location for the theme file. +```powershell +psedit -ConfigurationFile ".\mytheme.json" +``` -PSEdit supports customizable themes via a `psedit.json` file in the working directory. If the file is not present, a default theme is used. The theme file allows you to override editor colors for backgrounds, text, errors, and more. +If the file is not present, a default theme is used. The theme file allows you to override editor colors for backgrounds, text, errors, and more. ### Example psedit.json @@ -71,7 +96,7 @@ PSEdit supports customizable themes via a `psedit.json` file in the working dire If a color key is missing, the default value will be used. Changes to the theme file are loaded automatically when the editor starts. -## Execution +## Running code To execute your script, press `F5` to run the entire script. If you want to execute a select, you can press `F8`. You can also execute the script in the terminal and exit the editor by pressing `Ctrl+Shift+F5`. diff --git a/ShowEditorCommand.cs b/ShowEditorCommand.cs index e81132c..8bed85a 100644 --- a/ShowEditorCommand.cs +++ b/ShowEditorCommand.cs @@ -51,15 +51,34 @@ public class ShowEditorCommand : PSCmdlet public SwitchParameter DisableFormatOnSave { get; set; } protected override void BeginProcessing() - { - // Load theme from specified config file or default location - string configPath = ConfigurationFile; - if (string.IsNullOrEmpty(configPath)) + { + string configPath = ""; + if (!string.IsNullOrEmpty(ConfigurationFile)) + { + // evaluate provided config path + configPath = GetUnresolvedProviderPathFromPSPath(ConfigurationFile); + if (!File.Exists(configPath)) + { + throw new ArgumentException("Invalid filepath provided for Configuration File"); + } + } + else { - var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); - configPath = System.IO.Path.Combine(documents, "psedit.json"); + // evaluate if theme exists in My Documents + var myDocumentsThemePath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "psedit.json"); + if (File.Exists(myDocumentsThemePath)) + { + configPath = myDocumentsThemePath; + } + // evaluate if theme exists in working directory + var workingDirectoryThemePath = GetUnresolvedProviderPathFromPSPath("psedit.json"); + if (File.Exists(workingDirectoryThemePath)) + { + configPath = workingDirectoryThemePath; + } } - if (File.Exists(configPath)) + // Load theme from specified config file or default location + if (!String.IsNullOrEmpty(configPath)) { ThemeService.Instance.LoadTheme(configPath); } diff --git a/psedit_demo.gif b/psedit_demo.gif new file mode 100644 index 0000000..1bffc8e Binary files /dev/null and b/psedit_demo.gif differ diff --git a/screenshot.png b/screenshot.png deleted file mode 100644 index 520c3e9..0000000 Binary files a/screenshot.png and /dev/null differ