C
C#2y ago
Danaew

✅ ConsoleApp method spacing issue

Hey, I've been working on this PromptChoice method to make a new useHorizontal parameter which would essentially place all options on the same row and I've kind of got it working, except for the spacing — I cannot for the life of me figure that part out. Here's a video of what my issue looks like: https://streamable.com/l6ix1s I've attached the code to this post. Also if there are any areas where I can improve the code, please let me know, as I'm still pretty new to C#.
108 Replies
JakenVeina
JakenVeina2y ago
so, you're doing direct I/O with the console buffer? or you're doing cursor movement magic? and all your menu options are getting overwritten onto the same line? I mean seems pretty straightforward to me you're not properly advancing the cursor position to a new line for each item you're drawing
Danaew
DanaewOP2y ago
I'm trying to keep them in the same line, it is pretty simple but I cannot get the spacing correctly for some reason though
JakenVeina
JakenVeina2y ago
okay, so you're not calculating the column correctly then let's see that logic
Danaew
DanaewOP2y ago
It's line 83 Console.Write(isSelected ? $" \x1B[4m{optionText}\x1B[0m" : $" {optionText}");
JakenVeina
JakenVeina2y ago
I don't have line numbers and the loop for that?
Danaew
DanaewOP2y ago
Ok it's in the if statement that I check for useHorizontal
JakenVeina
JakenVeina2y ago
more importantly, the logic for actually manipulating the cursor
Danaew
DanaewOP2y ago
Oh right My bad
JakenVeina
JakenVeina2y ago
for (int i = 0; i < optionCount; i++)
{
string optionText = options[i];
int lastOptionLength = i == 0 ? 1 : options[i - 1].Length;
bool isSelected = selectedOption == (i + 1);
string padding = isSelected ? "> " : " ";

if (useHorizontal)
Console.SetCursorPosition(startingColumn + (lastOptionLength + 2), startingRow + ((startingRow != 0) ? 3 : 2)) ;
else
Console.SetCursorPosition(startingColumn, startingRow + i + ((startingRow != 0) ? 3 : 2));

Console.ForegroundColor = isSelected ? highlightColor : Console.ForegroundColor;

if (useHorizontal)
Console.Write(isSelected ? $" \x1B[4m{optionText}\x1B[0m" : $" {optionText}");
else
Console.Write(padding + (useDynamicPadding ? optionText.PadRight(maxOptionLength) + " " : optionText));

Console.ResetColor();
}
for (int i = 0; i < optionCount; i++)
{
string optionText = options[i];
int lastOptionLength = i == 0 ? 1 : options[i - 1].Length;
bool isSelected = selectedOption == (i + 1);
string padding = isSelected ? "> " : " ";

if (useHorizontal)
Console.SetCursorPosition(startingColumn + (lastOptionLength + 2), startingRow + ((startingRow != 0) ? 3 : 2)) ;
else
Console.SetCursorPosition(startingColumn, startingRow + i + ((startingRow != 0) ? 3 : 2));

Console.ForegroundColor = isSelected ? highlightColor : Console.ForegroundColor;

if (useHorizontal)
Console.Write(isSelected ? $" \x1B[4m{optionText}\x1B[0m" : $" {optionText}");
else
Console.Write(padding + (useDynamicPadding ? optionText.PadRight(maxOptionLength) + " " : optionText));

Console.ResetColor();
}
this bit?
Danaew
DanaewOP2y ago
Yeah Specifically Console.SetCursorPosition(startingColumn + (lastOptionLength + 2), startingRow + ((startingRow != 0) ? 3 : 2)); Copied the wrong thing Idk how to calculate it correctly
JakenVeina
JakenVeina2y ago
protip: don't make someone have to seek out the relevant code, that's just gonna make them less likely to care enough to help. If you can't identify a small portion of code relevant to the issue, you probably haven't thought through the issue enough for yourself
Danaew
DanaewOP2y ago
I got it working with some other code but it still had an issue of them not having the same spacing
JakenVeina
JakenVeina2y ago
so Console.SetCursorPosition()
Danaew
DanaewOP2y ago
Well I didn't expect the method to get that long Was initially like 50 lines Mb tho
JakenVeina
JakenVeina2y ago
the row part seems fine, so
Danaew
DanaewOP2y ago
Yeah it's the column calculation
JakenVeina
JakenVeina2y ago
what's useHorizontal?
Danaew
DanaewOP2y ago
Basically the method has 2 modes One puts the options on separate rows The other puts them on the same row So horizontally
JakenVeina
JakenVeina2y ago
no, I mean literally when this runs
Danaew
DanaewOP2y ago
Uh
JakenVeina
JakenVeina2y ago
for the output that is wrong what's the value?
Danaew
DanaewOP2y ago
Puts options on the same row instead of writeline on each option True
JakenVeina
JakenVeina2y ago
how do you know?
Danaew
DanaewOP2y ago
Cause I set it to true? 😭
JakenVeina
JakenVeina2y ago
so, you're assuming
Danaew
DanaewOP2y ago
It also defaults to true
JakenVeina
JakenVeina2y ago
no it doesn't
Danaew
DanaewOP2y ago
Ok it defaults to false My bad
JakenVeina
JakenVeina2y ago
if you KNEW everything about how this method was running, there'd be no issue set a breakpoint and prove it
Danaew
DanaewOP2y ago
What do you mean by that
JakenVeina
JakenVeina2y ago
I mean if you think the value of useHorizontal is true set a breakpoint and prove it
Danaew
DanaewOP2y ago
I don't get the breakpoint but I call it with useHorizontal true int selectedOption = await PromptChoice(options, highlightColor: ConsoleColor.Blue, useHorizontal: true); otherwise it defaults to false
JakenVeina
JakenVeina2y ago
you don't know what a breakpoint is?
Danaew
DanaewOP2y ago
well I thought u meant something else but if you're being literal then I would know it's true by the layout being different
JakenVeina
JakenVeina2y ago
that's an assumption
Danaew
DanaewOP2y ago
no
JakenVeina
JakenVeina2y ago
it assumes the layout is working the way you expect/intend set a breakpoint
Danaew
DanaewOP2y ago
this is useHorizontal being false
JakenVeina
JakenVeina2y ago
alternatively you could set a breakpoint
Danaew
DanaewOP2y ago
Ok I don't think I understand how to set a breakpoint Would u tell me what I need to do?
JakenVeina
JakenVeina2y ago
okay what IDE are you using?
Danaew
DanaewOP2y ago
Visual Studio 2022
JakenVeina
JakenVeina2y ago
the easiest way to set a breakpoint is by clicking in the gutter on the left-hand side of a source file there will be a little grey circle that pops up under your mouse
Danaew
DanaewOP2y ago
ahh
JakenVeina
JakenVeina2y ago
click that and it'll turn red, along with the whole line that is a breakpoint
Danaew
DanaewOP2y ago
should I do that on the if statement?
JakenVeina
JakenVeina2y ago
that is where Visual Studio will pause execution of your program, when that line of code is reached do it on any line where you want to inspect what's going on in the code generally, where you want to inspect variable values when the application is paused on a breakpoint (or exception) Visual Studio lets you examine the values of any variables or object fields in scope you can also begin stepping line-by-line through the code, as it executes with the buttons up at the top
Danaew
DanaewOP2y ago
ok this is helpful for debugging
JakenVeina
JakenVeina2y ago
indeed
Danaew
DanaewOP2y ago
but I already know the issue lies in how I format the options unless I'm doing something else incorrectly which I don't think I am
Danaew
DanaewOP2y ago
this is what I get when I pause on this line
Danaew
DanaewOP2y ago
I know useHorizontal is truthy now..
JakenVeina
JakenVeina2y ago
lol you JavaScript dev, you
Danaew
DanaewOP2y ago
no sir I've only been learning some Lua and I recently decided to dabble in C# lol
JakenVeina
JakenVeina2y ago
really? where are you getting the term "truthy" from?
Danaew
DanaewOP2y ago
and or statements from Lua
JakenVeina
JakenVeina2y ago
does Lua do "truthiness"? it's been a long time, I suppose it probably does for the record, C# doesn't values are true or false and any values that aren't true or false aren't bool and can't be directly related to bool so, at a glance, everything looks fine to me
Danaew
DanaewOP2y ago
yeah, it's all fine expect for that one line which is supposed to format the options I really don't know what went wrong, I tried several things
JakenVeina
JakenVeina2y ago
at this point, you are empowered with the debugger take those calculations and break them apart and start walking through them, step by step you should be able to spot the issue well I can at least say that I get the exact same result
for(var i = 0; i < options.Count; ++i)
{
var lastOptionLength = (i == 0) ? 1 : options[i - 1].Length;
var isSelected = (selectedOptionIndex == i);

Console.SetCursorPosition(startingColumn + (lastOptionLength + 2), startingRow);
if (isSelected)
Console.ForegroundColor = ConsoleColor.White;
Console.Write(isSelected ? $" \x1B[4m{options[i]}\x1B[0m" : $" {options[i]}");
Console.ResetColor();
}
for(var i = 0; i < options.Count; ++i)
{
var lastOptionLength = (i == 0) ? 1 : options[i - 1].Length;
var isSelected = (selectedOptionIndex == i);

Console.SetCursorPosition(startingColumn + (lastOptionLength + 2), startingRow);
if (isSelected)
Console.ForegroundColor = ConsoleColor.White;
Console.Write(isSelected ? $" \x1B[4m{options[i]}\x1B[0m" : $" {options[i]}");
Console.ResetColor();
}
logically, this seems fine to me oh lol nevermind the logic is flawed
Danaew
DanaewOP2y ago
ok honestly I've tinkered with it for a few days I even asked ChatGPT I still don't know how to fix it so
JakenVeina
JakenVeina2y ago
loool yeah if anything, ChatGPT would be likely to make things worse just think it through for yourself how do you figure out what column a particular option should go on?
Danaew
DanaewOP2y ago
usually I'd calculate the length of the last option and add a space or 2 for the next option doesn't work here though
JakenVeina
JakenVeina2y ago
right think that through
Danaew
DanaewOP2y ago
I got it to look like this with Console.SetCursorPosition(startingColumn + (maxOptionLength + 4) * i, startingRow + ((startingRow != 0) ? 3 : 2));
JakenVeina
JakenVeina2y ago
that's wrong
Danaew
DanaewOP2y ago
how
JakenVeina
JakenVeina2y ago
okay look at that formula you just posted how is that different from what your current non-working version does?
Danaew
DanaewOP2y ago
well it uses the maxOptionLength which in this case is 9 and adds 4 then multiplies it by the iteration of the loop
JakenVeina
JakenVeina2y ago
right that's what it does
Danaew
DanaewOP2y ago
vs. using lastOptionLength + 2
JakenVeina
JakenVeina2y ago
right how are those different? logically?
Danaew
DanaewOP2y ago
how should I answer that
JakenVeina
JakenVeina2y ago
maxOptionLength which in this case is 9 and adds 4 then multiplies it by the iteration of the loop
describe this to me, in words what is this doing, functionally why is this the correct calculation
Danaew
DanaewOP2y ago
I actually wouldn't know since ChatGPT wrote that
JakenVeina
JakenVeina2y ago
and there's your problem
Danaew
DanaewOP2y ago
it still isn't correct cause there's varying amount of spaces though
JakenVeina
JakenVeina2y ago
it's not correct for what you're trying to do, sure with dynamic column sizing it IS correct for static column sizing
Danaew
DanaewOP2y ago
true I rlly can't figure this out tho I wouldn't have made this post otherwise lol
JakenVeina
JakenVeina2y ago
if you were going to do this on pencil and paper what do you need to know to write the first column
Danaew
DanaewOP2y ago
nothing much I guess just add a space and write the option there's nothing to watch out for since it's the first one
JakenVeina
JakenVeina2y ago
well, you need to know where to start, right?
Danaew
DanaewOP2y ago
uh yea
JakenVeina
JakenVeina2y ago
that's a piece of info you need to know the option that you want to write that's a piece of info second column what do you need to know?
Danaew
DanaewOP2y ago
the way I'm thinking of it, I need the length of the first option and then I should simply setcursorposition 2 spaces forward and write it
JakenVeina
JakenVeina2y ago
and that works for the second option you need to know where the first option ended which is where it started, plus its length now third column
Danaew
DanaewOP2y ago
that should be the same thing
JakenVeina
JakenVeina2y ago
should it?
Danaew
DanaewOP2y ago
oh I see it now
JakenVeina
JakenVeina2y ago
😉
Danaew
DanaewOP2y ago
ok well that's tricky lol
JakenVeina
JakenVeina2y ago
the ChatGPT version incorporates this by MULTIPLYING the width of each column, by the number it's calculating the length of ALL previous columns, not just one you can do that, or you can just keep a running total
Danaew
DanaewOP2y ago
damn ok I need to get the length of the options before the third one
JakenVeina
JakenVeina2y ago
better option running total
Danaew
DanaewOP2y ago
as in?
JakenVeina
JakenVeina2y ago
var currentColumn = startingColumn;
for(var i = 0; i < options.Count; ++i)
{
var isSelected = (selectedOptionIndex == i);

Console.SetCursorPosition(
left: currentColumn,
top: startingRow);
currentColumn += options[i].Length + 2;

if (isSelected)
Console.ForegroundColor = ConsoleColor.White;
Console.Write(isSelected ? $" \x1B[4m{options[i]}\x1B[0m" : $" {options[i]}");
Console.ResetColor();
}
var currentColumn = startingColumn;
for(var i = 0; i < options.Count; ++i)
{
var isSelected = (selectedOptionIndex == i);

Console.SetCursorPosition(
left: currentColumn,
top: startingRow);
currentColumn += options[i].Length + 2;

if (isSelected)
Console.ForegroundColor = ConsoleColor.White;
Console.Write(isSelected ? $" \x1B[4m{options[i]}\x1B[0m" : $" {options[i]}");
Console.ResetColor();
}
Danaew
DanaewOP2y ago
have a variable which is incremented by the option's length each iteration?
JakenVeina
JakenVeina2y ago
yes more efficient than trying to loop over all the previous options, each time
Danaew
DanaewOP2y ago
true
JakenVeina
JakenVeina2y ago
cause you can't just multiply cause the widths are variable
Danaew
DanaewOP2y ago
bro u a genius ngl
JakenVeina
JakenVeina2y ago
no just experienced
Danaew
DanaewOP2y ago
nah u fixed my issue
JakenVeina
JakenVeina2y ago
doesn't make me a genius plus you fixed your issue I just guided you
Danaew
DanaewOP2y ago
held my hand the whole way tbh I appreciate it I guess I should close this now
JakenVeina
JakenVeina2y ago
that's what I'm here for
Danaew
DanaewOP2y ago
yeah, thank you if I have any other issues imma open another post
JakenVeina
JakenVeina2y ago
please do
Accord
Accord14mo 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