C
C#•3y ago
krazycay

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

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

Did you find this page helpful?