Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
6e343da
Clean up unused code regarding suggestion
daxian-dbw Sep 22, 2022
83a8ec8
Unify the folder name for existing subsystems
daxian-dbw Sep 22, 2022
80271e2
Refactor the suggestion framework
daxian-dbw Sep 24, 2022
8c18538
Minor fix
daxian-dbw Sep 24, 2022
f33efdf
Renaming types
daxian-dbw Sep 25, 2022
f428daf
Minor fix
daxian-dbw Sep 25, 2022
9b52e9e
Make 'UnixCommandNotFound' both a predictor and a feedback provider
daxian-dbw Sep 26, 2022
72dd6d6
Minor fix
daxian-dbw Sep 26, 2022
634e2fe
Ignore the stdout stream
daxian-dbw Sep 28, 2022
de7beeb
Fix fuzzy feedback
daxian-dbw Sep 28, 2022
977e15d
Fiz fuzzy feedback (2)
daxian-dbw Sep 28, 2022
9110754
Remove old suggestion code
daxian-dbw Oct 7, 2022
fd47f47
cleanup - wip
daxian-dbw Oct 7, 2022
7a9f00e
Cleanup - done
daxian-dbw Oct 10, 2022
2a5b4ba
Fix an error
daxian-dbw Oct 10, 2022
676c6c3
Update the suggest layout
daxian-dbw Oct 10, 2022
47cf37b
Minor fix
daxian-dbw Oct 10, 2022
3efddaa
Fix tests
daxian-dbw Oct 11, 2022
ef95744
Remove the debugging code
daxian-dbw Oct 11, 2022
e495d09
Make it an experimental feature
daxian-dbw Oct 11, 2022
baa1b17
Fix a few code factor issues
daxian-dbw Oct 11, 2022
8dba6f2
Revert some style changes to HostUtilities.cs
daxian-dbw Oct 11, 2022
6cddafa
Update the experimental json file
daxian-dbw Oct 12, 2022
55fcf94
Always register the general feedback provider
daxian-dbw Oct 12, 2022
a684a37
Add feedback styles to PSStyle.Formatting
daxian-dbw Oct 12, 2022
7a07d12
Use '-FuzzyMinimumDistance'
daxian-dbw Oct 13, 2022
b0d9183
Add test for feedback provider interface
daxian-dbw Oct 17, 2022
c060dbb
Address feedback
daxian-dbw Oct 17, 2022
50102aa
Minor fixes
daxian-dbw Oct 17, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion experimental-feature-linux.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"PSCustomTableHeaderLabelDecoration",
"PSLoadAssemblyFromNativeCode",
"PSNativeCommandErrorActionPreference",
"PSSubsystemPluginModel"
"PSSubsystemPluginModel",
"PSFeedbackProvider"
]
3 changes: 2 additions & 1 deletion experimental-feature-windows.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"PSCustomTableHeaderLabelDecoration",
"PSLoadAssemblyFromNativeCode",
"PSNativeCommandErrorActionPreference",
"PSSubsystemPluginModel"
"PSSubsystemPluginModel",
"PSFeedbackProvider"
]
84 changes: 80 additions & 4 deletions src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#pragma warning disable 1634, 1691

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
Expand All @@ -18,6 +16,7 @@
using System.Management.Automation.Remoting;
using System.Management.Automation.Remoting.Server;
using System.Management.Automation.Runspaces;
using System.Management.Automation.Subsystem.Feedback;
using System.Management.Automation.Tracing;
using System.Reflection;
using System.Runtime;
Expand Down Expand Up @@ -2489,7 +2488,6 @@ internal void Run(bool inputLoopIsNested)
if (inBlockMode)
{
// use a special prompt that denotes block mode

prompt = ">> ";
}
else
Expand All @@ -2502,7 +2500,14 @@ internal void Run(bool inputLoopIsNested)
// Evaluate any suggestions
if (previousResponseWasEmpty == false)
{
EvaluateSuggestions(ui);
if (ExperimentalFeature.IsEnabled(ExperimentalFeature.PSFeedbackProvider))
{
EvaluateFeedbacks(ui);
}
else
{
EvaluateSuggestions(ui);
}
}

// Then output the prompt
Expand Down Expand Up @@ -2805,6 +2810,77 @@ private static bool IsIncompleteParseException(Exception e)
return remoteException.ErrorRecord.CategoryInfo.Reason == nameof(IncompleteParseException);
}

