✅ 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#.Streamable
what happened to the spacing
108 Replies
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
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
okay, so you're not calculating the column correctly then
let's see that logic
It's line 83
Console.Write(isSelected ? $" \x1B[4m{optionText}\x1B[0m" : $" {optionText}");
I don't have line numbers
and the loop for that?
Ok it's in the if statement that I check for useHorizontal
more importantly, the logic for actually manipulating the cursor
Oh right
My bad
this bit?
Yeah
Specifically
Console.SetCursorPosition(startingColumn + (lastOptionLength + 2), startingRow + ((startingRow != 0) ? 3 : 2));
Copied the wrong thing
Idk how to calculate it correctlyprotip: 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
I got it working with some other code but it still had an issue of them not having the same spacing
so
Console.SetCursorPosition()
Well I didn't expect the method to get that long
Was initially like 50 lines
Mb tho
the row part seems fine, so
Yeah it's the column calculation
what's
useHorizontal
?Basically the method has 2 modes
One puts the options on separate rows
The other puts them on the same row
So horizontally
no, I mean
literally
when this runs
Uh
for the output that is wrong
what's the value?
Puts options on the same row instead of writeline on each option
True
how do you know?
Cause I set it to true?
😭
so, you're assuming
It also defaults to true
no it doesn't
Ok it defaults to false
My bad
if you KNEW everything about how this method was running, there'd be no issue
set a breakpoint and prove it
What do you mean by that
I mean
if you think the value of
useHorizontal
is true
set a breakpoint
and prove itI 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 falseyou don't know what a breakpoint is?
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
that's an assumption
no
it assumes the layout is working the way you expect/intend
set
a
breakpoint
this is useHorizontal being false
alternatively
you could set a breakpoint
Ok I don't think I understand how to set a breakpoint
Would u tell me what I need to do?
okay
what IDE are you using?
Visual Studio
2022
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
ahh
click that and it'll turn red, along with the whole line
that is a breakpoint
should I do that on the if statement?
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
ok this is helpful for debugging
indeed
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
this is what I get when I pause on this line
I know useHorizontal is truthy now..
lol
you JavaScript dev, you
no sir
I've only been learning some Lua and I recently decided to dabble in C# lol
really? where are you getting the term "truthy" from?
and or statements from Lua
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 meyeah, 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
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
logically, this seems fine to me
oh
lol
nevermind
the logic is flawed
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
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?
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
right
think that through
I got it to look like this with
Console.SetCursorPosition(startingColumn + (maxOptionLength + 4) * i, startingRow + ((startingRow != 0) ? 3 : 2));
that's wrong
how
okay
look at that formula you just posted
how is that different from what your current non-working version does?
well it uses the maxOptionLength which in this case is 9 and adds 4 then multiplies it by the iteration of the loop
right
that's what it does
vs. using lastOptionLength + 2
right
how are those different?
logically?
how should I answer that
maxOptionLength which in this case is 9 and adds 4 then multiplies it by the iteration of the loopdescribe this to me, in words what is this doing, functionally why is this the correct calculation
I actually wouldn't know since ChatGPT wrote that
and there's your problem
it still isn't correct cause there's varying amount of spaces though
it's not correct for what you're trying to do, sure
with dynamic column sizing
it IS correct for static column sizing
true
I rlly can't figure this out tho
I wouldn't have made this post otherwise
lol
if you were going to do this on pencil and paper
what do you need to know to write the first column
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
well, you need to know where to start, right?
uh
yea
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?
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
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
that should be the same thing
should it?
oh
I see it now
😉
ok well that's tricky
lol
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
damn
ok I need to get the length of the options before the third one
better option
running total
as in?
have a variable which is incremented by the option's length each iteration?
yes
more efficient than trying to loop over all the previous options, each time
true
cause you can't just multiply
cause the widths are variable
bro u a genius ngl
no
just experienced
nah
u fixed my issue
doesn't make me a genius
plus
you fixed your issue
I just guided you
held my hand the whole way tbh
I appreciate it
I guess I should close this now
that's what I'm here for
yeah, thank you
if I have any other issues imma open another post
please do
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.