Skip to content

Free, open-source Windows optimization tool for performance, privacy, and simplicity.

Refactor and enhance optimization logic and logging

itsfatduck27cdecb5e324
diff --git a/optimizerDuck/src/Core/Helpers/OptimizationHelper.cs b/optimizerDuck/src/Core/Helpers/OptimizationHelper.cs
index 1f5a6ed..7ab3cf1 100644
--- a/optimizerDuck/src/Core/Helpers/OptimizationHelper.cs
+++ b/optimizerDuck/src/Core/Helpers/OptimizationHelper.cs
@@ -1,5 +1,4 @@
-using System.Reflection;
-using Microsoft.Extensions.Caching.Memory;
+using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using optimizerDuck.Core.Managers;
@@ -8,6 +7,7 @@ using optimizerDuck.Interfaces;
using optimizerDuck.Models;
using optimizerDuck.UI.Logger;
using Spectre.Console;
+using System.Reflection;
namespace optimizerDuck.Core.Helpers;
@@ -121,19 +121,31 @@ public static class OptimizationHelper
var raw = JsonConvert.DeserializeObject<AppxClassificationRaw>(result.Stdout);
-
+
var allSafe = raw.SafeApps.SelectMany(x => x).ToList();
var allCaution = raw.CautionApps.SelectMany(x => x).ToList();
- var maxDisplayNameLength = new[] {
- allSafe.Max(x => Defaults.SAFE_APPS.TryGetValue(x.Name, out var dn) ? dn.Length : x.Name.Length),
- allCaution.Max(x => Defaults.CAUTION_APPS.TryGetValue(x.Name, out var dn) ? dn.Length : x.Name.Length)
- }.Max() + 1;
- var maxVersionLength = new[] {
- allSafe.Max(x => x.Version?.Length ?? 0),
- allCaution.Max(x => x.Version?.Length ?? 0)
- }.Max() + 1;
+ int safeDisplayLength = allSafe.Any()
+ ? allSafe.Max(x => Defaults.SAFE_APPS.TryGetValue(x.Name, out var dn) ? dn.Length : x.Name.Length)
+ : 0;
+
+ int cautionDisplayLength = allCaution.Any()
+ ? allCaution.Max(x => Defaults.CAUTION_APPS.TryGetValue(x.Name, out var dn) ? dn.Length : x.Name.Length)
+ : 0;
+
+ var maxDisplayNameLength = Math.Max(safeDisplayLength, cautionDisplayLength) + 1;
+
+ int safeVersionLength = allSafe.Any()
+ ? allSafe.Max(x => x.Version?.Length ?? 0)
+ : 0;
+
+ int cautionVersionLength = allCaution.Any()
+ ? allCaution.Max(x => x.Version?.Length ?? 0)
+ : 0;
+
+ var maxVersionLength = Math.Max(safeVersionLength, cautionVersionLength) + 1;
+
var classification = new AppxClassification(
allSafe.Select(app =>
@@ -155,15 +167,30 @@ public static class OptimizationHelper
};
}).ToList()
);
-
- Log.LogDebug("Found {TotalSafeApps} safe apps: {SafeApps}", classification.SafeApps.Count, string.Join(", ", classification.SafeApps.Select(x => x.Name).ToList()));
- Log.LogDebug("Found {TotalCautionApps} caution apps: {CautionApps}", classification.CautionApps.Count, string.Join(", ", classification.CautionApps.Select(x => x.Name).ToList()));
+
+ var parts = new List<string>();
+
+ if (classification.SafeApps.Count > 0)
+ {
+ parts.Add($"{classification.SafeApps.Count} safe apps");
+ Log.LogDebug("Found {TotalSafeApps} safe apps: {SafeApps}", classification.SafeApps.Count, string.Join(", ", classification.SafeApps.Select(x => x.Name).ToList()));
+ }
+ if (classification.CautionApps.Count > 0)
+ {
+ parts.Add($"{classification.CautionApps.Count} caution apps");
+ Log.LogDebug("Found {TotalCautionApps} caution apps: {CautionApps}", classification.CautionApps.Count, string.Join(", ", classification.CautionApps.Select(x => x.Name).ToList()));
+ }
+
+ if (parts.Any())
+ Log.LogInformation("Found {Apps}.", string.Join(" and ", parts));
+ else
+ Log.LogWarning("No apps found.");
return classification;
});
}
-
- private static string ResolveDisplayName(string name, Dictionary<string,string> dict)
+
+ private static string ResolveDisplayName(string name, Dictionary<string, string> dict)
{
var lower = name.ToLowerInvariant();
if (dict.TryGetValue(lower, out var exact))
diff --git a/optimizerDuck/src/Core/Managers/OptimizationManager.cs b/optimizerDuck/src/Core/Managers/OptimizationManager.cs
index 2a88593..8b0b77a 100644
--- a/optimizerDuck/src/Core/Managers/OptimizationManager.cs
+++ b/optimizerDuck/src/Core/Managers/OptimizationManager.cs
@@ -1,17 +1,14 @@
-using System.Diagnostics;
-using System.Reflection;
-using Microsoft.Extensions.Caching.Memory;
-using Microsoft.Extensions.Logging;
+using Microsoft.Extensions.Logging;
using optimizerDuck.Core.Extensions;
using optimizerDuck.Core.Helpers;
using optimizerDuck.Core.Optimizers;
using optimizerDuck.Core.Services;
-using optimizerDuck.Interfaces;
using optimizerDuck.Models;
using optimizerDuck.UI;
using optimizerDuck.UI.Components;
using optimizerDuck.UI.Logger;
using Spectre.Console;
+using System.Diagnostics;
using ILogger = Microsoft.Extensions.Logging.ILogger;
namespace optimizerDuck.Core.Managers;
@@ -51,7 +48,7 @@ public class OptimizationManager(SystemSnapshot systemSnapshot)
foreach (var g in optimizationGroups)
promptTweakChoice.AddChoiceGroup(
- new OptimizationTweakChoice(null, $"[bold underline]{g.Name}[/]", null,
+ new OptimizationTweakChoice(null, $"[bold underline]{g.Name}[/]", string.Empty,
false), // hack to add group title ok vjp pro, and this cant be selected btw
g.Tweaks
);
@@ -66,19 +63,31 @@ public class OptimizationManager(SystemSnapshot systemSnapshot)
var optimizationTweakChoices =
await escapeCancellableConsole.PromptAsync(promptTweakChoice).ConfigureAwait(false);
- _selectedTweaks = new Queue<OptimizationTweakChoice>(optimizationTweakChoices.Select(t => t with { Name = t.Name.Trim(), Description = t.Description.Trim() }));
+ _selectedTweaks = new Queue<OptimizationTweakChoice>(optimizationTweakChoices);
if (_selectedTweaks.Any(t =>
t.Instance?.GetType() ==
typeof(BloatwareAndServices.RemoveBloatwareApps))) // if Bloatware selection is selected
{
+ SystemHelper.Title("Select the bloatware you want to remove");
Log.LogDebug("Bloatware selection detected, prompting for bloatware apps...");
Log.LogInformation("Loading installed bloatware apps...");
var appxClassification = OptimizationHelper.GetBloatwareChoices();
- Log.LogInformation("Found {TotalSafeApps} safe apps and {TotalCautionApps} caution apps.",
- appxClassification.SafeApps.Count, appxClassification.CautionApps.Count);
+ if (appxClassification.SafeApps.Count == 0 && appxClassification.CautionApps.Count == 0)
+ {
+ return PromptDialog.Warning(
+ "No Bloatware Detected",
+ $"""
+ [{Theme.Success}]Great news![/]
+ We [{Theme.Warning}]couldn't[/] find any bloatware installed on your system.
+ There's nothing you need to remove, so you can [{Theme.Success}]safely continue[/].
+ """,
+ new PromptOption("Continue", Theme.Success),
+ new PromptOption("Back", Theme.Warning, () => false)
+ );
+ }
var promptBloatware = new MultiSelectionPrompt<AppxPackage>()
.Title("Select the bloatware you want to remove")
@@ -88,18 +97,34 @@ public class OptimizationManager(SystemSnapshot systemSnapshot)
.UseConverter(app =>
$"{(app.DisplayName.Contains("Safe Apps") || app.DisplayName.Contains("Caution Apps") ?
app.DisplayName :
- $"[bold]{app.DisplayName}[/] [dim]{app.Version} {app.InstallLocation}[/]")
- }")
- .AddChoiceGroup(
- new AppxPackage("[bold underline lightgreen]Safe Apps[/] - [lightgreen]Recommended[/]",
- string.Empty, string.Empty, string.Empty),
- appxClassification.SafeApps)
- .AddChoiceGroup(
- new AppxPackage("[bold underline red]Caution Apps[/] - [red]May cause issues[/]", string.Empty,
- string.Empty, string.Empty),
- appxClassification.CautionApps)
+ $"[bold]{app.DisplayName}[/] [dim]{app.Version} {app.InstallLocation}[/]")}")
+
.PageSize(24);
+ if (appxClassification.SafeApps.Count > 0)
+ promptBloatware.AddChoiceGroup(
+ new AppxPackage
+ {
+ DisplayName = "[bold underline lightgreen]Safe Apps[/] - [lightgreen]Safe to remove[/]",
+ Name = "",
+ Version = "",
+ InstallLocation = ""
+ },
+ appxClassification.SafeApps);
+
+ if (appxClassification.CautionApps.Count > 0)
+ promptBloatware.AddChoiceGroup(
+ new AppxPackage
+ {
+ DisplayName = "[bold underline red]Caution Apps[/] - [red]May cause issues[/]",
+ Name = "",
+ Version = "",
+ InstallLocation = ""
+ },
+ appxClassification.CautionApps);
+
+
+
if (SelectedBloatware is { Count: > 0 })
foreach (var app in SelectedBloatware)
promptBloatware.Select(app);
@@ -108,10 +133,9 @@ public class OptimizationManager(SystemSnapshot systemSnapshot)
promptBloatware.Select(app);
var bloatwareSelection = await escapeCancellableConsole.PromptAsync(promptBloatware).ConfigureAwait(false);
-
- SelectedBloatware = new Queue<AppxPackage>(bloatwareSelection.Select(app => app with { DisplayName = app.DisplayName.TrimEnd(), Version = app.Version.TrimEnd() })
- .ToList());
-
+
+ SelectedBloatware = new Queue<AppxPackage>(bloatwareSelection);
+
}
}
catch (OperationCanceledException)
@@ -167,11 +191,13 @@ public class OptimizationManager(SystemSnapshot systemSnapshot)
{
try
{
+ selectedTweak = selectedTweak with { Name = selectedTweak.Name.Trim(), Description = selectedTweak.Description.Trim() };
SystemHelper.Title(selectedTweak.Name);
ctx.Status($"Applying [{Theme.Primary}]{selectedTweak.Name}[/]...");
using var scope = Log.BeginScope("Tweak: {TweakName}", selectedTweak.Name);
+
Rule($"[bold white] {selectedTweak.Name} [/]", Theme.Success);
Log.LogInformation("Applying {TweakName}...", selectedTweak.Name);
diff --git a/optimizerDuck/src/Core/Optimizers/BloatwareAndServices.cs b/optimizerDuck/src/Core/Optimizers/BloatwareAndServices.cs
index 48129a5..137ac49 100644
--- a/optimizerDuck/src/Core/Optimizers/BloatwareAndServices.cs
+++ b/optimizerDuck/src/Core/Optimizers/BloatwareAndServices.cs
@@ -28,8 +28,10 @@ public class BloatwareAndServices : IOptimizationGroup
while (OptimizationManager.SelectedBloatware.TryDequeue(out var appxPackage))
{
+ appxPackage = appxPackage with { DisplayName = appxPackage.DisplayName.TrimEnd(), Version = appxPackage.Version.TrimEnd() };
Log.LogInformation("Removing bloatware app: {Bloatware} ({Version})",
- appxPackage.DisplayName.TrimEnd(), appxPackage.Version.TrimEnd());
+ appxPackage.DisplayName, appxPackage.Version);
+
Log.LogDebug("Raw AppxPackage name: {AppxPackage}", appxPackage.Name);
ShellService.PowerShell($$"""
$packages = Get-AppxPackage -AllUsers | Where-Object { $_.Name -eq "{{appxPackage.Name}}" }
@@ -366,7 +368,7 @@ public class BloatwareAndServices : IOptimizationGroup
new ServiceItem("wudfsvc", ServiceStartupType.Manual)
);
Log.LogInformation("Services have been configured.");
-
+
var hasSystemSSD = s.Disk.Volumes.Any(volume => volume is { SystemDrive: true, DriveTypeDescription: "SSD" });
ServiceProcessService.ChangeServiceStartupType(
diff --git a/optimizerDuck/src/Models/OptimizationTweakChoice.cs b/optimizerDuck/src/Models/OptimizationTweakChoice.cs
index 7bce0ee..2f9111d 100644
--- a/optimizerDuck/src/Models/OptimizationTweakChoice.cs
+++ b/optimizerDuck/src/Models/OptimizationTweakChoice.cs
@@ -5,5 +5,5 @@ namespace optimizerDuck.Models;
public record struct OptimizationTweakChoice(
IOptimizationTweak? Instance,
string Name,
- string? Description,
+ string Description,
bool EnabledByDefault);
\ No newline at end of file