private void EvaluateFeedbacks(ConsoleHostUserInterface ui)
{
// Output any training suggestions
try
{
List<FeedbackEntry> feedbacks = FeedbackHub.GetFeedback(_parent.Runspace);
if (feedbacks is null || feedbacks.Count == 0)
{
return;
}

// Feedback section starts with a new line.
ui.WriteLine();

const string Indentation = " ";
string nameStyle = PSStyle.Instance.Formatting.FeedbackProvider;
string textStyle = PSStyle.Instance.Formatting.FeedbackText;
string ansiReset = PSStyle.Instance.Reset;

if (!ui.SupportsVirtualTerminal)
{
nameStyle = string.Empty;
textStyle = string.Empty;
ansiReset = string.Empty;
}

int count = 0;
var output = new StringBuilder();

foreach (FeedbackEntry entry in feedbacks)
{
if (count > 0)
{
output.AppendLine();
}

output.Append("Suggestion [")
.Append(nameStyle)
.Append(entry.Name)
.Append(ansiReset)
.AppendLine("]:")
.Append(textStyle);

string[] lines = entry.Text.Split('\n', StringSplitOptions.RemoveEmptyEntries);
foreach (string line in lines)
{
output.Append(Indentation)
.Append(line.AsSpan().TrimEnd())
.AppendLine();
}

output.Append(ansiReset);
ui.Write(output.ToString());

count++;
output.Clear();
}

// Feedback section ends with a new line.
ui.WriteLine();
}
catch (Exception e)
{
// Catch-all OK. This is a third-party call-out.
ui.WriteErrorLine(e.Message);

LocalRunspace localRunspace = (LocalRunspace)_parent.Runspace;
localRunspace.GetExecutionContext.AppendDollarError(e);
}
}

