Skip to content

Commit ed2e282

Browse files
committed
Feature: Find app path
1 parent 4d26745 commit ed2e282

File tree

8 files changed

+156
-194
lines changed

8 files changed

+156
-194
lines changed

Source/NETworkManager.Models/PowerShell/PowerShell.cs

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,41 +14,42 @@ public static class PowerShell
1414
/// <summary>
1515
/// Default installation paths for PowerShell.
1616
/// </summary>
17-
public static readonly List<string> GetDefaultInstallationPaths = new()
18-
{
17+
18+
public static readonly List<string> GetDefaultInstallationPaths =
19+
[
1920
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "PowerShell", "7", "pwsh.exe"),
2021
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "PowerShell", "7",
2122
"pwsh.exe"),
23+
2224
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows),
2325
@"System32\WindowsPowerShell\v1.0\powershell.exe")
24-
};
26+
];
2527

2628
/// <summary>
2729
/// Default SZ registry keys for the global PowerShell profile.
2830
/// </summary>
29-
private static readonly List<Tuple<string, string>> DefaultProfileRegkeysSzBase = new()
30-
{
31-
new Tuple<string, string>("FaceName", "Consolas")
32-
};
31+
private static readonly List<Tuple<string, string>> DefaultProfileRegkeysSzBase =
32+
[
33+
new("FaceName", "Consolas")
34+
];
3335

3436
/// <summary>
3537
/// Default DWORD registry keys for the global PowerShell profile.
3638
/// </summary>
37-
private static readonly List<Tuple<string, int>> DefaultProfileRegkeysDwordBase = new()
38-
{
39-
new Tuple<string, int>("CursorType", 1),
40-
new Tuple<string, int>("FontFamily", 54), // 36
41-
new Tuple<string, int>("FontSize", 1179648), // 120000
42-
new Tuple<string, int>("FontWeight", 400) // 190
43-
};
39+
private static readonly List<Tuple<string, int>> DefaultProfileRegkeysDwordBase =
40+
[
41+
new("CursorType", 1),
42+
new("FontFamily", 54), // 36
43+
new("FontSize", 1179648), // 120000
44+
new("FontWeight", 400)
45+
];
4446

4547
/// <summary>
4648
/// Default DWORD registry keys for the global PowerShell profile to delete.
4749
/// </summary>
48-
private static readonly List<string> DefaultProfileRegkeysDwordDelete = new()
49-
{
50+
private static readonly List<string> DefaultProfileRegkeysDwordDelete = [
5051
"ScreenColors"
51-
};
52+
];
5253

5354
/// <summary>
5455
/// Default DWORD registry keys for the global PowerShell profile with dark theme.
@@ -57,12 +58,11 @@ public static class PowerShell
5758
private static List<Tuple<string, int>> GetProfileRegkeysDwordDark()
5859
{
5960
return DefaultProfileRegkeysDwordBase.Concat(
60-
new[]
61-
{
62-
new Tuple<string, int>("DefaultBackground", 2434341), // HEX: 252525
61+
[
62+
new Tuple<string, int>("DefaultBackground", 2434341), // HEX: 252525
6363
new Tuple<string, int>("ColorTable00", 2434341), // HEX: 252525
6464
new Tuple<string, int>("ColorTable07", 13421772) // HEX: cccccc
65-
}).ToList();
65+
]).ToList();
6666
}
6767

6868
/// <summary>
@@ -72,12 +72,11 @@ private static List<Tuple<string, int>> GetProfileRegkeysDwordDark()
7272
private static List<Tuple<string, int>> GetProfileRegkeysDwordWhite()
7373
{
7474
return DefaultProfileRegkeysDwordBase.Concat(
75-
new[]
76-
{
77-
new Tuple<string, int>("DefaultBackground", 16777215), // HEX: FFFFFF
75+
[
76+
new Tuple<string, int>("DefaultBackground", 16777215), // HEX: FFFFFF
7877
new Tuple<string, int>("ColorTable00", 16777215), // HEX: FFFFFF
7978
new Tuple<string, int>("ColorTable07", 2434341) // HEX: 252525
80-
}).ToList();
79+
]).ToList();
8180
}
8281

8382
/// <summary>

Source/NETworkManager.Models/PuTTY/PuTTY.cs

