C
C#ā€¢2y ago
krazycay

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

how could i do this?
51 Replies
Angius
Angiusā€¢2y ago
You want to change the same text's colour? Or print a new thing in a different colour every second?
krazycay
krazycayOPā€¢2y ago
same texts color
Angius
Angiusā€¢2y 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
krazycayOPā€¢2y ago
problem is i have other code running which does this lemme just show u
krazycay
krazycayOPā€¢2y ago
Angius
Angiusā€¢2y ago
And the issue is...? With my solution, that is
krazycay
krazycayOPā€¢2y ago
it would make me reposition the cursor and then make me type where i want to change the color lol
Angius
Angiusā€¢2y ago
Ah, you want the text that user enters to change colour?
krazycay
krazycayOPā€¢2y ago
nono i want the logo lizthux to be color changing rainbow
Angius
Angiusā€¢2y 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
krazycayOPā€¢2y ago
Would it be transferrable across machines without downloading stuff on their machine?
Angius
Angiusā€¢2y ago
Of course When you publish a project, all the dependencies are compiled with it
krazycay
krazycayOPā€¢2y 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ā€¢2y ago
No, it's just a C# library that makes it a bit easier to work with console
krazycay
krazycayOPā€¢2y ago
alrighty how would i import it
Angius
Angiusā€¢2y 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
krazycayOPā€¢2y ago
where do i find package manager ui lmao
krazycay
krazycayOPā€¢2y ago
ahh i got it so how could i do the rainbow text?
Angius
Angiusā€¢2y ago
Check the documentation of Spectre.Console The Live thing should be of help
krazycay
krazycayOPā€¢2y ago
krazycay
krazycayOPā€¢2y ago
@Angius how would i do this lol
Angius
Angiusā€¢2y ago
.Live() takes a Widget So you'd use one of the Spectre.Console widgets Rows for example
krazycay
krazycayOPā€¢2y ago
krazycay
krazycayOPā€¢2y ago
which should i use for my purpose rows doesnt work
Thinker
Thinkerā€¢2y 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
krazycayOPā€¢2y ago
Thinker
Thinkerā€¢2y 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ā€¢2y ago
This will change the text in the entire console We need to update only a part of it
Angius
Angiusā€¢2y ago
await can only be used inside of async methods
krazycay
krazycayOPā€¢2y ago
š™‡š™¤š™¤š™£š™– šŸ’¢
Does the text change at all, or just the color?
krazycay
krazycayOPā€¢2y ago
ok so whats going on is
Angius
Angiusā€¢2y ago
Just the colour
krazycay
krazycayOPā€¢2y 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ā€¢2y ago
You might need to use Spectre.Console's inputs
krazycay
krazycayOPā€¢2y ago
krazycay
krazycayOPā€¢2y ago
Alright how do i do that
Angius
Angiusā€¢2y ago
async void šŸ’€ Don't do async void plz async Task
krazycay
krazycayOPā€¢2y ago
then i can't thread it šŸ˜‚
Angius
Angiusā€¢2y ago
https://spectreconsole.net/prompts/text The documentation, of course
krazycay
krazycayOPā€¢2y ago
Thinker
Thinkerā€¢2y ago
Don't use Thread
krazycay
krazycayOPā€¢2y 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ā€¢2y ago
I think Task.Run(() => Yeh()); should work
krazycay
krazycayOPā€¢2y ago
Accord
Accordā€¢2y 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