Skip to content

Commit fe651cc

Browse files
BornToBeRootclaude
andcommitted
Fix: Run command final review findings
- The suggestion list is limited to 340 px, so it fits into the dialog (max. 500 px) and the selected entry is always visible. - WaitForLoadedAsync stops waiting after 5 seconds, so a request to a view that wasn't shown doesn't run later when the view is opened. - Completing a command uses the shortest matching name ("pin" → "ping"). - Profile names are compared like they are parsed (trimmed, without quotes), so recent commands with such names work. - Empty DNS server list entries ("1.1.1.1; ") are ignored. - Commands without arguments use the last word for the suggestions. - Command names are computed once per dialog; one close function for the run command dialog. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent b77a73b commit fe651cc

12 files changed

Lines changed: 75 additions & 30 deletions

File tree

‎Source/NETworkManager.Models/RunCommand/RunCommandParser.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ public static RunCommandParseResult Parse(string input, IEnumerable<RunCommandDe
139139
Command = command,
140140
CommandText = commandText,
141141
Position = RunCommandPosition.Target,
142-
CurrentWord = words[1],
143-
InputBeforeCurrentWord = commandPrefix,
142+
CurrentWord = endsWithWhitespace ? string.Empty : words[^1],
143+
InputBeforeCurrentWord = string.Join(" ", tokens[..^(endsWithWhitespace ? 0 : 1)]) + " ",
144144
Error = RunCommandParseError.NoArguments,
145145
ErrorValue = command.Command
146146
};

‎Source/NETworkManager.Tests/RunCommand/RunCommandParserTests.cs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,15 @@ public void NoArguments(string input)
249249
Assert.Equal(RunCommandParseError.NoArguments, result.Error);
250250
}
251251

252+
[Fact]
253+
public void NoArgumentsCurrentWordIsTheLastWord()
254+
{
255+
var result = Parse("settings foo ba");
256+
257+
Assert.Equal("ba", result.CurrentWord);
258+
Assert.Equal("settings foo ", result.InputBeforeCurrentWord);
259+
}
260+
252261
[Theory]
253262
[InlineData("ping @dns-01", "dns-01")]
254263
[InlineData("ping @Web Server 01", "Web Server 01")]
Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Threading.Tasks;
1+
using System;
2+
using System.Threading.Tasks;
23
using System.Windows;
34

45
namespace NETworkManager.Utilities.WPF;
@@ -8,27 +9,45 @@ namespace NETworkManager.Utilities.WPF;
89
/// </summary>
910
public static class FrameworkElementExtensions
1011
{
12+
/// <summary>
13+
/// Default time to wait for the <see cref="FrameworkElement.Loaded" /> event. A view that is shown is loaded
14+
/// within milliseconds.
15+
/// </summary>
16+
private static readonly TimeSpan DefaultLoadedTimeout = TimeSpan.FromSeconds(5);
17+
1118
/// <summary>
1219
/// Waits until the element is loaded (e.g. before showing a dialog from a view that was just created, which
1320
/// MahApps doesn't allow before the view is loaded). Completes immediately if the element is already loaded.
21+
/// Stops waiting after the timeout (e.g. the user switched to another view before it was loaded), so an action
22+
/// doesn't run unexpectedly when the view is shown later.
1423
/// </summary>
1524
/// <param name="element">Element to wait for.</param>
16-
/// <returns>Task that completes when the element is loaded.</returns>
17-
public static Task WaitForLoadedAsync(this FrameworkElement element)
25+
/// <param name="timeout">Maximum time to wait. Default: 5 seconds.</param>
26+
/// <returns>True if the element is loaded, false if the timeout elapsed.</returns>
27+
public static async Task<bool> WaitForLoadedAsync(this FrameworkElement element, TimeSpan? timeout = null)
1828
{
1929
if (element.IsLoaded)
20-
return Task.CompletedTask;
30+
return true;
2131

2232
var taskCompletionSource = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
2333

2434
void OnLoaded(object sender, RoutedEventArgs e)
2535
{
26-
element.Loaded -= OnLoaded;
2736
taskCompletionSource.TrySetResult();
2837
}
2938

3039
element.Loaded += OnLoaded;
3140

32-
return taskCompletionSource.Task;
41+
try
42+
{
43+
var completedTask = await Task.WhenAny(taskCompletionSource.Task,
44+
Task.Delay(timeout ?? DefaultLoadedTimeout));
45+
46+
return completedTask == taskCompletionSource.Task;
47+
}
48+
finally
49+
{
50+
element.Loaded -= OnLoaded;
51+
}
3352
}
3453
}

‎Source/NETworkManager/MainWindow.xaml.cs‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,24 +1101,23 @@ private void OpenRunAction()
11011101

11021102
var childWindow = new RunCommandChildWindow();
11031103

1104-
var childWindowViewModel = new RunCommandViewModel(instance =>
1104+
void CloseDialog()
11051105
{
11061106
childWindow.IsOpen = false;
11071107
ConfigurationManager.Current.IsChildWindowOpen = false;
11081108

11091109
ConfigurationManager.OnDialogClose();
1110+
}
1111+
1112+
var childWindowViewModel = new RunCommandViewModel(instance =>
1113+
{
1114+
CloseDialog();
11101115

11111116
if (instance.IsSettings)
11121117
NavigationManager.OpenSettings();
11131118
else
11141119
NavigationManager.Open(instance.Request);
1115-
}, _ =>
1116-
{
1117-
childWindow.IsOpen = false;
1118-
ConfigurationManager.Current.IsChildWindowOpen = false;
1119-
1120-
ConfigurationManager.OnDialogClose();
1121-
});
1120+
}, _ => CloseDialog());
11221121

