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

MorganKryze / ConsoleAppVisuals / 8114215569

01 Mar 2024 04:29PM UTC coverage: 85.767% (-9.3%) from 95.093%
8114215569

push

github

MorganKryze
🤖 moved to publish

865 of 1072 branches covered (80.69%)

Branch coverage included in aggregate %.

1708 of 1928 relevant lines covered (88.59%)

273.5 hits per line

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

68.0
/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
        while (true)
165
        {
166
            DisplayChoices(_defaultIndex, _placement, _choices, lineChoice, delay);
167
            delay = false;
168

169
            switch (Console.ReadKey(intercept: true).Key)
170
            {
171
                case ConsoleKey.UpArrow:
172
                case ConsoleKey.Z:
173
                    _defaultIndex = (_defaultIndex == 0) ? _choices.Length - 1 : _defaultIndex - 1;
174
                    break;
175
                case ConsoleKey.DownArrow:
176
                case ConsoleKey.S:
177
                    _defaultIndex = (_defaultIndex == _choices.Length - 1) ? 0 : _defaultIndex + 1;
178
                    break;
179
                case ConsoleKey.Enter:
180
                    SendResponse(
181
                        this,
182
                        new InteractionEventArgs<int>(Output.Selected, _defaultIndex)
183
                    );
184
                    return;
185
                case ConsoleKey.Escape:
186
                    SendResponse(
187
                        this,
188
                        new InteractionEventArgs<int>(Output.Escaped, _defaultIndex)
189
                    );
190
                    return;
191
                case ConsoleKey.Backspace:
192
                    SendResponse(
193
                        this,
194
                        new InteractionEventArgs<int>(Output.Deleted, _defaultIndex)
195
                    );
196
                    return;
197
            }
198
        }
199
        [Visual]
200
        static void EqualizeChoicesLength(string[] choices)
201
        {
202
            int totalWidth = (choices.Length != 0) ? choices.Max((string s) => s.Length) : 0;
203
            for (int i = 0; i < choices.Length; i++)
204
            {
205
                choices[i] = choices[i].PadRight(totalWidth);
206
            }
207
        }
208
        [Visual]
209
        static void DisplayChoices(
210
            int defaultIndex,
211
            Placement placement,
212
            string[] choices,
213
            int lineChoice,
214
            bool delay = false
215
        )
216
        {
217
            string[] array = new string[choices.Length];
218
            for (int i = 0; i < choices.Length; i++)
219
            {
220
                array[i] =
221
                    (i == defaultIndex)
222
                        ? $" {Core.GetSelector.Item1} {choices[i]}  "
223
                        : $"   {choices[i]}  ";
224
                Core.WritePositionedString(
225
                    array[i],
226
                    placement.ToTextAlignment(),
227
                    i == defaultIndex,
228
                    lineChoice + i
229
                );
230
                if (delay)
231
                    Thread.Sleep(30);
232
            }
233
        }
234
    }
235
    #endregion
236
}
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

© 2026 Coveralls, Inc