private void EvaluateSuggestions(ConsoleHostUserInterface ui)
{
// Output any training suggestions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2030,12 +2030,15 @@ private static IEnumerable<FormatViewDefinition> ViewsOf_System_Management_Autom
.AddItemScriptBlock(@"""$($_.Strikethrough)$($_.Strikethrough.Replace(""""`e"""",'`e'))$($_.Reset)""", label: "Strikethrough")
.AddItemProperty(@"OutputRendering")
.AddItemScriptBlock(@"""$($_.Formatting.FormatAccent)$($_.Formatting.FormatAccent.Replace(""""`e"""",'`e'))$($PSStyle.Reset)""", label: "Formatting.FormatAccent")
.AddItemScriptBlock(@"""$($_.Formatting.TableHeader)$($_.Formatting.TableHeader.Replace(""""`e"""",'`e'))$($PSStyle.Reset)""", label: "Formatting.TableHeader")
.AddItemScriptBlock(@"""$($_.Formatting.ErrorAccent)$($_.Formatting.ErrorAccent.Replace(""""`e"""",'`e'))$($PSStyle.Reset)""", label: "Formatting.ErrorAccent")
.AddItemScriptBlock(@"""$($_.Formatting.Error)$($_.Formatting.Error.Replace(""""`e"""",'`e'))$($PSStyle.Reset)""", label: "Formatting.Error")
.AddItemScriptBlock(@"""$($_.Formatting.Warning)$($_.Formatting.Warning.Replace(""""`e"""",'`e'))$($PSStyle.Reset)""", label: "Formatting.Warning")
.AddItemScriptBlock(@"""$($_.Formatting.Verbose)$($_.Formatting.Verbose.Replace(""""`e"""",'`e'))$($PSStyle.Reset)""", label: "Formatting.Verbose")
.AddItemScriptBlock(@"""$($_.Formatting.Debug)$($_.Formatting.Debug.Replace(""""`e"""",'`e'))$($PSStyle.Reset)""", label: "Formatting.Debug")
.AddItemScriptBlock(@"""$($_.Formatting.TableHeader)$($_.Formatting.TableHeader.Replace(""""`e"""",'`e'))$($PSStyle.Reset)""", label: "Formatting.TableHeader")
.AddItemScriptBlock(@"""$($_.Formatting.CustomTableHeaderLabel)$($_.Formatting.CustomTableHeaderLabel.Replace(""""`e"""",'`e'))$($PSStyle.Reset)""", label: "Formatting.CustomTableHeaderLabel")
.AddItemScriptBlock(@"""$($_.Formatting.FeedbackProvider)$($_.Formatting.FeedbackProvider.Replace(""""`e"""",'`e'))$($PSStyle.Reset)""", label: "Formatting.FeedbackProvider")
.AddItemScriptBlock(@"""$($_.Formatting.FeedbackText)$($_.Formatting.FeedbackText.Replace(""""`e"""",'`e'))$($PSStyle.Reset)""", label: "Formatting.FeedbackText")
.AddItemScriptBlock(@"""$($_.Progress.Style)$($_.Progress.Style.Replace(""""`e"""",'`e'))$($PSStyle.Reset)""", label: "Progress.Style")
.AddItemScriptBlock(@"""$($_.Progress.MaxWidth)""", label: "Progress.MaxWidth")
.AddItemScriptBlock(@"""$($_.Progress.View)""", label: "Progress.View")
Expand Down Expand Up @@ -2086,12 +2089,15 @@ private static IEnumerable<FormatViewDefinition> ViewsOf_System_Management_Autom
ListControl.Create()
.StartEntry()
.AddItemScriptBlock(@"""$($_.FormatAccent)$($_.FormatAccent.Replace(""""`e"""",'`e'))$($PSStyle.Reset)""", label: "FormatAccent")
.AddItemScriptBlock(@"""$($_.TableHeader)$($_.TableHeader.Replace(""""`e"""",'`e'))$($PSStyle.Reset)""", label: "TableHeader")
.AddItemScriptBlock(@"""$($_.ErrorAccent)$($_.ErrorAccent.Replace(""""`e"""",'`e'))$($PSStyle.Reset)""", label: "ErrorAccent")
.AddItemScriptBlock(@"""$($_.Error)$($_.Error.Replace(""""`e"""",'`e'))$($PSStyle.Reset)""", label: "Error")
.AddItemScriptBlock(@"""$($_.Warning)$($_.Warning.Replace(""""`e"""",'`e'))$($PSStyle.Reset)""", label: "Warning")
.AddItemScriptBlock(@"""$($_.Verbose)$($_.Verbose.Replace(""""`e"""",'`e'))$($PSStyle.Reset)""", label: "Verbose")
.AddItemScriptBlock(@"""$($_.Debug)$($_.Debug.Replace(""""`e"""",'`e'))$($PSStyle.Reset)""", label: "Debug")
.AddItemScriptBlock(@"""$($_.TableHeader)$($_.TableHeader.Replace(""""`e"""",'`e'))$($PSStyle.Reset)""", label: "TableHeader")
.AddItemScriptBlock(@"""$($_.CustomTableHeaderLabel)$($_.CustomTableHeaderLabel.Replace(""""`e"""",'`e'))$($PSStyle.Reset)""", label: "CustomTableHeaderLabel")
.AddItemScriptBlock(@"""$($_.FeedbackProvider)$($_.FeedbackProvider.Replace(""""`e"""",'`e'))$($PSStyle.Reset)""", label: "FeedbackProvider")
.AddItemScriptBlock(@"""$($_.FeedbackText)$($_.FeedbackText.Replace(""""`e"""",'`e'))$($PSStyle.Reset)""", label: "FeedbackText")
.EndEntry()
.EndList());
}
Expand Down
22 changes: 22 additions & 0 deletions src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,28 @@ public string Debug
}

private string _debug = "\x1b[33;1m";

/// <summary>
/// Gets or sets the style for rendering feedback provider names.
/// </summary>
public string FeedbackProvider
{
get => _feedbackProvider;
set => _feedbackProvider = ValidateNoContent(value);
}

private string _feedbackProvider = "\x1b[33m";

/// <summary>
/// Gets or sets the style for rendering feedback text.
/// </summary>
public string FeedbackText
{
get => _feedbackText;
set => _feedbackText = ValidateNoContent(value);
}

private string _feedbackText = "\x1b[96m";
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class ExperimentalFeature
internal const string EngineSource = "PSEngine";
internal const string PSNativeCommandErrorActionPreferenceFeatureName = "PSNativeCommandErrorActionPreference";
internal const string PSCustomTableHeaderLabelDecoration = "PSCustomTableHeaderLabelDecoration";
internal const string PSFeedbackProvider = "PSFeedbackProvider";

#endregion

Expand Down Expand Up @@ -120,6 +121,9 @@ static ExperimentalFeature()
new ExperimentalFeature(
name: PSCustomTableHeaderLabelDecoration,
description: "Formatting differentiation for table header labels that aren't property members"),
new ExperimentalFeature(
name: PSFeedbackProvider,
description: "Replace the hard-coded suggestion framework with the extensible feedback provider"),
};

EngineExperimentalFeatures = new ReadOnlyCollection<ExperimentalFeature>(engineFeatures);
Expand Down
Loading