11231122
childWindow.DataContext = childWindowViewModel;
11241123

‎Source/NETworkManager/RunCommandManager.cs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,10 @@ public static string TryCreateRequest(RunCommandParseResult result, ProfileInfo
112112
{
113113
if (profile == null)
114114
{
115+
// The name is compared like it is parsed (without surrounding whitespace and quotes).
115116
var profiles = GetProfiles(result.Command)
116-
.Where(x => string.Equals(x.Name, result.ProfileName, StringComparison.OrdinalIgnoreCase))
117+
.Where(x => string.Equals(x.Name.Trim().Trim('"').Trim(), result.ProfileName,
118+
StringComparison.OrdinalIgnoreCase))
117119
.ToList();
118120

119121
switch (profiles.Count)
@@ -231,7 +233,8 @@ private static string ValidateDNSServer(string dnsServer)
231233
if (GetDNSServers().Any(x => string.Equals(x, dnsServer, StringComparison.OrdinalIgnoreCase)))
232234
return null;
233235

234-
foreach (var server in dnsServer.Split(';'))
236+
foreach (var server in dnsServer.Split(';',
237+
StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries))
235238
{
236239
if (!ServerConnectionInfo.TryParse(server, out var serverInfo, 53, TransportProtocol.Udp) ||
237240
!IPAddress.TryParse(serverInfo.Server, out _) && !serverInfo.Server.Contains('.'))

‎Source/NETworkManager/ViewModels/RunCommandViewModel.cs‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ public class RunCommandViewModel : ViewModelBase
4040
/// </summary>
4141
private readonly List<(string Text, RunCommandDefinition Command)> _history;
4242

43+
/// <summary>
44+
/// Names (command and aliases) and translated name per command.
45+
/// </summary>
46+
private readonly List<(RunCommandDefinition Command, List<string> Names, string TranslatedName)> _commandNames;
47+
4348
/// <summary>
4449
/// Profiles per command (loaded once per command while the dialog is open).
4550
/// </summary>
@@ -57,6 +62,12 @@ public RunCommandViewModel(Action<RunCommandViewModel> runHandler, Action<RunCom
5762
_commands = RunCommandManager.GetCommands();
5863
_runHandler = runHandler;
5964

65+
_commandNames =
66+
[
67+
.. _commands.Select(x => (x, new List<string> { x.Command }.Concat(x.Aliases).ToList(),
68+
RunCommandManager.GetTranslatedName(x)))
69+
];
70+
6071
// Entries of commands that are not available anymore (e.g. a hidden application) are not shown.
6172
_history =
6273
[
@@ -324,14 +335,13 @@ private IEnumerable<RunCommandSuggestion> GetCommandSuggestions()
324335

325336
var commands = new List<(int Rank, RunCommandSuggestion Suggestion)>();
326337

327-
foreach (var command in _commands)
338+
foreach (var (command, names, translatedName) in _commandNames)
328339
{
329-
var names = new[] { command.Command }.Concat(command.Aliases).ToList();
330-
var translatedName = RunCommandManager.GetTranslatedName(command);
331-
332-
// Complete with the command or alias that was typed (e.g. "ping" instead of "pingmonitor").
340+
// Complete with the command or alias that was typed, or the shortest one that starts with the text (e.g.
341+
// "pin" → "ping" instead of "pingmonitor").
333342
var exactName = names.FirstOrDefault(x => string.Equals(x, text, StringComparison.OrdinalIgnoreCase));
334-
var name = exactName ?? names.FirstOrDefault(x => x.StartsWith(text, StringComparison.OrdinalIgnoreCase));
343+
var name = exactName ?? names.Where(x => x.StartsWith(text, StringComparison.OrdinalIgnoreCase))
344+
.OrderBy(x => x.Length).FirstOrDefault();
335345

336346
if (name == null && !translatedName.Contains(text, StringComparison.OrdinalIgnoreCase))
337347
continue;

‎Source/NETworkManager/Views/PowerShellHostView.xaml.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public async void Open(ApplicationRequest request)
1717
{
1818
// Wait for the interface to load, before displaying the dialog to connect a new profile...
1919
// MahApps will throw an exception...
20-
await this.WaitForLoadedAsync();
20+
if (!await this.WaitForLoadedAsync())
21+
return;
2122

2223
if (!_viewModel.IsExecutableConfigured)
2324
return;

‎Source/NETworkManager/Views/PuTTYHostView.xaml.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public async void Open(ApplicationRequest request)
1717
{
1818
// Wait for the interface to load, before displaying the dialog to connect a new profile...
1919
// MahApps will throw an exception...
20-
await this.WaitForLoadedAsync();
20+
if (!await this.WaitForLoadedAsync())
21+
return;
2122

2223
if (!_viewModel.IsExecutableConfigured)
2324
return;

‎Source/NETworkManager/Views/RemoteDesktopHostView.xaml.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public async void Open(ApplicationRequest request)
1717
{
1818
// Wait for the interface to load, before displaying the dialog to connect a new Profile...
1919
// MahApps will throw an exception...
20-
await this.WaitForLoadedAsync();
20+
if (!await this.WaitForLoadedAsync())
21+
return;
2122

2223
if (request.Profile != null)
2324
_viewModel.ConnectProfile(request.Profile);

‎Source/NETworkManager/Views/RunCommandChildWindow.xaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
<!-- Suggestions -->
9090
<ListBox x:Name="ListBoxSuggestions" Grid.Row="3"
9191
Margin="0,10,0,0"
92-
MaxHeight="450"
92+
MaxHeight="340"
9393
ItemsSource="{Binding Suggestions}"
9494
SelectedItem="{Binding SelectedSuggestion, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
9595
SelectionMode="Single"

0 commit comments

Comments
 (0)