diff --git a/.vscode/launch.json b/.vscode/launch.json index c01006234..f15ddc52f 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -14,7 +14,7 @@ "-NoProfile", "-NoExit", "-Command", - "Import-Module '${workspaceFolder}/PSReadLine/bin/Debug/netstandard2.0/PSReadLine.psd1'" + "Import-Module '${workspaceFolder}/PSReadLine/bin/Debug/net8.0/PSReadLine.psd1'" ], "console": "integratedTerminal", "justMyCode": false, diff --git a/MockPSConsole/MockPSConsole.csproj b/MockPSConsole/MockPSConsole.csproj index cebd3941d..d0f91fa71 100644 --- a/MockPSConsole/MockPSConsole.csproj +++ b/MockPSConsole/MockPSConsole.csproj @@ -4,18 +4,14 @@ Exe MockPSConsole MockPSConsole - net472;net6.0 + net8.0 512 Program.manifest true - - - - - - + + diff --git a/PSReadLine.build.ps1 b/PSReadLine.build.ps1 index e245dfae3..4d54be206 100644 --- a/PSReadLine.build.ps1 +++ b/PSReadLine.build.ps1 @@ -19,46 +19,37 @@ param( [ValidateSet("Debug", "Release")] [string]$Configuration = (property Configuration Release), - [ValidateSet("net472", "net6.0")] - [string]$TestFramework, - [switch]$CheckHelpContent ) Import-Module "$PSScriptRoot/tools/helper.psm1" -# Final bits to release go here -$targetDir = "bin/$Configuration/PSReadLine" +# Dynamically read target framework from project file +$csprojPath = "$PSScriptRoot/PSReadLine/PSReadLine.csproj" +[xml]$csproj = Get-Content $csprojPath +$targetFramework = $csproj.Project.PropertyGroup.TargetFramework | Where-Object { $_ } | Select-Object -First 1 -if (-not $TestFramework) { - $TestFramework = $IsWindows ? "net472" : "net6.0" +if (-not $targetFramework) { + throw "Could not determine TargetFramework from $csprojPath" } +Write-Verbose "Target framework: $targetFramework" + +# Final bits to release go here +$targetDir = "bin/$Configuration/PSReadLine" + function ConvertTo-CRLF([string] $text) { $text.Replace("`r`n","`n").Replace("`n","`r`n") } -$polyFillerParams = @{ - Inputs = { Get-ChildItem Polyfill/*.cs, Polyfill/Polyfill.csproj } - Outputs = "Polyfill/bin/$Configuration/netstandard2.0/Microsoft.PowerShell.PSReadLine.Polyfiller.dll" -} - $binaryModuleParams = @{ - Inputs = { Get-ChildItem PSReadLine/*.cs, PSReadLine/PSReadLine.csproj, PSReadLine/PSReadLineResources.resx, Polyfill/*.cs, Polyfill/Polyfill.csproj } - Outputs = "PSReadLine/bin/$Configuration/netstandard2.0/Microsoft.PowerShell.PSReadLine.dll" + Inputs = { Get-ChildItem PSReadLine/*.cs, PSReadLine/PSReadLine.csproj, PSReadLine/PSReadLineResources.resx } + Outputs = "PSReadLine/bin/$Configuration/$targetFramework/Microsoft.PowerShell.PSReadLine.dll" } $xUnitTestParams = @{ Inputs = { Get-ChildItem test/*.cs, test/*.json, test/PSReadLine.Tests.csproj } - Outputs = "test/bin/$Configuration/$TestFramework/PSReadLine.Tests.dll" -} - -<# -Synopsis: Build the Polyfiller assembly -#> -task BuildPolyfiller @polyFillerParams { - exec { dotnet publish -c $Configuration -f 'netstandard2.0' Polyfill } - exec { dotnet publish -c $Configuration -f 'net6.0' Polyfill } + Outputs = "test/bin/$Configuration/$targetFramework/PSReadLine.Tests.dll" } <# @@ -72,15 +63,15 @@ task BuildMainModule @binaryModuleParams { Synopsis: Build xUnit tests #> task BuildXUnitTests @xUnitTestParams { - exec { dotnet publish -f $TestFramework -c $Configuration test } + exec { dotnet publish -f $targetFramework -c $Configuration test } } <# Synopsis: Run the unit tests #> task RunTests BuildMainModule, BuildXUnitTests, { - Write-Verbose "Run tests targeting '$TestFramework' ..." - Start-TestRun -Configuration $Configuration -Framework $TestFramework + Write-Verbose "Run tests targeting $targetFramework ..." + Start-TestRun -Configuration $Configuration -Framework $targetFramework } <# @@ -97,7 +88,7 @@ task CheckHelpContent -If $CheckHelpContent { <# Synopsis: Copy all of the files that belong in the module to one place in the layout for installation #> -task LayoutModule BuildPolyfiller, BuildMainModule, { +task LayoutModule BuildMainModule, { if (-not (Test-Path $targetDir -PathType Container)) { New-Item $targetDir -ItemType Directory -Force > $null } @@ -115,17 +106,7 @@ task LayoutModule BuildPolyfiller, BuildMainModule, { Set-Content -Path (Join-Path $targetDir (Split-Path $file -Leaf)) -Value (ConvertTo-CRLF $content) -Force } - if (-not (Test-Path "$targetDir/netstd")) { - New-Item "$targetDir/netstd" -ItemType Directory -Force > $null - } - if (-not (Test-Path "$targetDir/net6plus")) { - New-Item "$targetDir/net6plus" -ItemType Directory -Force > $null - } - - Copy-Item "Polyfill/bin/$Configuration/netstandard2.0/Microsoft.PowerShell.PSReadLine.Polyfiller.dll" "$targetDir/netstd" -Force - Copy-Item "Polyfill/bin/$Configuration/net6.0/Microsoft.PowerShell.PSReadLine.Polyfiller.dll" "$targetDir/net6plus" -Force - - $binPath = "PSReadLine/bin/$Configuration/netstandard2.0/publish" + $binPath = "PSReadLine/bin/$Configuration/$targetFramework/publish" Copy-Item $binPath/Microsoft.PowerShell.PSReadLine.dll $targetDir Copy-Item $binPath/Microsoft.PowerShell.Pager.dll $targetDir diff --git a/PSReadLine/OnImportAndRemove.cs b/PSReadLine/OnImportAndRemove.cs index 70208420a..cc91701c0 100644 --- a/PSReadLine/OnImportAndRemove.cs +++ b/PSReadLine/OnImportAndRemove.cs @@ -9,29 +9,12 @@ public class OnModuleImportAndRemove : IModuleAssemblyInitializer, IModuleAssemb { public void OnImport() { - AppDomain.CurrentDomain.AssemblyResolve += ResolveAssembly; + // Module initialization - reserved for future use } public void OnRemove(PSModuleInfo module) { - AppDomain.CurrentDomain.AssemblyResolve -= ResolveAssembly; - } - - /// - /// Load the correct 'Polyfiller' assembly based on the runtime. - /// - private static Assembly ResolveAssembly(object sender, ResolveEventArgs args) - { - if (args.Name != "Microsoft.PowerShell.PSReadLine.Polyfiller, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null") - { - return null; - } - - string root = Path.GetDirectoryName(typeof(OnModuleImportAndRemove).Assembly.Location); - string subd = (Environment.Version.Major >= 6) ? "net6plus" : "netstd"; - string path = Path.Combine(root, subd, "Microsoft.PowerShell.PSReadLine.Polyfiller.dll"); - - return Assembly.LoadFrom(path); + // Module cleanup - reserved for future use } } } diff --git a/PSReadLine/PSReadLine.csproj b/PSReadLine/PSReadLine.csproj index f67e29e4c..f77b56fc8 100644 --- a/PSReadLine/PSReadLine.csproj +++ b/PSReadLine/PSReadLine.csproj @@ -5,23 +5,26 @@ Microsoft.PowerShell.PSReadLine Microsoft.PowerShell.PSReadLine $(NoWarn);CA1416 - 2.4.5.0 - 2.4.5 - 2.4.5 + 3.0.0.0 + 3.0.0 + 3.0.0 true - netstandard2.0 + net8.0 true false 9.0 - - - - + + + contentFiles + All + + + - + diff --git a/PSReadLine/PSReadLine.psd1 b/PSReadLine/PSReadLine.psd1 index 0690e85cc..4ce7eff73 100644 --- a/PSReadLine/PSReadLine.psd1 +++ b/PSReadLine/PSReadLine.psd1 @@ -1,13 +1,13 @@ @{ RootModule = 'PSReadLine.psm1' NestedModules = @("Microsoft.PowerShell.PSReadLine.dll") -ModuleVersion = '2.4.5' +ModuleVersion = '3.0.0' GUID = '5714753b-2afd-4492-a5fd-01d9e2cff8b5' Author = 'Microsoft Corporation' CompanyName = 'Microsoft Corporation' Copyright = '(c) Microsoft Corporation. All rights reserved.' Description = 'Great command line editing in the PowerShell console host' -PowerShellVersion = '5.1' +PowerShellVersion = '7.4' FormatsToProcess = 'PSReadLine.format.ps1xml' AliasesToExport = @() FunctionsToExport = 'PSConsoleHostReadLine' diff --git a/PSReadLine/PSReadLine.psm1 b/PSReadLine/PSReadLine.psm1 index 572aee80f..94d094a1e 100644 --- a/PSReadLine/PSReadLine.psm1 +++ b/PSReadLine/PSReadLine.psm1 @@ -8,4 +8,4 @@ function PSConsoleHostReadLine $lastRunStatus = $? Microsoft.PowerShell.Core\Set-StrictMode -Off [Microsoft.PowerShell.PSConsoleReadLine]::ReadLine($host.Runspace, $ExecutionContext, $lastRunStatus) -} +} \ No newline at end of file diff --git a/Polyfill/CommandPrediction.cs b/Polyfill/CommandPrediction.cs deleted file mode 100644 index e7b331a8b..000000000 --- a/Polyfill/CommandPrediction.cs +++ /dev/null @@ -1,232 +0,0 @@ -#if LEGACY - -using System.Collections.Generic; -using System.Threading.Tasks; -using System.Management.Automation.Language; - -namespace System.Management.Automation.Subsystem.Prediction -{ - /// - /// Kinds of prediction clients. - /// - public enum PredictionClientKind - { - /// - /// A terminal client, representing the command-line experience. - /// - Terminal, - - /// - /// An editor client, representing the editor experience. - /// - Editor, - } - - /// - /// The class represents a client that interacts with predictors. - /// - public sealed class PredictionClient - { - /// - /// Gets the client name. - /// - [HiddenAttribute] - public string Name { get; } - - /// - /// Gets the client kind. - /// - [HiddenAttribute] - public PredictionClientKind Kind { get; } - - /// - /// Initializes a new instance of the class. - /// - /// Name of the interactive client. - /// Kind of the interactive client. - [HiddenAttribute] - public PredictionClient(string name, PredictionClientKind kind) - { - Name = name; - Kind = kind; - } - } - - /// - /// The class represents the prediction result from a predictor. - /// - public sealed class PredictionResult - { - /// - /// Gets the Id of the predictor. - /// - [HiddenAttribute] - public Guid Id { get; } - - /// - /// Gets the name of the predictor. - /// - [HiddenAttribute] - public string Name { get; } - - /// - /// Gets the mini-session id that represents a specific invocation that returns this result. - /// When it's not specified, it's considered by a client that the predictor doesn't expect feedback. - /// - [HiddenAttribute] - public uint? Session { get; } - - /// - /// Gets the suggestions. - /// - [HiddenAttribute] - public IReadOnlyList Suggestions { get; } - - internal PredictionResult(Guid id, string name, uint? session, List suggestions) - { - Id = id; - Name = name; - Session = session; - Suggestions = suggestions; - } - } - - /// - /// The class represents a predictive suggestion generated by a predictor. - /// - public sealed class PredictiveSuggestion - { - /// - /// Gets the suggestion. - /// - [HiddenAttribute] - public string SuggestionText { get; } - - /// - /// Gets the tooltip of the suggestion. - /// - [HiddenAttribute] - public string ToolTip { get; } - - /// - /// Initializes a new instance of the class. - /// - /// The predictive suggestion text. - [HiddenAttribute] - public PredictiveSuggestion(string suggestion) - : this(suggestion, toolTip: null) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// The predictive suggestion text. - /// The tooltip of the suggestion. - [HiddenAttribute] - public PredictiveSuggestion(string suggestion, string toolTip) - { - if (string.IsNullOrEmpty(suggestion)) - { - throw new ArgumentNullException(nameof(suggestion)); - } - - SuggestionText = suggestion; - ToolTip = toolTip; - } - } - - /// - /// Provides a set of possible predictions for given input. - /// - public static class CommandPrediction - { - /// - /// Collect the predictive suggestions from registered predictors using the default timeout. - /// - /// Represents the client that initiates the call. - /// The object from parsing the current command line input. - /// The objects from parsing the current command line input. - /// A list of objects. - [HiddenAttribute] - public static Task> PredictInputAsync(PredictionClient client, Ast ast, Token[] astTokens) - { - return null; - } - - /// - /// Collect the predictive suggestions from registered predictors using the specified timeout. - /// - /// Represents the client that initiates the call. - /// The object from parsing the current command line input. - /// The objects from parsing the current command line input. - /// The milliseconds to timeout. - /// A list of objects. - [HiddenAttribute] - public static Task> PredictInputAsync(PredictionClient client, Ast ast, Token[] astTokens, int millisecondsTimeout) - { - return null; - } - - /// - /// Allow registered predictors to do early processing when a command line is accepted. - /// - /// Represents the client that initiates the call. - /// History command lines provided as references for prediction. - [HiddenAttribute] - public static void OnCommandLineAccepted(PredictionClient client, IReadOnlyList history) - { - } - - /// - /// Allow registered predictors to know the execution result (success/failure) of the last accepted command line. - /// - /// Represents the client that initiates the call. - /// The last accepted command line. - /// Whether the execution of the last command line was successful. - [HiddenAttribute] - public static void OnCommandLineExecuted(PredictionClient client, string commandLine, bool success) - { - } - - /// - /// Send feedback to a predictor when one or more suggestions from it were displayed to the user. - /// - /// Represents the client that initiates the call. - /// The identifier of the predictor whose prediction result was accepted. - /// The mini-session where the displayed suggestions came from. - /// - /// When the value is > 0, it's the number of displayed suggestions from the list returned in , starting from the index 0. - /// When the value is <= 0, it means a single suggestion from the list got displayed, and the index is the absolute value. - /// - [HiddenAttribute] - public static void OnSuggestionDisplayed(PredictionClient client, Guid predictorId, uint session, int countOrIndex) - { - } - - /// - /// Send feedback to predictors about their last suggestions. - /// - /// Represents the client that initiates the call. - /// The identifier of the predictor whose prediction result was accepted. - /// The mini-session where the accepted suggestion came from. - /// The accepted suggestion text. - [HiddenAttribute] - public static void OnSuggestionAccepted(PredictionClient client, Guid predictorId, uint session, string suggestionText) - { - } - } -} - -#else - -using System.Management.Automation.Subsystem.Prediction; -using System.Runtime.CompilerServices; - -[assembly: TypeForwardedTo(typeof(PredictionClientKind))] -[assembly: TypeForwardedTo(typeof(PredictionClient))] -[assembly: TypeForwardedTo(typeof(PredictiveSuggestion))] -[assembly: TypeForwardedTo(typeof(PredictionResult))] -[assembly: TypeForwardedTo(typeof(CommandPrediction))] - -#endif diff --git a/Polyfill/Polyfill.csproj b/Polyfill/Polyfill.csproj deleted file mode 100644 index 2cdccfbba..000000000 --- a/Polyfill/Polyfill.csproj +++ /dev/null @@ -1,22 +0,0 @@ - - - - Microsoft.PowerShell.PSReadLine.Polyfiller - 1.0.0.0 - netstandard2.0;net6.0 - true - - - - - - - - - - - - $(DefineConstants);LEGACY - - - diff --git a/appveyor.yml b/appveyor.yml index 0bc286805..57bc98f24 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -11,6 +11,8 @@ cache: install: - pwsh: | + dotnet nuget remove source PowerShell_PublicPackages + dotnet nuget add source https://api.nuget.org/v3/index.json --name nuget.org Write-Host "PS Version: $($PSVersionTable.PSVersion)" ./build.ps1 -Bootstrap @@ -19,7 +21,7 @@ build_script: ./build.ps1 -Configuration Release test_script: - - pwsh: ./build.ps1 -Test -Configuration Release -Framework net472 + - pwsh: ./build.ps1 -Test -Configuration Release artifacts: - path: .\bin\Release\PSReadLine.zip diff --git a/build.ps1 b/build.ps1 index 481f2a271..47adfcbfe 100644 --- a/build.ps1 +++ b/build.ps1 @@ -10,13 +10,13 @@ Check and install prerequisites for the build. .EXAMPLE PS > .\build.ps1 -Configuration Release - Build the main module with 'Release' configuration targeting 'netstandard2.0'. + Build the main module with 'Release' configuration targeting 'net8.0'. .EXAMPLE PS > .\build.ps1 - Build the main module with the default configuration (Debug) targeting 'netstandard2.0'. + Build the main module with the default configuration (Debug) targeting 'net8.0'. .EXAMPLE PS > .\build.ps1 -Test - Run xUnit tests with the default configuration (Debug) and the default target framework (net472 on Windows or net6.0 otherwise). + Run xUnit tests with the default configuration (Debug) targeting 'net8.0'. .PARAMETER Clean Clean the local repo, but keep untracked files. .PARAMETER Bootstrap @@ -25,13 +25,6 @@ Run tests. .PARAMETER Configuration The configuration setting for the build. The default value is 'Debug'. -.PARAMETER Framework - The target framework when testing: - - net472: run tests with .NET Framework - - net6.0: run tests with .NET 6.0 - When not specified, the target framework is determined by the current OS platform: - - use 'net472' on Windows - - use 'net6.0' on Unix platforms #> [CmdletBinding(DefaultParameterSetName = 'default')] param( @@ -47,10 +40,6 @@ param( [Parameter(ParameterSetName = 'test')] [switch] $CheckHelpContent, - [Parameter(ParameterSetName = 'test')] - [ValidateSet("net472", "net6.0")] - [string] $Framework, - [Parameter(ParameterSetName = 'default')] [Parameter(ParameterSetName = 'test')] [ValidateSet("Debug", "Release")] @@ -93,7 +82,6 @@ if (-not (Get-Module -Name InvokeBuild -ListAvailable)) { $buildTask = if ($Test) { "RunTests" } else { "ZipRelease" } $arguments = @{ Task = $buildTask; Configuration = $Configuration } -if ($Framework) { $arguments.Add("TestFramework", $Framework) } if ($CheckHelpContent) { $arguments.Add("CheckHelpContent", $true) } Invoke-Build @arguments diff --git a/global.json b/global.json new file mode 100644 index 000000000..fc22515fc --- /dev/null +++ b/global.json @@ -0,0 +1,7 @@ +{ + "sdk": { + "version": "8.0.415", + "rollForward": "latestPatch", + "allowPrerelease": false + } +} diff --git a/test/InlinePredictionTest.cs b/test/InlinePredictionTest.cs index febf0ac1f..a22cd43fd 100644 --- a/test/InlinePredictionTest.cs +++ b/test/InlinePredictionTest.cs @@ -638,7 +638,7 @@ public void Inline_HistoryAndPluginSource_Acceptance() Assert.Equal(Guid.Empty, _mockedMethods.acceptedPredictorId); Assert.Null(_mockedMethods.acceptedSuggestion); Assert.NotNull(_mockedMethods.commandHistory); - Assert.Equal(1, _mockedMethods.commandHistory.Count); + Assert.Single(_mockedMethods.commandHistory); Assert.Equal("netsh show me", _mockedMethods.commandHistory[0]); _mockedMethods.ClearPredictionFields(); @@ -839,5 +839,15 @@ public void Inline_TruncateVeryLongSuggestion() TokenClassification.Command, "vv")) )); } + + // Example: How to validate 256-color emission for predictions + // When PSReadLine emits ANSI escape codes like \x1b[38;5;238m (256-color), + // the test console now tracks them in Emitted256Colors list. + // + // Usage in tests: + // CheckThat(() => { + // // Verify PSReadLine emitted color 238 for prediction text + // Assert.Contains(_console.Emitted256Colors, c => c.ColorIndex == 238); + // }) } } diff --git a/test/MockConsole.cs b/test/MockConsole.cs index 990698671..421a4ce22 100644 --- a/test/MockConsole.cs +++ b/test/MockConsole.cs @@ -178,6 +178,9 @@ public virtual int WindowHeight public virtual int WindowTop { get; set; } + // Track 256-color/RGB escape sequences emitted by PSReadLine + public List<(int Position, bool IsForeground, int ColorIndex)> Emitted256Colors { get; } = new(); + public virtual ConsoleColor BackgroundColor { get => _backgroundColor; @@ -233,8 +236,12 @@ public virtual void Write(string s) var endSequence = s.IndexOfAny(endEscapeChars, i); var len = endSequence - i - (s[endSequence] != 'm' ? 1 : 2); var escapeSequence = s.Substring(i + 2, len); - foreach (var subsequence in escapeSequence.Split(';')) + var parts = escapeSequence.Split(';'); + + for (int j = 0; j < parts.Length; j++) { + var subsequence = parts[j]; + if (subsequence is "1" or "2" or "3") { // Ignore the font effect sequence: 1 - bold; 2 - dimmed color; 3 - italics @@ -242,7 +249,35 @@ public virtual void Write(string s) continue; } - EscapeSequenceActions[subsequence](this); + // Handle 256-color/RGB sequences: 38 (FG) or 48 (BG) + // PSReadLine uses \x1b[38;5;238m and \x1b[48;5;238m for predictions + // Track these sequences without changing color state + if (subsequence is "38" or "48") + { + bool isForeground = subsequence == "38"; + if (j + 2 < parts.Length) + { + var mode = parts[j + 1]; + if (mode == "5") + { + if (int.TryParse(parts[j + 2], out var colorIndex)) + { + Emitted256Colors.Add((writePos, isForeground, colorIndex)); + } + j += 2; // Skip mode and color index + } + else if (mode == "2" && j + 4 < parts.Length) + { + j += 4; // Skip mode and RGB values (r, g, b) + } + } + continue; + } + + if (EscapeSequenceActions.ContainsKey(subsequence)) + { + EscapeSequenceActions[subsequence](this); + } } i = endSequence; continue; @@ -462,8 +497,12 @@ public override void Write(string s) var endSequence = s.IndexOfAny(endEscapeChars, i); var len = endSequence - i - (s[endSequence] != 'm' ? 1 : 2); var escapeSequence = s.Substring(i + 2, len); - foreach (var subsequence in escapeSequence.Split(';')) + var parts = escapeSequence.Split(';'); + + for (int j = 0; j < parts.Length; j++) { + var subsequence = parts[j]; + if (subsequence is "1" or "2" or "3") { // Ignore the font effect sequence: 1 - bold; 2 - dimmed color; 3 - italics @@ -471,7 +510,35 @@ public override void Write(string s) continue; } - EscapeSequenceActions[subsequence](this); + // Handle 256-color/RGB sequences: 38 (FG) or 48 (BG) + // PSReadLine uses \x1b[38;5;238m and \x1b[48;5;238m for predictions + // Track these sequences without changing color state + if (subsequence is "38" or "48") + { + bool isForeground = subsequence == "38"; + if (j + 2 < parts.Length) + { + var mode = parts[j + 1]; + if (mode == "5") + { + if (int.TryParse(parts[j + 2], out var colorIndex)) + { + Emitted256Colors.Add((writePos, isForeground, colorIndex)); + } + j += 2; // Skip mode and color index + } + else if (mode == "2" && j + 4 < parts.Length) + { + j += 4; // Skip mode and RGB values (r, g, b) + } + } + continue; + } + + if (EscapeSequenceActions.ContainsKey(subsequence)) + { + EscapeSequenceActions[subsequence](this); + } } i = endSequence; continue; diff --git a/test/PSReadLine.Tests.csproj b/test/PSReadLine.Tests.csproj index 34598cbe9..3cf7aee80 100644 --- a/test/PSReadLine.Tests.csproj +++ b/test/PSReadLine.Tests.csproj @@ -5,7 +5,7 @@ library UnitTestPSReadLine PSReadLine.Tests - net472;net6.0 + net8.0 512 {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} False @@ -14,13 +14,8 @@ 9.0 - - - - - - - + +