GsDChat.Client fixes:
- Command output not being displayed. - Screen not cleaning up. + Better initial UI.
This commit is contained in:
parent
b60b5a0774
commit
302d440dbd
12 changed files with 348 additions and 47 deletions
|
|
@ -41,7 +41,7 @@ public class Program
|
|||
Console.WriteLine($"========================================{Ansi.Reset}");
|
||||
|
||||
var historyManager = new ServerHistoryManager();
|
||||
string? rememberedUsername = null;
|
||||
UserProfile? selectedProfile = null;
|
||||
|
||||
// If no server was specified via CLI, show interactive server selection menu
|
||||
while (string.IsNullOrEmpty(serverUrl))
|
||||
|
|
@ -54,8 +54,8 @@ public class Program
|
|||
for (int i = 0; i < servers.Count; i++)
|
||||
{
|
||||
var s = servers[i];
|
||||
string userTag = !string.IsNullOrEmpty(s.LastUsername) ? $" (@{s.LastUsername})" : "";
|
||||
Console.WriteLine($" {Ansi.BrightYellow}[{i + 1}]{Ansi.Reset} {Ansi.BrightWhite}{s.Name}{Ansi.Reset} {Ansi.Gray}({s.Url}){userTag}{Ansi.Reset}");
|
||||
string profileCount = s.UserProfiles.Count > 0 ? $" ({s.UserProfiles.Count} profile{(s.UserProfiles.Count > 1 ? "s" : "")})" : "";
|
||||
Console.WriteLine($" {Ansi.BrightYellow}[{i + 1}]{Ansi.Reset} {Ansi.BrightWhite}{s.Name}{Ansi.Reset} {Ansi.Gray}({s.Url}){profileCount}{Ansi.Reset}");
|
||||
}
|
||||
}
|
||||
else
|
||||
|
|
@ -71,27 +71,42 @@ public class Program
|
|||
Console.WriteLine($" {Ansi.Gray}[Q]{Ansi.Reset} Quit");
|
||||
|
||||
Console.Write($"\nSelect option: ");
|
||||
string? input = Console.ReadLine()?.Trim();
|
||||
char input = ReadKeyChar();
|
||||
|
||||
if (string.Equals(input, "Q", StringComparison.OrdinalIgnoreCase))
|
||||
if (char.ToUpper(input) == 'Q')
|
||||
{
|
||||
Console.WriteLine("Goodbye.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (string.Equals(input, "D", StringComparison.OrdinalIgnoreCase) && servers.Count > 0)
|
||||
if (char.ToUpper(input) == 'D' && servers.Count > 0)
|
||||
{
|
||||
selectedProfile = null;
|
||||
Console.Write("Enter number to delete: ");
|
||||
if (int.TryParse(Console.ReadLine(), out int delIdx) && delIdx >= 1 && delIdx <= servers.Count)
|
||||
char delChar = ReadKeyChar();
|
||||
if (delChar >= '1' && delChar <= '9')
|
||||
{
|
||||
historyManager.RemoveServer(servers[delIdx - 1].Id);
|
||||
Console.WriteLine($"{Ansi.Green}Removed server from history.{Ansi.Reset}");
|
||||
int delIdx = delChar - '0';
|
||||
if (delIdx >= 1 && delIdx <= servers.Count)
|
||||
{
|
||||
historyManager.RemoveServer(servers[delIdx - 1].Id);
|
||||
Console.WriteLine($"{Ansi.Green}Removed server from history.{Ansi.Reset}");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"{Ansi.BrightRed}Invalid number.{Ansi.Reset}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"{Ansi.BrightRed}Invalid input.{Ansi.Reset}");
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (string.Equals(input, "N", StringComparison.OrdinalIgnoreCase) || servers.Count == 0 && string.IsNullOrEmpty(input))
|
||||
if (char.ToUpper(input) == 'N' || (servers.Count == 0 && input == '\r'))
|
||||
{
|
||||
selectedProfile = null;
|
||||
Console.Write("\nEnter Server URL or IP (e.g. chat.example.com, http://localhost:5000): ");
|
||||
string? customUrl = Console.ReadLine()?.Trim();
|
||||
if (!string.IsNullOrEmpty(customUrl))
|
||||
|
|
@ -101,12 +116,34 @@ public class Program
|
|||
continue;
|
||||
}
|
||||
|
||||
if (int.TryParse(input, out int sel) && sel >= 1 && sel <= servers.Count)
|
||||
if (input >= '1' && input <= '9')
|
||||
{
|
||||
var chosen = servers[sel - 1];
|
||||
serverUrl = chosen.Url;
|
||||
rememberedUsername = chosen.LastUsername;
|
||||
break;
|
||||
int sel = input - '0';
|
||||
if (sel >= 1 && sel <= servers.Count)
|
||||
{
|
||||
var chosen = servers[sel - 1];
|
||||
serverUrl = chosen.Url;
|
||||
selectedProfile = null;
|
||||
|
||||
// Show profile selection if profiles exist
|
||||
if (chosen.UserProfiles.Count > 0)
|
||||
{
|
||||
selectedProfile = SelectProfile(chosen);
|
||||
if (selectedProfile == null)
|
||||
{
|
||||
// User cancelled profile selection, reset serverUrl and let them choose again
|
||||
serverUrl = null;
|
||||
continue;
|
||||
}
|
||||
// If user chose "use different username", clear selectedProfile
|
||||
if (string.IsNullOrEmpty(selectedProfile.Username))
|
||||
{
|
||||
selectedProfile = null;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine($"{Ansi.BrightRed}Invalid choice. Please select again.{Ansi.Reset}");
|
||||
|
|
@ -129,17 +166,19 @@ public class Program
|
|||
{
|
||||
Console.WriteLine($"{Ansi.BrightRed}Failed to reach server!{Ansi.Reset}");
|
||||
Console.Write("Would you like to [R]etry, choose [A]nother server, or [Q]uit? ");
|
||||
var failChoice = Console.ReadLine()?.Trim().ToUpperInvariant();
|
||||
if (failChoice == "A")
|
||||
char failChoice = ReadKeyChar();
|
||||
if (char.ToUpper(failChoice) == 'A')
|
||||
{
|
||||
serverUrl = null;
|
||||
selectedProfile = null;
|
||||
await Main(args); // Restart server selection
|
||||
return;
|
||||
}
|
||||
if (failChoice == "Q")
|
||||
if (char.ToUpper(failChoice) == 'Q')
|
||||
{
|
||||
return;
|
||||
}
|
||||
selectedProfile = null;
|
||||
continue; // Retry
|
||||
}
|
||||
|
||||
|
|
@ -158,23 +197,34 @@ public class Program
|
|||
|
||||
if (string.IsNullOrEmpty(username))
|
||||
{
|
||||
Console.Write("\nChoose action: [L]ogin or [R]egister: ");
|
||||
var choice = Console.ReadLine()?.Trim().ToUpperInvariant();
|
||||
if (choice == "R" || choice == "REGISTER")
|
||||
// If a profile is selected with a username, skip login/register choice and use the profile
|
||||
if (selectedProfile != null && !string.IsNullOrEmpty(selectedProfile.Username))
|
||||
{
|
||||
doRegister = true;
|
||||
username = selectedProfile.Username;
|
||||
|
||||
// If profile has saved password, use it without prompting
|
||||
if (!string.IsNullOrEmpty(selectedProfile.SavedPassword))
|
||||
{
|
||||
password = selectedProfile.SavedPassword;
|
||||
Console.WriteLine($"\nUsing saved profile: {Ansi.BrightCyan}{username}{Ansi.Reset}");
|
||||
Console.WriteLine("Password: (saved)");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"\nUsing saved profile: {Ansi.BrightCyan}{username}{Ansi.Reset}");
|
||||
Console.Write("Password: ");
|
||||
password = ReadMaskedPassword();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// No profile selected or user chose different username - skip login/register since we have a server
|
||||
Console.Write("\nUsername: ");
|
||||
username = Console.ReadLine()?.Trim();
|
||||
|
||||
string userPrompt = !string.IsNullOrEmpty(rememberedUsername)
|
||||
? $"Username [{rememberedUsername}]: "
|
||||
: "Username: ";
|
||||
|
||||
Console.Write(userPrompt);
|
||||
string? enteredUser = Console.ReadLine()?.Trim();
|
||||
username = string.IsNullOrEmpty(enteredUser) ? rememberedUsername : enteredUser;
|
||||
|
||||
Console.Write("Password: ");
|
||||
password = ReadMaskedPassword();
|
||||
Console.Write("Password: ");
|
||||
password = ReadMaskedPassword();
|
||||
}
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password))
|
||||
|
|
@ -182,6 +232,7 @@ public class Program
|
|||
Console.WriteLine($"{Ansi.BrightRed}Username and password are required.{Ansi.Reset}");
|
||||
username = null;
|
||||
password = null;
|
||||
selectedProfile = null;
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
@ -196,13 +247,38 @@ public class Program
|
|||
Console.WriteLine($"{Ansi.BrightRed}Authentication failed: {authResult.ErrorMessage}{Ansi.Reset}");
|
||||
username = null;
|
||||
password = null;
|
||||
selectedProfile = null;
|
||||
continue;
|
||||
}
|
||||
|
||||
Console.WriteLine($"{Ansi.BrightGreen}Success! Welcome @{authResult.Username} [{authResult.Role}]{Ansi.Reset}");
|
||||
|
||||
// Save server to history (with upgraded protocol if applicable)
|
||||
historyManager.SaveServer(state.ServerUrl, info.ServerName, authResult.Username);
|
||||
// Handle profile saving
|
||||
if (!string.IsNullOrEmpty(authResult.Username))
|
||||
{
|
||||
if (selectedProfile == null || selectedProfile.Username != authResult.Username)
|
||||
{
|
||||
// Ask if user wants to save this profile
|
||||
Console.Write($"\nSave this profile for {Ansi.BrightCyan}{info.ServerName}{Ansi.Reset}? [Y/n]: ");
|
||||
char saveChoice = ReadKeyChar();
|
||||
|
||||
if (char.ToUpper(saveChoice) != 'N')
|
||||
{
|
||||
// Ask if user wants to save password
|
||||
Console.Write($"Save password for {Ansi.BrightCyan}{authResult.Username}{Ansi.Reset}? [y/N]: ");
|
||||
char savePasswordChoice = ReadKeyChar();
|
||||
bool savePassword = char.ToUpper(savePasswordChoice) == 'Y';
|
||||
|
||||
historyManager.SaveProfile(state.ServerUrl, authResult.Username, password, savePassword);
|
||||
Console.WriteLine(savePassword ? $"{Ansi.Green}Profile and password saved.{Ansi.Reset}" : $"{Ansi.Green}Profile saved (password not saved).{Ansi.Reset}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Update last used time for existing profile
|
||||
historyManager.SaveProfile(state.ServerUrl, authResult.Username, null, false);
|
||||
}
|
||||
}
|
||||
|
||||
state.Token = authResult.Token;
|
||||
state.CurrentUserId = authResult.UserId ?? "";
|
||||
|
|
@ -252,4 +328,68 @@ public class Program
|
|||
}
|
||||
return pwd;
|
||||
}
|
||||
|
||||
private static char ReadKeyChar()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
var key = Console.ReadKey(true);
|
||||
if (key.Key == ConsoleKey.Enter)
|
||||
{
|
||||
Console.WriteLine();
|
||||
return '\r';
|
||||
}
|
||||
if (!char.IsControl(key.KeyChar))
|
||||
{
|
||||
Console.Write(key.KeyChar);
|
||||
Console.WriteLine();
|
||||
return key.KeyChar;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static UserProfile? SelectProfile(ServerProfile server)
|
||||
{
|
||||
Console.WriteLine($"\n{Ansi.Bold}Select Profile for {server.Name}:{Ansi.Reset}");
|
||||
|
||||
for (int i = 0; i < server.UserProfiles.Count; i++)
|
||||
{
|
||||
var p = server.UserProfiles[i];
|
||||
string hasPassword = !string.IsNullOrEmpty(p.SavedPassword) ? $"{Ansi.Green}[+password]{Ansi.Reset}" : $"{Ansi.Gray}[no password]{Ansi.Reset}";
|
||||
Console.WriteLine($" {Ansi.BrightYellow}[{i + 1}]{Ansi.Reset} {Ansi.BrightWhite}{p.Username}{Ansi.Reset} {hasPassword}");
|
||||
}
|
||||
|
||||
Console.WriteLine($" {Ansi.BrightCyan}[N]{Ansi.Reset} Use a different username");
|
||||
Console.WriteLine($" {Ansi.Gray}[Esc]{Ansi.Reset} Go back");
|
||||
|
||||
Console.Write($"\nSelect option: ");
|
||||
|
||||
while (true)
|
||||
{
|
||||
var key = Console.ReadKey(true);
|
||||
|
||||
if (key.Key == ConsoleKey.Escape)
|
||||
{
|
||||
Console.WriteLine();
|
||||
return null;
|
||||
}
|
||||
|
||||
if (char.ToUpper(key.KeyChar) == 'N')
|
||||
{
|
||||
Console.WriteLine();
|
||||
// Return a special marker to indicate "use different username"
|
||||
return new UserProfile { Username = "" };
|
||||
}
|
||||
|
||||
if (key.KeyChar >= '1' && key.KeyChar <= '9')
|
||||
{
|
||||
int sel = key.KeyChar - '0';
|
||||
if (sel >= 1 && sel <= server.UserProfiles.Count)
|
||||
{
|
||||
Console.WriteLine();
|
||||
return server.UserProfiles[sel - 1];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue