C
C#ā€¢15mo ago
krazycay

ā” how can i make C# writeline change color every second

how could i do this?
51 Replies
Angius
Angiusā€¢15mo ago
You want to change the same text's colour? Or print a new thing in a different colour every second?
krazycay
krazycayā€¢15mo ago
same texts color
Angius
Angiusā€¢15mo ago
For the timing part, PeriodicTimer is probably the way to go For changing colour, Console.ForegroundColor To replace the text, you'd need to delete the current text and write the new text, or overwrite the old text Look into setting the position of the cursor
krazycay
krazycayā€¢15mo ago
problem is i have other code running which does this lemme just show u
krazycay
krazycayā€¢15mo ago
Angius
Angiusā€¢15mo ago
And the issue is...? With my solution, that is
krazycay
krazycayā€¢15mo ago
it would make me reposition the cursor and then make me type where i want to change the color lol
Angius
Angiusā€¢15mo ago
Ah, you want the text that user enters to change colour?
krazycay
krazycayā€¢15mo ago
nono i want the logo lizthux to be color changing rainbow
Angius
Angiusā€¢15mo ago
Ah, huh It'll require hooking a little deeper into the console, then Probably Look into existing libraries like Spectre.Console Maybe they'll be helpful
krazycay
krazycayā€¢15mo ago
Would it be transferrable across machines without downloading stuff on their machine?
Angius
Angiusā€¢15mo ago
Of course When you publish a project, all the dependencies are compiled with it
krazycay
krazycayā€¢15mo ago
ah i thought it was like a custom terminal you had to download and replace cmd prompt terminal with it not a c# library
Angius
Angiusā€¢15mo ago
No, it's just a C# library that makes it a bit easier to work with console
krazycay
krazycayā€¢15mo ago
alrighty how would i import it
Angius
Angiusā€¢15mo ago
https://spectreconsole.net/quick-start As described in the documentation Or if you use Visual Studio (not Code) you can just open the package manager UI And search for spectre.console
krazycay
krazycayā€¢15mo ago
where do i find package manager ui lmao
krazycay
krazycayā€¢15mo ago
ahh i got it so how could i do the rainbow text?
Angius
Angiusā€¢15mo ago
Check the documentation of Spectre.Console The Live thing should be of help
krazycay
krazycayā€¢15mo ago
krazycay
krazycayā€¢15mo ago
@Angius how would i do this lol
Angius
Angiusā€¢15mo ago
.Live() takes a Widget So you'd use one of the Spectre.Console widgets Rows for example
krazycay
krazycayā€¢15mo ago
krazycay
krazycayā€¢15mo ago
which should i use for my purpose rows doesnt work
Thinker
Thinkerā€¢15mo ago
You'll want something like this
await AnsiConsole.Live(new Markup("[red]uwu[/]")).StartAsync(async ctx =>
{
while (true)
{
ctx.UpdateTarget(new Markup("[blue]uwu[/]"));
ctx.Refresh();
await Task.Delay(1000);

ctx.UpdateTarget(new Markup("[green]uwu[/]"));
ctx.Refresh();
await Task.Delay(1000);

ctx.UpdateTarget(new Markup("[red]uwu[/]"));
ctx.Refresh();
await Task.Delay(1000);
}
});
await AnsiConsole.Live(new Markup("[red]uwu[/]")).StartAsync(async ctx =>
{
while (true)
{
ctx.UpdateTarget(new Markup("[blue]uwu[/]"));
ctx.Refresh();
await Task.Delay(1000);

ctx.UpdateTarget(new Markup("[green]uwu[/]"));
ctx.Refresh();
await Task.Delay(1000);

ctx.UpdateTarget(new Markup("[red]uwu[/]"));
ctx.Refresh();
await Task.Delay(1000);
}
});
This will swap between colors every second. I don't know how well this works with writing to the console simultaneously, though
krazycay
krazycayā€¢15mo ago
Thinker
Thinkerā€¢15mo ago
The error gives you a suggestion for what to do catsip
š™‡š™¤š™¤š™£š™– šŸ’¢
using System;
using System.Threading;

class Program
{
static void Main()
{
string text = "Hello World!";
while (true)
{
Console.SetCursorPosition(0, Console.CursorTop);
Console.ForegroundColor = GetRandomConsoleColor();
Console.Write(text);
Thread.Sleep(1000);
}
}

static ConsoleColor GetRandomConsoleColor()
{
var consoleColors = Enum.GetValues(typeof(ConsoleColor));
return (ConsoleColor)consoleColors.GetValue(new Random().Next(consoleColors.Length));
}
}
using System;
using System.Threading;

class Program
{
static void Main()
{
string text = "Hello World!";
while (true)
{
Console.SetCursorPosition(0, Console.CursorTop);
Console.ForegroundColor = GetRandomConsoleColor();
Console.Write(text);
Thread.Sleep(1000);
}
}

static ConsoleColor GetRandomConsoleColor()
{
var consoleColors = Enum.GetValues(typeof(ConsoleColor));
return (ConsoleColor)consoleColors.GetValue(new Random().Next(consoleColors.Length));
}
}
Angius
Angiusā€¢15mo ago
This will change the text in the entire console We need to update only a part of it
Angius
Angiusā€¢15mo ago
await can only be used inside of async methods
krazycay
krazycayā€¢15mo ago
š™‡š™¤š™¤š™£š™– šŸ’¢
Does the text change at all, or just the color?
krazycay
krazycayā€¢15mo ago
ok so whats going on is
Angius
Angiusā€¢15mo ago
Just the colour
krazycay
krazycayā€¢15mo ago
its printing uwu twice and moving the place i type to where the top uwu is (the top uwu is the only one color changing)
š™‡š™¤š™¤š™£š™– šŸ’¢
Am I missing code that's been provided?
Angius
Angiusā€¢15mo ago
You might need to use Spectre.Console's inputs
krazycay
krazycayā€¢15mo ago
krazycay
krazycayā€¢15mo ago
Alright how do i do that
Angius
Angiusā€¢15mo ago
async void šŸ’€ Don't do async void plz async Task
krazycay
krazycayā€¢15mo ago
then i can't thread it šŸ˜‚
Angius
Angiusā€¢15mo ago
https://spectreconsole.net/prompts/text The documentation, of course
krazycay
krazycayā€¢15mo ago
Thinker
Thinkerā€¢15mo ago
Don't use Thread
krazycay
krazycayā€¢15mo ago
alr how do i call the task then @thinker227
š™‡š™¤š™¤š™£š™– šŸ’¢
@kaos I've sent you a DM. Feel free to check it whenever and I'll try and help as well.
Thinker
Thinkerā€¢15mo ago
I think Task.Run(() => Yeh()); should work
krazycay
krazycayā€¢15mo ago
Accord
Accordā€¢15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
ā” āœ… Trouble getting Dependency Injection to work in a WPF applicationHi, I think I'm losing my mind. I cannot get DI to work in my WPF application. What I'm trying to doā” SQL Table to ObjectI have a SQL Table "Card" with a column "CardNr" and a column "CardType". How would I make this in tā” How to pull nuget packages from private Github registry when doing dotnet restore in Github actionDoes anyone know how to run `dotnet restore` from a Github action on a project that uses nuget packaā” how can i re use the readline after using it in a if statementso basically im using readline and if it == "hello" for example, it'd writeline "hi!", then the progā” Moq test exception thrown after adding and saved changes to the context object```Exception has occurred: CLR/Moq.MockException Exception thrown: 'Moq.MockException' in Moq.dll: 'ā” Make package C private in project A, which inherits project B which uses package CI have 2 projects, project A and B. project A inherits project B (dependency). however, I want projā” Possible to have whole app margin/padding in NET MAUI?Would it be possible to have an internal margin/padding for the whole presentation of the app?ā” error 8007006EHello, when I want to publish my WPF application with ClickOnce, I have an error: 8007006E and I donāœ… No IntelliSense in VSCodeHey, my VSCode is lacking of IntelliSense when I code in C#. I tried everything, I'm full of despairā” Splittings UT8 textI have an UTF8 text file text that is delimited by `ā•š` for lines and then further by `&` for values.