加点颜色

This commit is contained in:
2026-02-02 02:34:42 +08:00
parent f8a1485ee1
commit 1a3fc7b59d
2 changed files with 67 additions and 48 deletions

View File

@@ -22,6 +22,20 @@ namespace CMLeonOS
Console.ResetColor(); Console.ResetColor();
} }
public void ShowSuccess(string message)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"{message}");
Console.ResetColor();
}
public void ShowWarning(string warning)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"{warning}");
Console.ResetColor();
}
public Shell(UserSystem userSystem) public Shell(UserSystem userSystem)
{ {
this.userSystem = userSystem; this.userSystem = userSystem;
@@ -849,7 +863,7 @@ namespace CMLeonOS
// 使用FileSystem写入目标文件 // 使用FileSystem写入目标文件
fileSystem.WriteFile(destFile, content); fileSystem.WriteFile(destFile, content);
Console.WriteLine($"File copied successfully from '{sourceFile}' to '{destFile}'"); ShowSuccess($"File copied successfully from '{sourceFile}' to '{destFile}'");
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -893,7 +907,7 @@ namespace CMLeonOS
// 删除源文件 // 删除源文件
fileSystem.DeleteFile(sourceFile); fileSystem.DeleteFile(sourceFile);
Console.WriteLine($"File moved/renamed successfully from '{sourceFile}' to '{destFile}'"); ShowSuccess($"File moved/renamed successfully from '{sourceFile}' to '{destFile}'");
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -914,7 +928,7 @@ namespace CMLeonOS
{ {
// 使用FileSystem创建空文件 // 使用FileSystem创建空文件
fileSystem.WriteFile(args, ""); fileSystem.WriteFile(args, "");
Console.WriteLine($"Empty file '{args}' created successfully"); ShowSuccess($"Empty file '{args}' created successfully");
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -1275,14 +1289,14 @@ namespace CMLeonOS
if (Directory.Exists(backupPath)) if (Directory.Exists(backupPath))
{ {
Console.WriteLine($"BackupSystem: Backup '{backupName}' already exists"); ShowWarning($"Backup '{backupName}' already exists");
Console.WriteLine("BackupSystem: Returning without creating new backup"); ShowWarning("Returning without creating new backup");
return; return;
} }
Console.WriteLine($"BackupSystem: Creating backup directory: {backupPath}"); Console.WriteLine($"BackupSystem: Creating backup directory: {backupPath}");
Directory.CreateDirectory(backupPath); Directory.CreateDirectory(backupPath);
Console.WriteLine($"BackupSystem: Backup directory created"); ShowSuccess($"Backup directory created");
// 备份系统文件 // 备份系统文件
string sysPath = @"0:\system"; string sysPath = @"0:\system";
@@ -1316,8 +1330,8 @@ namespace CMLeonOS
Console.WriteLine($"BackupSystem: User path does not exist, skipping user backup"); Console.WriteLine($"BackupSystem: User path does not exist, skipping user backup");
} }
Console.WriteLine($"BackupSystem: Backup '{backupName}' created successfully"); ShowSuccess($"Backup '{backupName}' created successfully");
Console.WriteLine($"Backup location: {backupPath}"); ShowSuccess($"Backup location: {backupPath}");
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -1362,8 +1376,8 @@ namespace CMLeonOS
CopyDirectory(backupPath, userPath, true); CopyDirectory(backupPath, userPath, true);
} }
Console.WriteLine($"Backup '{backupName}' restored successfully"); ShowSuccess($"Backup '{backupName}' restored successfully");
Console.WriteLine($"Backup location: {backupPath}"); ShowSuccess($"Backup location: {backupPath}");
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -1390,7 +1404,7 @@ namespace CMLeonOS
if (sourceFiles.Length == 0) if (sourceFiles.Length == 0)
{ {
Console.WriteLine($"CopyDirectory: Warning: No files found in source directory"); ShowWarning($"CopyDirectory: Warning: No files found in source directory");
return; return;
} }