Lines changed: 45 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -13,92 +13,76 @@ namespace NETworkManager.Models.PuTTY;
1313
public class PuTTY
1414
{
1515
/// <summary>
16-
/// Default PuTTY installation paths.
16+
/// PuTTY file name.
1717
/// </summary>
18-
public static readonly List<string> GetDefaultInstallationPaths = new()
19-
{
20-
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), _puttyFolder, _puttyFile),
21-
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), _puttyFolder, _puttyFile)
22-
};
18+
public static readonly string FileName = "putty.exe";
2319

2420
/// <summary>
2521
/// Default SZ registry keys for PuTTY profile NETworkManager.
2622
/// </summary>
27-
private static readonly List<Tuple<string, string>> DefaultProfileRegkeysSZBase = new()
28-
{
29-
new Tuple<string, string>("Colour1", "255,255,255"),
30-
new Tuple<string, string>("Colour3", "85,85,85"),
31-
new Tuple<string, string>("Colour4", "0,0,0"),
32-
new Tuple<string, string>("Colour5", "0,255,0"),
33-
new Tuple<string, string>("Colour6", "0,0,0"),
34-
new Tuple<string, string>("Colour7", "85,85,85"),
35-
new Tuple<string, string>("Colour8", "187,0,0"),
36-
new Tuple<string, string>("Colour9", "255,85,85"),
37-
new Tuple<string, string>("Colour10", "0,187,0"),
38-
new Tuple<string, string>("Colour11", "85,255,85"),
39-
new Tuple<string, string>("Colour12", "187,187,0"),
40-
new Tuple<string, string>("Colour13", "255,255,85"),
41-
new Tuple<string, string>("Colour14", "0,0,187"),
42-
new Tuple<string, string>("Colour15", "85,85,255"),
43-
new Tuple<string, string>("Colour16", "187,0,187"),
44-
new Tuple<string, string>("Colour17", "255,85,255"),
45-
new Tuple<string, string>("Colour18", "0,187,187"),
46-
new Tuple<string, string>("Colour19", "85,255,255"),
47-
new Tuple<string, string>("Colour20", "187,187,187"),
48-
new Tuple<string, string>("Colour21", "255,255,255"),
49-
new Tuple<string, string>("LineCodePage", "UTF-8"),
50-
new Tuple<string, string>("Font", "Consolas")
51-
};
23+
private static readonly List<Tuple<string, string>> DefaultProfileRegkeysSzBase =
24+
[
25+
new("Colour1", "255,255,255"),
26+
new("Colour3", "85,85,85"),
27+
new("Colour4", "0,0,0"),
28+
new("Colour5", "0,255,0"),
29+
new("Colour6", "0,0,0"),
30+
new("Colour7", "85,85,85"),
31+
new("Colour8", "187,0,0"),
32+
new("Colour9", "255,85,85"),
33+
new("Colour10", "0,187,0"),
34+
new("Colour11", "85,255,85"),
35+
new("Colour12", "187,187,0"),
36+
new("Colour13", "255,255,85"),
37+
new("Colour14", "0,0,187"),
38+
new("Colour15", "85,85,255"),
39+
new("Colour16", "187,0,187"),
40+
new("Colour17", "255,85,255"),
41+
new("Colour18", "0,187,187"),
42+
new("Colour19", "85,255,255"),
43+
new("Colour20", "187,187,187"),
44+
new("Colour21", "255,255,255"),
45+
new("LineCodePage", "UTF-8"),
46+
new("Font", "Consolas")
47+
];
5248

5349
/// <summary>
5450
/// Default DWORD registry keys for PuTTY profile NETworkManager.
5551
/// </summary>
56-
private static readonly List<Tuple<string, int>> DefaultProfileRegkeysDwordBase = new()
57-
{
58-
new Tuple<string, int>("CurType", 2),
59-
new Tuple<string, int>("FontHeight", 12),
60-
new Tuple<string, int>("BlinkCur", 1),
61-
new Tuple<string, int>("ScrollBar", 0)
62-
};
63-
64-
/// <summary>
65-
/// Name of the PuTTY folder.
66-
/// </summary>
67-
private static string _puttyFolder => "PuTTY";
68-
69-
/// <summary>
70-
/// Name of the PuTTY executable.
71-
/// </summary>
72-
private static string _puttyFile => "putty.exe";
52+
private static readonly List<Tuple<string, int>> DefaultProfileRegkeysDwordBase =
53+
[
54+
new("CurType", 2),
55+
new("FontHeight", 12),
56+
new("BlinkCur", 1),
57+
new("ScrollBar", 0)
58+
];
7359

