commit 2d7c63e7cabf811baaf16c756bee4ccc5e2d336b Author: WeeXnes Date: Thu Jun 26 16:18:01 2025 +0200 initial commit diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..40d6c91 --- /dev/null +++ b/Program.cs @@ -0,0 +1,272 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; + +class Program +{ + private class KernelMenuItem + { + public string Name { get; set; } + public bool IsSelected { get; set; } + public bool IsInitiallyInstalled { get; set; } + } + + static void Main(string[] args) + { + Console.CursorVisible = false; + Console.Clear(); + + Console.WriteLine("CachyOS Kernel Manager"); + Console.WriteLine("----------------------"); + + List menuItems = InitializeKernelMenuItems(); + int selectedIndex = 0; + + while (true) + { + DrawMenu(menuItems, selectedIndex); + + ConsoleKeyInfo key = Console.ReadKey(true); + + switch (key.Key) + { + case ConsoleKey.UpArrow: + selectedIndex = (selectedIndex == 0) ? menuItems.Count - 1 : selectedIndex - 1; + break; + case ConsoleKey.DownArrow: + selectedIndex = (selectedIndex == menuItems.Count - 1) ? 0 : selectedIndex + 1; + break; + case ConsoleKey.Spacebar: + menuItems[selectedIndex].IsSelected = !menuItems[selectedIndex].IsSelected; + break; + case ConsoleKey.Enter: + Console.Clear(); + Console.WriteLine("Processing kernel changes..."); + foreach (var item in menuItems.Where(item => !item.IsInitiallyInstalled && item.IsSelected)) + { + Console.WriteLine($"\nPreparing to install {item.Name}..."); + if (InstallKernel(item.Name)) + { + Console.WriteLine($"Kernel {item.Name} installed successfully."); + } + else + { + Console.WriteLine($"Failed to install {item.Name}. Please check the output above for errors."); + } + } + foreach (var item in menuItems.Where(item => item.IsInitiallyInstalled && !item.IsSelected)) + { + Console.WriteLine($"\nPreparing to uninstall {item.Name}..."); + if (UninstallKernel(item.Name)) + { + Console.WriteLine($"Kernel {item.Name} uninstalled successfully."); + } + else + { + Console.WriteLine($"Failed to uninstall {item.Name}. Please check the output above for errors."); + } + } + Console.WriteLine("\nAll selected operations complete. Please reboot to apply changes. Press any key to exit."); + Console.ReadKey(true); + return; + case ConsoleKey.Q: + return; + } + } + } + + static void DrawMenu(List menuItems, int selectedIndex) + { + Console.SetCursorPosition(0, 3); + for (int i = 0; i < menuItems.Count; i++) + { + Console.SetCursorPosition(0, 3 + i); + if (i == selectedIndex) + { + Console.BackgroundColor = ConsoleColor.White; + Console.ForegroundColor = ConsoleColor.Black; + } + Console.Write($"{(menuItems[i].IsSelected ? "[X]" : "[ ]")}" + " " + $"{menuItems[i].Name}"); + Console.ResetColor(); + Console.WriteLine(new string(' ', Console.WindowWidth - Console.CursorLeft)); + } + Console.SetCursorPosition(0, 3 + menuItems.Count); + Console.WriteLine("\nUse \u2191\u2193 to navigate, SPACE to select/deselect, ENTER to apply, Q to quit."); + } + + static List InitializeKernelMenuItems() + { + var installedKernels = GetInstalledKernels(); + var availableCachyosKernels = GetAvailableCachyosKernels(); + + var commonArchKernels = new List + { + "linux", + "linux-lts", + "linux-zen", + "linux-hardened", + "linux-rt" + }; + + var allKernelNames = availableCachyosKernels + .Concat(commonArchKernels) + .Distinct() + .OrderBy(k => k) + .ToList(); + + var menuItems = new List(); + foreach (var kernelName in allKernelNames) + { + bool isInstalled = installedKernels.Contains(kernelName); + menuItems.Add(new KernelMenuItem + { + Name = kernelName, + IsSelected = isInstalled, + IsInitiallyInstalled = isInstalled + }); + } + return menuItems; + } + + static HashSet GetInstalledKernels() + { + var installed = new HashSet(); + var process = new Process + { + StartInfo = new ProcessStartInfo + { + FileName = "pacman", + Arguments = "-Q", + RedirectStandardOutput = true, + UseShellExecute = false, + CreateNoWindow = true, + } + }; + + try + { + process.Start(); + string output = process.StandardOutput.ReadToEnd(); + process.WaitForExit(); + + if (process.ExitCode == 0) + { + foreach (var line in output.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries)) + { + var parts = line.Split(' '); + if (parts.Length > 0) + { + string packageName = parts[0]; + if (packageName.StartsWith("linux") || packageName.StartsWith("cachyos-linux")) + { + installed.Add(packageName); + } + } + } + } + } + catch (Exception ex) + { + Console.WriteLine($"Error getting installed kernels: {ex.Message}"); + } + return installed; + } + + static string[] GetAvailableCachyosKernels() + { + var process = new Process + { + StartInfo = new ProcessStartInfo + { + FileName = "pacman", + Arguments = "-Ss linux-cachyos", + RedirectStandardOutput = true, + UseShellExecute = false, + CreateNoWindow = true, + } + }; + + try + { + process.Start(); + string output = process.StandardOutput.ReadToEnd(); + process.WaitForExit(); + + if (process.ExitCode != 0) + { + return Array.Empty(); + } + + return output.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries) + .Where(line => !line.StartsWith(" ")) + .Select(line => line.Split('/')[1].Split(' ')[0]) + .Distinct() + .ToArray(); + } + catch (Exception ex) + { + Console.WriteLine($"Error searching for CachyOS kernels: {ex.Message}"); + return Array.Empty(); + } + } + + static bool InstallKernel(string kernelName) + { + string headersPackageName = kernelName + "-headers"; + Console.WriteLine($"Running: sudo pacman -S --noconfirm {kernelName} {headersPackageName}"); + Console.WriteLine("You may be prompted for your password."); + + var process = new Process + { + StartInfo = new ProcessStartInfo + { + FileName = "sudo", + Arguments = $"pacman -S --noconfirm {kernelName} {headersPackageName}", + UseShellExecute = true, + } + }; + + try + { + process.Start(); + process.WaitForExit(); + return process.ExitCode == 0; + } + catch (Exception ex) + { + Console.WriteLine($"Error installing kernel: {ex.Message}"); + return false; + } + } + + static bool UninstallKernel(string kernelName) + { + string headersPackageName = kernelName + "-headers"; + Console.WriteLine($"Running: sudo pacman -Rns --noconfirm {kernelName} {headersPackageName}"); + Console.WriteLine("You may be prompted for your password."); + + var process = new Process + { + StartInfo = new ProcessStartInfo + { + FileName = "sudo", + Arguments = $"pacman -Rns --noconfirm {kernelName} {headersPackageName}", + UseShellExecute = true, + } + }; + + try + { + process.Start(); + process.WaitForExit(); + return process.ExitCode == 0; + } + catch (Exception ex) + { + Console.WriteLine($"Error uninstalling kernel: {ex.Message}"); + return false; + } + } +} +