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

MorganKryze / ConsoleAppVisuals / 7941534291

17 Feb 2024 12:10PM UTC coverage: 68.805% (+5.5%) from 63.31%
7941534291

push

github

MorganKryze
🚑 fix issue with width of loading bars

606 of 970 branches covered (62.47%)

Branch coverage included in aggregate %.

4 of 6 new or added lines in 2 files covered. (66.67%)

38 existing lines in 4 files now uncovered.

1282 of 1774 relevant lines covered (72.27%)

267.55 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
    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;
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
    [Visual]
87
    protected override void RenderElementActions()
88
    {
89
        EqualizeChoicesLength(_choices);
90
        Core.WriteContinuousString(_question, Line, false, 1500, 50);
91
        int lineChoice = Line + 2;
92
        bool delay = true;
93
        while (true)
94
        {
95
            DisplayChoices(_defaultIndex, _placement, _choices, lineChoice, delay);
96
            delay = false;
97

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

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