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

MorganKryze / ConsoleAppVisuals / 7473998763

10 Jan 2024 11:06AM UTC coverage: 37.669% (+1.7%) from 35.959%
7473998763

push

github

MorganKryze
🤖 (ci and docs) remove obsolete attribute from coverage

365 of 1034 branches covered (0.0%)

Branch coverage included in aggregate %.

750 of 1926 relevant lines covered (38.94%)

157.53 hits per line

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

0.0
/src/ConsoleAppVisuals/elements/interactive/ScrollingMenu.cs
1
/*
2
    MIT License 2023 MorganKryze
3
    For full license information, please visit: https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/LICENSE
4
*/
5
namespace ConsoleAppVisuals;
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/Program.cs">Example Project</a></description></item>
15
/// </list>
16
/// </remarks>
17
public class ScrollingMenu : InteractiveElement<int>
18
{
19
    #region Fields
20
    private readonly string _question;
21
    private readonly string[] _choices;
22
    private int _defaultIndex;
23
    private readonly Placement _placement;
24
    private readonly int _line;
25
    #endregion
26

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

33
    /// <summary>
34
    /// The line where the menu will be displayed.
35
    /// </summary>
36
    public override int Line => _line;
×
37

38
    /// <summary>
39
    /// The height of the menu.
40
    /// </summary>
41
    public override int Height => _choices.Length + 2;
×
42

43
    /// <summary>
44
    /// The width of the menu.
45
    /// </summary>
46
    public override int Width =>
47
        Math.Max(_question.Length + 1, _choices.Max((string s) => s.Length) + 4);
×
48
    #endregion
49

50
    #region Constructor
51
    /// <summary>
52
    /// The constructor of the ScrollingMenu class.
53
    /// </summary>
54
    /// <param name="question">The question to ask the user.</param>
55
    /// <param name="defaultIndex">The index of the default choice(initially 0).</param>
56
    /// <param name="placement">The placement of the menu on the console.</param>
57
    /// <param name="line">The line where the menu will be displayed.</param>
58
    /// <param name="choices">The different choices of the menu.</param>
59
    /// <remarks>
60
    /// For more information, refer to the following resources:
61
    /// <list type="bullet">
62
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
63
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
64
    /// </list>
65
    /// </remarks>
66
    public ScrollingMenu(
×
67
        string question,
×
68
        int defaultIndex = 0,
×
69
        Placement placement = Placement.TopCenter,
×
70
        int? line = null,
×
71
        params string[] choices
×
72
    )
×
73
    {
74
        _question = question;
×
75
        _defaultIndex = defaultIndex;
×
76
        _placement = placement;
×
77
        _line = Window.CheckLine(line) ?? Window.GetLineAvailable(_placement);
×
78
        _choices = choices;
×
79
    }
×
80
    #endregion
81

82
    #region Methods
83
    /// <summary>
84
    /// This method is used to draw the menu on the console.
85
    /// </summary>
86
    protected override void RenderElementActions()
87
    {
88
        EqualizeChoicesLength(_choices);
×
89
        Core.WriteContinuousString(_question, Line, false, 1500, 50);
×
90
        int lineChoice = Line + 2;
×
91
        bool delay = true;
×
92
        while (true)
93
        {
94
            DisplayChoices(_defaultIndex, _placement, _choices, lineChoice, delay);
×
95
            delay = false;
×
96

97
            switch (Console.ReadKey(intercept: true).Key)
×
98
            {
99
                case ConsoleKey.UpArrow:
100
                case ConsoleKey.Z:
101
                    _defaultIndex = (_defaultIndex == 0) ? _choices.Length - 1 : _defaultIndex - 1;
×
102
                    break;
×
103
                case ConsoleKey.DownArrow:
104
                case ConsoleKey.S:
105
                    _defaultIndex = (_defaultIndex == _choices.Length - 1) ? 0 : _defaultIndex + 1;
×
106
                    break;
×
107
                case ConsoleKey.Enter:
108
                    SendResponse(this, new InteractionEventArgs<int>(Output.Select, _defaultIndex));
×
109
                    return;
×
110
                case ConsoleKey.Escape:
111
                    SendResponse(this, new InteractionEventArgs<int>(Output.Exit, _defaultIndex));
×
112
                    return;
×
113
                case ConsoleKey.Backspace:
114
                    SendResponse(this, new InteractionEventArgs<int>(Output.Delete, _defaultIndex));
×
115
                    return;
×
116
            }
117
        }
118

119
        static void EqualizeChoicesLength(string[] choices)
120
        {
121
            int totalWidth = (choices.Length != 0) ? choices.Max((string s) => s.Length) : 0;
×
122
            for (int i = 0; i < choices.Length; i++)
×
123
            {
124
                choices[i] = choices[i].PadRight(totalWidth);
×
125
            }
126
        }
×
127
        static void DisplayChoices(
128
            int defaultIndex,
129
            Placement placement,
130
            string[] choices,
131
            int lineChoice,
132
            bool delay = false
133
        )
134
        {
135
            string[] array = new string[choices.Length];
×
136
            for (int i = 0; i < choices.Length; i++)
×
137
            {
138
                array[i] =
×
139
                    (i == defaultIndex)
×
140
                        ? $" {Core.GetSelector.Item1} {choices[i]}  "
×
141
                        : $"   {choices[i]}  ";
×
142
                Core.WritePositionedString(array[i], placement.ToTextAlignment(), i == defaultIndex, lineChoice + i);
×
143
                if (delay)
×
144
                    Thread.Sleep(30);
×
145
            }
146
        }
×
147
    }
148
    #endregion
149
}
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