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

MorganKryze / ConsoleAppVisuals / 8471182871

28 Mar 2024 05:20PM UTC coverage: 96.244%. Remained the same
8471182871

push

github

MorganKryze
🚑 (CD) fix regex to accept -beta.1 format

946 of 1056 branches covered (89.58%)

Branch coverage included in aggregate %.

2129 of 2139 relevant lines covered (99.53%)

249.89 hits per line

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

92.71
/src/ConsoleAppVisuals/models/Element.cs
1
/*
2
    Copyright (c) 2024 Yann M. Vidamment (MorganKryze)
3
    Licensed under GNU GPL v2.0. See full license at: https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/LICENSE.md
4
*/
5
namespace ConsoleAppVisuals.Models;
6

7
/// <summary>
8
/// The <see cref="Element"/> class is an abstract class that represents an element that can be rendered on the console.
9
/// </summary>
10
/// <remarks>
11
/// For more information, consider visiting the documentation available <a href="https://morgankryze.github.io/ConsoleAppVisuals/">here</a>.
12
/// </remarks>
13
public abstract class Element
14
{
15
    #region Constants
16
    /// <summary>
17
    /// The default visibility of the elements when they are added to the window.
18
    /// </summary>
19
    /// <remarks>
20
    /// This value should not be changed.
21
    /// Each time the user adds an element to the window, it will try to toggle the visibility of the element.
22
    /// </remarks>
23
    private const bool DEFAULT_VISIBILITY = false;
24

25
    private const int DEFAULT_HEIGHT = 0;
26

27
    private const int DEFAULT_WIDTH = 0;
28

29
    private const int DEFAULT_MAX_NUMBER_OF_THIS_ELEMENT = int.MaxValue;
30
    #endregion
31

32
    #region Sealed Properties
33
    /// <summary>
34
    /// Gets the id number of the element.
35
    /// </summary>
36
    /// <remarks>This property is sealed. The ID of an element is automatically generated and managed by the <see cref="Window"/> class.</remarks>
37
    public int Id { get; set; }
135✔
38

39
    /// <summary>
40
    /// Gets the visibility of the element.
41
    /// </summary>
42
    /// <remarks>This property is sealed. The visibility of an element is managed by the <see cref="ToggleVisibility"/> method.</remarks>
43
    public bool Visibility { get; private set; } = DEFAULT_VISIBILITY;
165✔
44
    #endregion
45

46
    #region Properties
47
    /// <summary>
48
    /// Gets the type of the element.
49
    /// </summary>
50
    [Visual]
51
    public virtual ElementType Type { get; }
52

53
    /// <summary>
54
    /// Gets the height of the element, the vertical number of lines taken in the console.
55
    /// </summary>
56
    /// <remarks>This property is marked as virtual. It is recommended to override this property in derived classes to make it more specific.</remarks>
57
    public virtual int Height { get; } = DEFAULT_HEIGHT;
5✔
58

59
    /// <summary>
60
    /// Gets the width of the element, the horizontal number of lines taken in the console.
61
    /// </summary>
62
    /// <remarks>This property is marked as virtual. It is recommended to override this property in derived classes to make it more specific.</remarks>
63
    public virtual int Width { get; } = DEFAULT_WIDTH;
1✔
64

65
    /// <summary>
66
    /// Gets the placement of the element int the console. See the <see cref="Placement"/> enum to know the possible values.
67
    /// </summary>
68
    /// <remarks>This property is marked as virtual. It is recommended to override this property in derived classes to make it more specific.</remarks>
69
    public virtual Placement Placement { get; set; }
29✔
70

71
    /// <summary>
72
    ///Gets the text alignment of the text of the element. See the <see cref="TextAlignment"/> enum to know the possible values.
73
    /// </summary>
74
    /// <remarks>This property is marked as virtual. It is recommended to override this property in derived classes to make it more specific.</remarks>
75
    public virtual TextAlignment TextAlignment { get; set; }
2✔
76

77
    /// <summary>
78
    /// Gets the maximum number of this element that can be drawn on the console.
79
    /// </summary>
80
    /// <remarks>This property is marked as virtual. It is recommended to override this property in derived classes to make it more specific.</remarks>
81
    public virtual int MaxNumberOfThisElement { get; } = DEFAULT_MAX_NUMBER_OF_THIS_ELEMENT;
545✔
82

83
    /// <summary>
84
    /// Gets a line to place the element in the console.
85
    /// </summary>
86
    /// <exception cref="ArgumentOutOfRangeException">Thrown when the placement of the element is invalid.</exception>
87
    /// <remarks>ATTENTION: This property is not marked as virtual. Override this property only to give it a constant value.</remarks>
88
    public virtual int Line
89
    {
90
        get
91
        {
92
            var elements = Window.Range(0, Id);
31✔
93
            return Placement switch
31✔
94
            {
31✔
95
                Placement.TopCenterFullWidth
31✔
96
                    => elements
6✔
97
                        .Where(e => e.Placement == Placement.TopCenterFullWidth && e.Visibility)
3✔
98
                        .Sum(e => e.Height)
1✔
99
                        + elements
6✔
100
                            .Where(e => e.Placement == Placement.TopCenter && e.Visibility)
3✔
101
                            .Sum(e => e.Height)
2✔
102
                        + elements
6✔
103
                            .Where(e => e.Placement == Placement.TopLeft && e.Visibility)
3!
104
                            .Sum(e => e.Height)
×
105
                        + elements
6✔
106
                            .Where(e => e.Placement == Placement.TopRight && e.Visibility)
3!
107
                            .Sum(e => e.Height),
6✔
108
                Placement.TopCenter
31✔
109
                    => elements
16✔
110
                        .Where(e => e.Placement == Placement.TopCenterFullWidth && e.Visibility)
7✔
111
                        .Sum(e => e.Height)
2✔
112
                        + elements
16✔
113
                            .Where(e => e.Placement == Placement.TopCenter && e.Visibility)
7✔
114
                            .Sum(e => e.Height),
18✔
115
                Placement.TopLeft
31✔
116
                    => elements
5✔
117
                        .Where(e => e.Placement == Placement.TopCenterFullWidth && e.Visibility)
5✔
118
                        .Sum(e => e.Height)
2✔
119
                        + elements
5✔
120
                            .Where(e => e.Placement == Placement.TopLeft && e.Visibility)
5!
121
                            .Sum(e => e.Height),
5✔
122

31✔
123
                Placement.TopRight
31✔
124
                    => elements
2✔
125
                        .Where(e => e.Placement == Placement.TopCenterFullWidth && e.Visibility)
6✔
126
                        .Sum(e => e.Height)
2✔
127
                        + elements
2✔
128
                            .Where(e => e.Placement == Placement.TopRight && e.Visibility)
6!
129
                            .Sum(e => e.Height),
2✔
130
                Placement.BottomCenterFullWidth
31✔
131
                    => (Console.WindowHeight == 0 ? 0 : Console.WindowHeight - 1)
1!
132
                        - (Height - 1)
1✔
133
                        - elements
1✔
134
                            .Where(e =>
1✔
135
                                e.Placement == Placement.BottomCenterFullWidth && e.Visibility
4!
136
                            )
1✔
137
                            .Sum(e => e.Height),
1✔
138
                _ => throw new ArgumentOutOfRangeException(nameof(Placement), "Invalid placement.")
1✔
139
            };
31✔
140
        }
141
    }
142
    #endregion
143

144
    #region Methods
145
    /// <summary>
146
    /// Toggles the visibility of the element. If the maximum number of this element is reached, an exception is thrown.
147
    /// </summary>
148
    /// <exception cref="InvalidOperationException">Thrown when the maximum number of this element is reached.</exception>
149
    /// <remarks>This method is effectively sealed. The only way to change the visibility of an element is to use this method.</remarks>
150
    public void ToggleVisibility()
151
    {
152
        if (Visibility)
31✔
153
        {
154
            Visibility = false;
5✔
155
        }
156
        else if (Window.IsElementActivatable(Id))
26✔
157
        {
158
            Visibility = true;
25✔
159
        }
160
        else
161
        {
162
            throw new InvalidOperationException(
1✔
163
                $"Operation not allowed, too many elements of {GetType()} already toggled from the maximum of {MaxNumberOfThisElement}. Consider turning off one element of this type."
1✔
164
            );
1✔
165
        }
166
    }
167
    #endregion
168

169
    #region Rendering
170
    /// <summary>
171
    /// Renders the element on the console.
172
    /// </summary>
173
    /// <remarks>
174
    /// For more information, consider visiting the documentation available <a href="https://morgankryze.github.io/ConsoleAppVisuals/">here</a>.
175
    /// </remarks>
176
    [Visual]
177
    public void RenderElement()
178
    {
179
        if (Visibility)
180
        {
181
            RenderOptionsBeforeHand();
182
            RenderElementActions();
183
            RenderOptionsAfterHand();
184
        }
185
    }
186

187
    /// <summary>
188
    /// Defines the actions to perform when the element is called to be rendered on the console.
189
    /// </summary>
190
    /// <remarks>This method is marked as virtual. It is recommended to override this method in derived classes to make it more specific.</remarks>
191
    [Visual]
192
    protected virtual void RenderElementActions()
193
    {
194
        throw new NotImplementedException("Consider overriding this method in the derived class.");
195
    }
196

197
    /// <summary>
198
    /// Defines actions to perform before rendering the element on the console.
199
    /// </summary>
200
    [Visual]
201
    protected virtual void RenderOptionsBeforeHand() { }
202

203
    /// <summary>
204
    /// Defines actions to perform after rendering the element on the console.
205
    /// </summary>
206
    [Visual]
207
    protected virtual void RenderOptionsAfterHand() { }
208

209
    /// <summary>
210
    /// Renders the space taken by the element on the console.
211
    /// </summary>
212
    /// <param name="ignoreVisibility">Whether to ignore the visibility of the element or not.</param>
213
    /// <remarks>
214
    /// For more information, consider visiting the documentation available <a href="https://morgankryze.github.io/ConsoleAppVisuals/">here</a>.
215
    /// </remarks>
216
    [Visual]
217
    public void RenderElementSpace(bool ignoreVisibility = false)
218
    {
219
        if (Visibility || ignoreVisibility)
220
        {
221
            Core.SaveColorPanel();
222
            Core.SetForegroundColor(Core.GetRandomColor());
223
            Core.WriteMultiplePositionedLines(
224
                false,
225
                TextAlignment.Center,
226
                Placement,
227
                true,
228
                Line,
229
                GetRenderSpace()
230
            );
231
            Core.LoadSavedColorPanel();
232
        }
233
    }
234

235
    /// <summary>
236
    /// Gets the space taken by the element on the console.
237
    /// </summary>
238
    /// <returns>The space taken by the element.</returns>
239
    /// <remarks>This method is marked as virtual. It is recommended to override this method in derived classes to make it more specific.</remarks>
240
    [Visual]
241
    protected virtual string[] GetRenderSpace()
242
    {
243
        var space = new string[Height];
244
        for (int i = 0; i < space.Length; i++)
245
        {
246
            space[i] = new string(' ', Width);
247
        }
248
        return space;
249
    }
250

251
    /// <summary>
252
    /// Clears the space taken by the element on the console.
253
    /// </summary>
254
    /// <remarks>
255
    /// For more information, consider visiting the documentation available <a href="https://morgankryze.github.io/ConsoleAppVisuals/">here</a>.
256
    /// </remarks>
257
    [Visual]
258
    public void Clear()
259
    {
260
        Core.ClearMultiplePositionedLines(Placement, Line, GetRenderSpace());
261
    }
262
    #endregion
263
}
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