• 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/IntSelector.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 number selector of 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 IntSelector : InteractiveElement<int>
18
{
19
    #region Fields
20
    private readonly string _question;
21
    private readonly int _minimumValue;
22
    private readonly int _maximumValue;
23
    private readonly int _startValue;
24
    private readonly int _step;
25
    private readonly bool _roundedCorners;
26
    private readonly Placement _placement;
27
    private readonly int _line;
28
    #endregion
29

30
    #region Properties
31
    /// <summary>
32
    /// The placement of the selector on the console.
33
    /// </summary>
34
    public override Placement Placement => _placement;
×
35

36
    /// <summary>
37
    /// The line where the selector will be displayed.
38
    /// </summary>
39
    public override int Line => _line;
×
40

41
    /// <summary>
42
    /// The height of the selector.
43
    /// </summary>
44
    public override int Height => 7;
×
45

46
    /// <summary>
47
    /// The width of the selector.
48
    /// </summary>
49
    public override int Width =>
50
        Math.Max(
×
51
            _question.Length,
×
52
            $" {Core.GetSelector.Item1} {BuildNumber(_maximumValue)} {Core.GetSelector.Item2} ".Length
×
53
        );
×
54
    #endregion
55

56
    #region Constructor
57
    /// <summary>
58
    /// The constructor of the intSelector class.
59
    /// </summary>
60
    /// <param name="question">The question to ask the user.</param>
61
    /// <param name="min">The minimum value of the selector.</param>
62
    /// <param name="max">The maximum value of the selector.</param>
63
    /// <param name="start">The start value of the selector.</param>
64
    /// <param name="step">The step of the selector.</param>
65
    /// <param name="placement">The placement of the selector on the console.</param>
66
    /// <param name="line">The line where the selector will be displayed.</param>
67
    /// <param name="roundedCorners">Whether the corners of the selector are rounded.</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/Program.cs">Example Project</a></description></item>
73
    /// </list>
74
    /// </remarks>
75
    public IntSelector(
×
76
        string question,
×
77
        int min,
×
78
        int max,
×
79
        int start = 0,
×
80
        int step = 100,
×
81
        Placement placement = Placement.TopCenter,
×
82
        int? line = null,
×
83
        bool roundedCorners = false
×
84
    )
×
85
    {
86
        _question = question;
×
87
        _minimumValue = CheckMin(min, max);
×
88
        _maximumValue = CheckMax(min, max);
×
89
        _startValue = CheckStart(start, _minimumValue, _maximumValue);
×
90
        _step = CheckStep(step, _minimumValue, _maximumValue);
×
91
        _placement = placement;
×
92
        _line = Window.CheckLine(line) ?? Window.GetLineAvailable(_placement);
×
93
        _roundedCorners = roundedCorners;
×
94
    }
×
95

96
    private static int CheckMin(int min, int max)
97
    {
98
        if (min > max)
×
99
            throw new ArgumentException(
×
100
                "The minimum value cannot be greater than the maximum value."
×
101
            );
×
102
        return min;
×
103
    }
104

105
    private static int CheckMax(int min, int max)
106
    {
107
        if (max < min)
×
108
            throw new ArgumentException("The maximum value cannot be less than the minimum value.");
×
109
        return max;
×
110
    }
111

112
    private static int CheckStart(int start, int min, int max)
113
    {
114
        if (start < min)
×
115
            throw new ArgumentException("The start value cannot be less than the minimum value.");
×
116
        if (start > max)
×
117
            throw new ArgumentException(
×
118
                "The start value cannot be greater than the maximum value."
×
119
            );
×
120
        return start;
×
121
    }
122

123
    private static int CheckStep(int step, int min, int max)
124
    {
125
        if (step > max - min)
×
126
            throw new ArgumentException(
×
127
                "The step cannot be greater than the difference between the minimum and maximum values."
×
128
            );
×
129
        return step;
×
130
    }
131
    #endregion
132

133
    #region Methods
134
    /// <summary>
135
    /// This method is used to draw the selector on the console.
136
    /// </summary>
137
    protected override void RenderElementActions()
138
    {
139
        Core.WriteContinuousString(_question, _line, default, 1500, 50);
×
140
        int currentNumber = _startValue;
×
141
        int lineSelector = _line + 4;
×
142
        while (true)
×
143
        {
144
            DisplayChoices(lineSelector, currentNumber);
×
145

146
            switch (Console.ReadKey(true).Key)
×
147
            {
148
                case ConsoleKey.UpArrow:
149
                case ConsoleKey.Z:
150
                    currentNumber = NextNumber(Direction.Up, currentNumber);
×
151
                    break;
×
152
                case ConsoleKey.DownArrow:
153
                case ConsoleKey.S:
154
                    currentNumber = NextNumber(Direction.Down, currentNumber);
×
155
                    break;
×
156
                case ConsoleKey.Enter:
157
                    SendResponse(this, new InteractionEventArgs<int>(Output.Select, currentNumber));
×
158
                    return;
×
159
                case ConsoleKey.Escape:
160
                    SendResponse(this, new InteractionEventArgs<int>(Output.Exit, currentNumber));
×
161
                    return;
×
162
                default:
163
                    break;
164
            }
165
            Thread.Sleep(1);
×
166
        }
167
    }
168

169
    void DisplayChoices(int lineSelector, int currentNumber)
170
    {
171
        Core.WritePositionedString(
×
172
            BuildLine(Direction.Up),
×
173
            TextAlignment.Center,
×
174
            false,
×
175
            lineSelector - 2
×
176
        );
×
177
        Core.WritePositionedString(
×
178
            BuildNumber(NextNumber(Direction.Up, currentNumber)),
×
179
            TextAlignment.Center,
×
180
            false,
×
181
            lineSelector - 1
×
182
        );
×
183
        Core.WritePositionedString(
×
184
            $" {Core.GetSelector.Item1} {BuildNumber(currentNumber)} {Core.GetSelector.Item2} ",
×
185
            TextAlignment.Center,
×
186
            true,
×
187
            lineSelector
×
188
        );
×
189
        Core.WritePositionedString(
×
190
            BuildNumber(NextNumber(Direction.Down, currentNumber)),
×
191
            TextAlignment.Center,
×
192
            false,
×
193
            lineSelector + 1
×
194
        );
×
195
        Core.WritePositionedString(
×
196
            BuildLine(Direction.Down),
×
197
            TextAlignment.Center,
×
198
            false,
×
199
            lineSelector + 2
×
200
        );
×
201
    }
×
202

203
    int NextNumber(Direction direction, int currentNumber)
204
    {
205
        if (direction == Direction.Up)
×
206
        {
207
            if (currentNumber + _step <= _maximumValue)
×
208
                return currentNumber + _step;
×
209
            else if (currentNumber + _step > _maximumValue)
×
210
                return _minimumValue;
×
211
        }
212
        else
213
        {
214
            if (currentNumber - _step >= _minimumValue)
×
215
                return currentNumber - _step;
×
216
            else if (currentNumber - _step < _minimumValue)
×
217
                return _maximumValue;
×
218
        }
219
        return currentNumber;
×
220
    }
221

222
    string BuildLine(Direction direction)
223
    {
224
        string corners = _roundedCorners ? "╭╮╰╯" : "┌┐└┘";
×
225
        StringBuilder line = new();
×
226
        for (int i = 0; i < _maximumValue.ToString().Length + 2; i++)
×
227
            line.Append('─');
×
228
        if (direction == Direction.Up)
×
229
            line.Insert(0, corners[0].ToString(), 1).Append(corners[1], 1);
×
230
        else
231
            line.Insert(0, corners[2].ToString(), 1).Append(corners[3], 1);
×
232
        return line.ToString();
×
233
    }
234

235
    string BuildNumber(int number)
236
    {
237
        StringBuilder numberStr = new();
×
238
        numberStr.Append("│ ");
×
239
        numberStr.Append(
×
240
            number.ToString().ResizeString(_maximumValue.ToString().Length, TextAlignment.Center)
×
241
        );
×
242
        numberStr.Append(" │");
×
243
        return numberStr.ToString();
×
244
    }
245
    #endregion
246
}
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