View File

@@ -19,6 +19,20 @@ namespace CMLeonOS
public bool fixmode = Kernel.FixMode; public bool fixmode = Kernel.FixMode;
private User currentLoggedInUser; private User currentLoggedInUser;
public void ShowError(string error)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"{error}");
Console.ResetColor();
}
public void ShowSuccess(string message)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"{message}");
Console.ResetColor();
}
public UserSystem() public UserSystem()
{ {
EnsureSysDirectoryExists(); EnsureSysDirectoryExists();
@@ -102,7 +116,7 @@ namespace CMLeonOS
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine($"Error saving users: {ex.Message}"); ShowError($"Error saving users: {ex.Message}");
} }
} }
@@ -138,7 +152,7 @@ namespace CMLeonOS
while (string.IsNullOrWhiteSpace(username)) while (string.IsNullOrWhiteSpace(username))
{ {
Console.WriteLine("Username cannot be empty."); ShowError("Username cannot be empty.");
Console.Write("Username: "); Console.Write("Username: ");
username = Console.ReadLine(); username = Console.ReadLine();
} }
@@ -151,14 +165,14 @@ namespace CMLeonOS
while (password != confirmPassword) while (password != confirmPassword)
{ {
Console.WriteLine("Passwords do not match. Please try again."); ShowError("Passwords do not match. Please try again.");
Console.Write("Username: "); Console.Write("Username: ");
username = Console.ReadLine(); username = Console.ReadLine();
while (string.IsNullOrWhiteSpace(username)) while (string.IsNullOrWhiteSpace(username))
{ {
Console.WriteLine("Username cannot be empty."); ShowError("Username cannot be empty.");
Console.Write("Username: "); Console.Write("Username: ");
username = Console.ReadLine(); username = Console.ReadLine();
} }
@@ -180,14 +194,14 @@ namespace CMLeonOS
}; };
users.Add(adminUser); users.Add(adminUser);
SaveUsers(); SaveUsers();
Console.WriteLine("Admin user created successfully!"); ShowSuccess("Admin user created successfully!");
// 创建用户文件夹 // 创建用户文件夹
CreateUserFolder(username); CreateUserFolder(username);
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine($"Error creating admin user: {ex.Message}"); ShowError($"Error creating admin user: {ex.Message}");
} }
} }
@@ -213,7 +227,7 @@ namespace CMLeonOS
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine($"Error creating user folder: {ex.Message}"); ShowError($"Error creating user folder: {ex.Message}");
} }
} }
@@ -281,7 +295,7 @@ namespace CMLeonOS
if (string.IsNullOrWhiteSpace(username)) if (string.IsNullOrWhiteSpace(username))
{ {
Console.WriteLine("Username cannot be empty."); ShowError("Username cannot be empty.");
return false; return false;
} }
@@ -301,13 +315,13 @@ namespace CMLeonOS
if (foundUser == null) if (foundUser == null)
{ {
Console.WriteLine("User not found."); ShowError("User not found.");
return false; return false;
} }
if (foundUser.Password == password) if (foundUser.Password == password)
{ {
Console.WriteLine("Login successful!"); ShowSuccess("Login successful!");
Console.Beep(); Console.Beep();
// 设置当前登录用户 // 设置当前登录用户
@@ -320,7 +334,7 @@ namespace CMLeonOS
} }
else else
{ {
Console.WriteLine("Invalid password. Please try again."); ShowError("Invalid password. Please try again.");
return false; return false;
} }
} }
@@ -328,7 +342,7 @@ namespace CMLeonOS
catch catch
{ {
// 如果读取按键失败,使用普通登录 // 如果读取按键失败,使用普通登录
Console.WriteLine("Error reading key input. Using normal login."); ShowError("Error reading key input. Using normal login.");
return false; return false;
} }
@@ -350,8 +364,8 @@ namespace CMLeonOS
string[] parts = args.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); string[] parts = args.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length < 2) if (parts.Length < 2)
{ {
Console.WriteLine("Error: Please specify username and password"); ShowError("Error: Please specify username and password");
Console.WriteLine($"Usage: user add {(isAdmin ? "admin" : "user")} <username> <password>"); ShowError($"Usage: user add {(isAdmin ? "admin" : "user")} <username> <password>");
return false; return false;
} }
@@ -363,7 +377,7 @@ namespace CMLeonOS
{ {
if (user.Username.ToLower() == username.ToLower()) if (user.Username.ToLower() == username.ToLower())
{ {
Console.WriteLine($"Error: User '{username}' already exists."); ShowError($"Error: User '{username}' already exists.");
return false; return false;
} }
} }
@@ -382,14 +396,14 @@ namespace CMLeonOS
// 创建用户文件夹 // 创建用户文件夹
CreateUserFolder(username); CreateUserFolder(username);
Console.WriteLine($"{(isAdmin ? "Admin" : "User")} '{username}' created successfully!"); ShowSuccess($"{(isAdmin ? "Admin" : "User")} '{username}' created successfully!");
Console.WriteLine("You shall restart the system to apply the changes."); ShowSuccess("You shall restart the system to apply the changes.");
return true; return true;
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine($"Error adding user: {ex.Message}"); ShowError($"Error adding user: {ex.Message}");
return false; return false;
} }
} }
@@ -402,8 +416,8 @@ namespace CMLeonOS
if (string.IsNullOrWhiteSpace(username)) if (string.IsNullOrWhiteSpace(username))
{ {
Console.WriteLine("Error: Please specify username"); ShowError("Error: Please specify username");
Console.WriteLine("Usage: user delete <username>"); ShowError("Usage: user delete <username>");
return false; return false;
} }
@@ -420,7 +434,7 @@ namespace CMLeonOS
if (foundUser == null) if (foundUser == null)
{ {
Console.WriteLine($"Error: User '{username}' not found."); ShowError($"Error: User '{username}' not found.");
return false; return false;
} }
@@ -436,7 +450,7 @@ namespace CMLeonOS
if (foundUser.IsAdmin && adminCount <= 1) if (foundUser.IsAdmin && adminCount <= 1)
{ {
Console.WriteLine("Error: Cannot delete the last admin user."); ShowError("Error: Cannot delete the last admin user.");
return false; return false;
} }
@@ -444,12 +458,12 @@ namespace CMLeonOS
{ {
users.Remove(foundUser); users.Remove(foundUser);
SaveUsers(); SaveUsers();
Console.WriteLine($"User '{username}' deleted successfully!"); ShowSuccess($"User '{username}' deleted successfully!");
return true; return true;
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine($"Error deleting user: {ex.Message}"); ShowError($"Error deleting user: {ex.Message}");
return false; return false;
} }
} }
@@ -486,7 +500,7 @@ namespace CMLeonOS
// 检查是否有用户登录 // 检查是否有用户登录
if (currentLoggedInUser == null) if (currentLoggedInUser == null)
{ {
Console.WriteLine("Error: No user logged in."); ShowError("Error: No user logged in.");
return false; return false;
} }
@@ -502,31 +516,22 @@ namespace CMLeonOS
{ {
currentLoggedInUser.Password = newPassword; currentLoggedInUser.Password = newPassword;
SaveUsers(); SaveUsers();
Console.WriteLine("Password changed successfully!"); ShowSuccess("Password changed successfully!");
return true; return true;
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine($"Error changing password: {ex.Message}"); ShowError($"Error changing password: {ex.Message}");
return false; return false;
} }
} }
else else
{ {
Console.WriteLine("New passwords do not match."); ShowError("New passwords do not match.");
return false; return false;
} }
} }
public void Logout()
{
Console.WriteLine("====================================");
Console.WriteLine(" User Logout");
Console.WriteLine("====================================");
Console.WriteLine("Logging out...");
Console.WriteLine("Logout successful!");
}
private string ReadPassword() private string ReadPassword()
{ {
string password = ""; string password = "";