7460
/// <summary>
7561
/// SZ registry keys for PuTTY profile NETworkManager if app theme is dark.
7662
/// </summary>
7763
/// <returns>List with SZ registry keys.</returns>
78-
private static List<Tuple<string, string>> GetProfileRegkeysSZDark()
64+
private static List<Tuple<string, string>> GetProfileRegkeysSzDark()
7965
{
80-
return DefaultProfileRegkeysSZBase.Concat(
81-
new[]
82-
{
83-
// new Tuple<string, string>("Colour0", "255,255,255"),
66+
return DefaultProfileRegkeysSzBase.Concat(
67+
[
68+
// new Tuple<string, string>("Colour0", "255,255,255"),
8469
new Tuple<string, string>("Colour0", "187,187,187"), // Foreground
8570
new Tuple<string, string>("Colour2", "37,37,37") // Background
86-
}).ToList();
71+
]).ToList();
8772
}
8873

8974
/// <summary>
9075
/// SZ registry keys for PuTTY profile NETworkManager if app theme is white.
9176
/// </summary>
9277
/// <returns>List with DWORD registry keys.</returns>
93-
private static List<Tuple<string, string>> GetProfileRegkeysSZWhite()
78+
private static List<Tuple<string, string>> GetProfileRegkeysSzWhite()
9479
{
95-
return DefaultProfileRegkeysSZBase.Concat(
96-
new[]
97-
{
98-
// new Tuple<string, string>("Colour0", "68,68,68"),
80+
return DefaultProfileRegkeysSzBase.Concat(
81+
[
82+
// new Tuple<string, string>("Colour0", "68,68,68"),
9983
new Tuple<string, string>("Colour0", "0,0,0"), // Foreground
10084
new Tuple<string, string>("Colour2", "255,255,255") // Background
101-
}).ToList();
85+
]).ToList();
10286
}
10387

10488
/// <summary>
@@ -116,7 +100,7 @@ public static void WriteDefaultProfileToRegistry(string theme)
116100

