• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

MorganKryze / ConsoleAppVisuals / 8119668611

02 Mar 2024 02:13AM UTC coverage: 85.188% (-9.9%) from 95.086%
8119668611

push

github

MorganKryze
🤖 update action to node 20

866 of 1080 branches covered (80.19%)

Branch coverage included in aggregate %.

1699 of 1931 relevant lines covered (87.99%)

272.46 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

65.38
/src/ConsoleAppVisuals/elements/interactive_elements/ScrollingMenu.cs
1
/*
2
    GNU GPL License 2024 MorganKryze(Yann Vidamment)
3
    For full license information, please visit: https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/LICENSE
4
*/
5
namespace ConsoleAppVisuals.Elements;
6

7
/// <summary>
8
/// Defines the scrolling menu the console window.
9
/// </summary>
10
/// <remarks>
11
/// For more information, refer to the following resources:
12
/// <list type="bullet">
13
/// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
14
/// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
15
/// </list>
16
/// </remarks>
17
public class ScrollingMenu : InteractiveElement<int>
18
{
19
    #region Fields
20
    private string _question;
21
    private string[] _choices;
22
    private int _defaultIndex;
23
    private Placement _placement;
24
    #endregion
25

26
    #region Properties
27
    /// <summary>
28
    /// The placement of the menu on the console.
29
    /// </summary>
30
    public override Placement Placement => _placement;
9✔
31

32
    /// <summary>
33
    /// The height of the menu.
34
    /// </summary>
35
    public override int Height => _choices.Length + 2;
3✔
36

37
    /// <summary>
38
    /// The width of the menu.
39
    /// </summary>
40
    public override int Width =>
41
        Math.Max(_question.Length + 1, _choices.Max((string s) => s.Length) + 4);
12✔
42

43
    /// <summary>
44
    /// The question to ask the user.
45
    /// </summary>
46
    public string Question => _question;
3✔
47

48
    /// <summary>
49
    /// The different choices of the menu.
50
    /// </summary>
51
    public string[] Choices => _choices;
3✔
52

53
    /// <summary>
54
    /// The index of the default choice(initially 0).
55
    /// </summary>
56
    public int DefaultIndex => _defaultIndex;
3✔
57

58
    #endregion
59

60
    #region Constructor
61
    /// <summary>
62
    /// The constructor of the ScrollingMenu class.
63
    /// </summary>
64
    /// <param name="question">The question to ask the user.</param>
65
    /// <param name="defaultIndex">The index of the default choice(initially 0).</param>
66
    /// <param name="placement">The placement of the menu on the console.</param>
67
    /// <param name="choices">The different choices of the menu.</param>
68
    /// <remarks>
69
    /// For more information, refer to the following resources:
70
    /// <list type="bullet">
71
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
72
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
73
    /// </list>
74
    /// </remarks>
75
    public ScrollingMenu(
24✔
76
        string question,
24✔
77
        int defaultIndex = 0,
24✔
78
        Placement placement = Placement.TopCenter,
24✔
79
        params string[] choices
24✔
80
    )
24✔
81
    {
82
        _question = question;
24✔
83
        _defaultIndex = defaultIndex;
24✔
84
        _placement = placement;
24✔
85
        _choices = choices;
24✔
86
    }
24✔
87
    #endregion
88

89
    #region Methods
90
    /// <summary>
91
    /// This method is used to update the question of the menu.
92
    /// </summary>
93
    /// <param name="question">The new question of the menu.</param>
94
    /// <remarks>
95
    /// For more information, refer to the following resources:
96
    /// <list type="bullet">
97
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
98
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
99
    /// </list>
100
    /// </remarks>
101
    public void UpdateQuestion(string question)
102
    {
103
        _question = question;
×
104
    }
×
105

106
    /// <summary>
107
    /// This method is used to update the choices of the menu.
108
    /// </summary>
109
    /// <param name="choices">The new choices of the menu.</param>
110
    /// <remarks>
111
    /// For more information, refer to the following resources:
112
    /// <list type="bullet">
113
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
114
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
115
    /// </list>
116
    /// </remarks>
117
    public void UpdateChoices(params string[] choices)
118
    {
119
        _choices = choices;
×
120
    }
×
121

122
    /// <summary>
123
    /// This method is used to update the default index of the menu.
124
    /// </summary>
125
    /// <param name="defaultIndex">The new default index of the menu.</param>
126
    /// <remarks>
127
    /// For more information, refer to the following resources:
128
    /// <list type="bullet">
129
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
130
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
131
    /// </list>
132
    /// </remarks>
133
    public void UpdateDefaultIndex(int defaultIndex)
134
    {
135
        _defaultIndex = defaultIndex;
×
136
    }
×
137

138
    /// <summary>
139
    /// This method is used to update the placement of the menu.
140
    /// </summary>
141
    /// <param name="placement">The new placement of the menu.</param>
142
    /// <remarks>
143
    /// For more information, refer to the following resources:
144
    /// <list type="bullet">
145
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
146
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
147
    /// </list>
148
    /// </remarks>
149
    public void UpdatePlacement(Placement placement)
150
    {
151
        _placement = placement;
×
152
    }
×
153

154
    /// <summary>
155
    /// This method is used to draw the menu on the console.
156
    /// </summary>
157
    [Visual]
158
    protected override void RenderElementActions()
159
    {
160
        EqualizeChoicesLength(_choices);
161
        Core.WriteContinuousString(_question, Line, false, 1500, 50);
162
        int lineChoice = Line + 2;
163
        bool delay = true;
164
        bool loop = true;
165
        while (loop)
166
        {
167
            DisplayChoices(_defaultIndex, _placement, _choices, lineChoice, delay);
168
            delay = false;
169

170
            switch (Console.ReadKey(intercept: true).Key)
171
            {
172
                case ConsoleKey.UpArrow:
173
                case ConsoleKey.Z:
174
                    _defaultIndex = (_defaultIndex == 0) ? _choices.Length - 1 : _defaultIndex - 1;
175
                    break;
176
                case ConsoleKey.DownArrow:
177
                case ConsoleKey.S:
178
                    _defaultIndex = (_defaultIndex == _choices.Length - 1) ? 0 : _defaultIndex + 1;
179
                    break;
180
                case ConsoleKey.Enter:
181
                    SendResponse(
182
                        this,
183
                        new InteractionEventArgs<int>(Output.Selected, _defaultIndex)
184
                    );
185
                    loop = false;
186
                    break;
187
                case ConsoleKey.Escape:
188
                    SendResponse(
189
                        this,
190
                        new InteractionEventArgs<int>(Output.Escaped, _defaultIndex)
191
                    );
192
                    loop = false;
193
                    break;
194
                case ConsoleKey.Backspace:
195
                    SendResponse(
196
                        this,
197
                        new InteractionEventArgs<int>(Output.Deleted, _defaultIndex)
198
                    );
199
                    loop = false;
200
                    break;
201
            }
202
        }
203
    }
204

205
    [Visual]
206
    private static void EqualizeChoicesLength(string[] choices)
207
    {
208
        int totalWidth = (choices.Length != 0) ? choices.Max((string s) => s.Length) : 0;
×
209
        for (int i = 0; i < choices.Length; i++)
210
        {
211
            choices[i] = choices[i].PadRight(totalWidth);
212
        }
213
    }
214

215
    [Visual]
216
    private static void DisplayChoices(
217
        int defaultIndex,
218
        Placement placement,
219
        string[] choices,
220
        int lineChoice,
221
        bool delay = false
222
    )
223
    {
224
        string[] array = new string[choices.Length];
225
        for (int i = 0; i < choices.Length; i++)
226
        {
227
            array[i] =
228
                (i == defaultIndex)
229
                    ? $" {Core.GetSelector.Item1} {choices[i]}  "
230
                    : $"   {choices[i]}  ";
231
            Core.WritePositionedString(
232
                array[i],
233
                placement.ToTextAlignment(),
234
                i == defaultIndex,
235
                lineChoice + i
236
            );
237
            if (delay)
238
                Thread.Sleep(30);
239
        }
240
    }
241
    #endregion
242
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc