C
C#10mo ago
Bored Student

Neat String interpolation

C#
foreach(file in files){
//Process file
Console.WriteLine($"Processed File [{file.Nr}/{files.Count}]");
}
C#
foreach(file in files){
//Process file
Console.WriteLine($"Processed File [{file.Nr}/{files.Count}]");
}
I have this little something, and I want the counter to stay fixed width so [ 1/50] instead of [1/50] how can I do this ? As simple as possible, I just want it to look neat not write a TUI framework. I found $"{val , fixedWidth}" but that only works with constants so it can't adapt if I reach 100 files.
12 Replies
Angius
Angius10mo ago
{file.Nr,2} IIRC? Or -2 One is leftpad, other is rightpad Can't remember which is which
V0FBU1VM
V0FBU1VM10mo ago
foreach(var file in files){
//Process file
var fileNr = file.Nr.ToString().PadLeft(files.Count.ToString().Length, ' ');
Console.WriteLine($"Processed File [{fileNr}/{files.Count}]");
}
foreach(var file in files){
//Process file
var fileNr = file.Nr.ToString().PadLeft(files.Count.ToString().Length, ' ');
Console.WriteLine($"Processed File [{fileNr}/{files.Count}]");
}
Bored Student
Bored StudentOP10mo ago
I suppose that's the best it's gonna get, thx
Omnissiah
Omnissiah10mo ago
omg please assign files.Count.ToString().Length to a variable outside the foreach instead of calculating it every cycle also if you do that you can use the format inside the $"..." like {fileNr,pad} like Z²⁵ said
Bored Student
Bored StudentOP10mo ago
no {value,pad} requests a const value, so i can't calculate it after loading my files.
Angius
Angius10mo ago
string.Format($"[{{0}},{pad}/{{1}}]", nr, count)
string.Format($"[{{0}},{pad}/{{1}}]", nr, count)
i like chatgpt
i like chatgpt10mo ago
IList<File> files = GetFiles();
int filesCount = files.Count;
int digitsCount = filesCount.ToString().Length;

foreach (var file in files)
{
string order = file.Number.ToString().PadLeft(digitsCount, ' ');
string output = $"Processed File [{order}/{filesCount}]";
Console.WriteLine(output);
await Task.Delay(500); // simulating delay...
}

static IList<File> GetFiles(int N = 100) =>
Enumerable
.Range(0, N)
.Select(x => new File(x + 1, Path.GetRandomFileName()))
.ToList();

public record File(int Number, string Name);
IList<File> files = GetFiles();
int filesCount = files.Count;
int digitsCount = filesCount.ToString().Length;

foreach (var file in files)
{
string order = file.Number.ToString().PadLeft(digitsCount, ' ');
string output = $"Processed File [{order}/{filesCount}]";
Console.WriteLine(output);
await Task.Delay(500); // simulating delay...
}

static IList<File> GetFiles(int N = 100) =>
Enumerable
.Range(0, N)
.Select(x => new File(x + 1, Path.GetRandomFileName()))
.ToList();

public record File(int Number, string Name);
No description
Bored Student
Bored StudentOP10mo ago
thx but, we were done already
Chiyoko_S
Chiyoko_S10mo ago
I know this has been solved already but there is a decent CUI framework thingy that I found pretty nice for looking things pretty https://github.com/spectreconsole/spectre.console
GitHub
GitHub - spectreconsole/spectre.console: A .NET library that makes ...
A .NET library that makes it easier to create beautiful console applications. - GitHub - spectreconsole/spectre.console: A .NET library that makes it easier to create beautiful console applications.
Chiyoko_S
Chiyoko_S10mo ago
for example I have an app that displays progress like this
No description
Bored Student
Bored StudentOP10mo ago
And it wasn't what I looked for, for that specific script. But it looks very interesting to have in my Toolbox, so Thank you very much regardless.
Chiyoko_S
Chiyoko_S10mo ago
yeah, not really what you asked for but it is a great lib if you write a lot of CLI stuffs :D
Want results from more Discord servers?
Add your server