117101
if (registryKey != null)
118102
{
119-
foreach (var item in theme == "Dark" ? GetProfileRegkeysSZDark() : GetProfileRegkeysSZWhite())
103+
foreach (var item in theme == "Dark" ? GetProfileRegkeysSzDark() : GetProfileRegkeysSzWhite())
120104
registryKey.SetValue(item.Item1, item.Item2);
121105

122106
foreach (var item in DefaultProfileRegkeysDwordBase)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.IO;
3+
using System.Linq;
4+
5+
namespace NETworkManager.Utilities;
6+
7+
/// <summary>
8+
/// Helper class to interact with the applications on the system.
9+
/// </summary>
10+
public static class ApplicationHelper
11+
{
12+
/// <summary>
13+
/// Find an application in the system PATH.
14+
/// This is similar to the `where` command in Windows.
15+
/// </summary>
16+
/// <param name="fileName">The name of the application to find (like `notepad` or `notepad.exe`).</param>
17+
/// <returns>The full path to the application if found, otherwise `null`.</returns>
18+
public static string Find(string fileName)
19+
{
20+
var path = Environment.GetEnvironmentVariable("PATH");
21+
22+
if (path == null)
23+
return null;
24+
25+
var directories = path.Split(';');
26+
27+
if (!fileName.EndsWith(".exe"))
28+
fileName += ".exe";
29+
30+
return directories
31+
.Select(dir => Path.Combine(dir, fileName))
32+
.FirstOrDefault(File.Exists);
33+
}
34+
}

Source/NETworkManager.Utilities/ExternalProcessStarter.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,18 @@ public static class ExternalProcessStarter
1010
/// <param name="url">Url like: https://github.com/BornToBeRoot</param>
1111
public static void OpenUrl(string url)
1212
{
13-
// Escape the $ in the command promp
13+
// Escape the $ in the command prompt
1414
url = url.Replace("&", "^&");
1515

1616
Process.Start(new ProcessStartInfo("cmd", $"/c start {url}") { CreateNoWindow = true });
1717
}
1818

19-
public static void RunProcess(string filename)
19+
public static void RunProcess(string filename, bool asAdmin = false)
2020
{
21-
RunProcess(filename, false);
21+
RunProcess(filename, null, asAdmin);
2222
}
2323

24-
public static void RunProcess(string filename, bool asAdmin)
25-
{
26-
RunProcess(filename, "", asAdmin);
27-
}
28-
29-
public static void RunProcess(string filename, string arguments = "", bool asAdmin = false)
24+
public static void RunProcess(string filename, string arguments, bool asAdmin = false)
3025
{
3126
ProcessStartInfo info = new()
3227
{

Source/NETworkManager/MainWindow.xaml.cs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -504,13 +504,13 @@ await this.ShowMessageAsync(Strings.SettingsHaveBeenReset,
504504
if (SettingsManager.Current.WelcomeDialog_Show)
505505
{
506506

507-
var x = new WelcomeChildWindow();
507+
var welcomeChildWindow = new WelcomeChildWindow();
508508

509509
var welcomeViewModel = new WelcomeViewModel(async instance =>
510510
{
511511
IsWelcomeWindowOpen = false;
512512

513-
x.IsOpen = false;
513+
welcomeChildWindow.IsOpen = false;
514514

515515
// Set settings based on user choice
516516
SettingsManager.Current.Update_CheckForUpdatesAtStartup = instance.CheckForUpdatesAtStartup;
@@ -547,13 +547,6 @@ await this.ShowMessageAsync(Strings.SettingsHaveBeenReset,
547547
break;
548548
}
549549

550-
// Check if PuTTY is installed
551-
foreach (var file in PuTTY.GetDefaultInstallationPaths.Where(File.Exists))
552-
{
553-
SettingsManager.Current.PuTTY_ApplicationFilePath = file;
554-
break;
555-
}
556-
557550
SettingsManager.Current.WelcomeDialog_Show = false;
558551

559552
// Save it to create a settings file
@@ -562,11 +555,11 @@ await this.ShowMessageAsync(Strings.SettingsHaveBeenReset,
562555
Load();
563556
});
564557

565-
x.DataContext = welcomeViewModel;
558+
welcomeChildWindow.DataContext = welcomeViewModel;
566559

567560
IsWelcomeWindowOpen = true;
568561

569-
await this.ShowChildWindowAsync(x);
562+
await this.ShowChildWindowAsync(welcomeChildWindow);
570563
}
571564
else
572565
{

Source/NETworkManager/ViewModels/AWSSessionManagerHostViewModel.cs

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ public string InterTabPartition
6363

6464
private readonly bool _isLoading;
6565
private bool _isViewActive = true;
66-
private bool _disableFocusEmbeddedWindow;
67-
66+
6867
private bool _isAWSCLIInstalled;
6968

7069
public bool IsAWSCLIInstalled
@@ -154,27 +153,7 @@ public int SelectedTabIndex
154153
OnPropertyChanged();
155154
}
156155
}
157-
158-
private DragablzTabItem _selectedTabItem;
159-
160-
public DragablzTabItem SelectedTabItem
161-
{
162-
get => _selectedTabItem;
163-
set
164-
{
165-
if (value == _selectedTabItem)
166-
return;
167-
168-
_selectedTabItem = value;
169-
170-
// Focus embedded window on switching tab
171-
if (!_disableFocusEmbeddedWindow)
172-
FocusEmbeddedWindow();
173-
174-
OnPropertyChanged();
175-
}
176-
}
177-
156+
178157
private bool _headerContextMenuIsOpen;
179158

180159
public bool HeaderContextMenuIsOpen
@@ -854,9 +833,7 @@ private void Connect(AWSSessionManagerSessionInfo sessionInfo, string header = n
854833
new AWSSessionManagerControl(tabId, sessionInfo), tabId));
855834

856835
// Select the added tab
857-
_disableFocusEmbeddedWindow = true;
858836
SelectedTabIndex = TabItems.Count - 1;
859-
_disableFocusEmbeddedWindow = false;
860837
}
861838

862839
// Modify history list

0 commit comments

Comments
 (0)