Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using WriteLine while waiting for AskAsync? #1728

Closed
Charanor opened this issue Jan 8, 2025 · 1 comment
Closed

Using WriteLine while waiting for AskAsync? #1728

Charanor opened this issue Jan 8, 2025 · 1 comment

Comments

@Charanor
Copy link

Charanor commented Jan 8, 2025

Hello, I have a console application that sends & receives data from a server. The problem I'm facing is if I want to print the result from the server using AnsiConsole.WriteLine it will write the text on the same line as the prompt from AnsiConsole.AskAsync, for example when the user sends "hello" the server responds with "world":

Here is the AskAsync prompt: hello
Here is the AskAsync prompt: world  <-- The response from the server is placed on the same line as the prompt
asd  <-- The input itself continues on the next line
Here is the AskAsync prompt: hello
Here is the AskAsync prompt: world
hello
Here is the AskAsync prompt: world
asd
Here is the AskAsync prompt:

I would expect (want) it to look like this:

Here is the AskAsync prompt: hello
world <-- The response from the server is placed on its own line
Here is the AskAsync prompt: asd <-- The input continues on the next line
Here is the AskAsync prompt: hello
world
Here is the AskAsync prompt: hello
world
Here is the AskAsync prompt: asd
Here is the AskAsync prompt:

The code itself boils down to this:

HandleInputAsync();

while (true)
{
    if (GetServerMessage(out var message))
    {
        AnsiConsole.WriteLine(message);
    }

    Thread.Sleep(100);
}

async void HandleInputAsync()
{
	while (true)
	{
		var input = await AnsiConsole.AskAsync<string>("Here is the AskAsync prompt:");
		Send(input); // Send input to server
	}
}

I have tried many things like using cancellation tokens and to no avail, the prompt ("Here is the AskAsync prompt:") still stays before the message and the actual input is placed on a new line without the prompt before it. Is what I'm trying to do possible?

If important, I'm using the default cmd console in Windows 10 (but behaviour is the same in all other consoles I've tested, like GitBash).

P.S. was not sure if I should mark this as a bug since it could be intended.

@patriksvensson
Copy link
Contributor

Yes, this is not a supported scenario at the moment. AskAsync (and other prompts that accepts input except selection prompts) are not based on a LiveRenderable and therefore cannot be used in this manner.

@Charanor Charanor closed this as completed Jan 9, 2025
@github-project-automation github-project-automation bot moved this from Todo 🕑 to Done 🚀 in Spectre Console Jan 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done 🚀
Development

No branches or pull requests

2 participants