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

MorganKryze / ConsoleAppVisuals / 8263352155

13 Mar 2024 10:45AM UTC coverage: 95.25% (+0.07%) from 95.179%
8263352155

push

github

MorganKryze
🌟 selector char managed locally in the element and not in the core class

987 of 1120 branches covered (88.13%)

Branch coverage included in aggregate %.

20 of 20 new or added lines in 3 files covered. (100.0%)

6 existing lines in 2 files now uncovered.

1981 of 1996 relevant lines covered (99.25%)

721.27 hits per line

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

90.72
/src/ConsoleAppVisuals/models/Element.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.Models;
6

7
/// <summary>
8
/// Defines the basic properties of an console element.
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 abstract class Element
18
{
19
    #region Properties
20
    /// <summary>
21
    /// The id number of the element.
22
    /// </summary>
23
    /// <remarks>This property is sealed. The ID of an element is automatically generated and managed by the <see cref="Window"/> class.</remarks>
24
    public int Id { get; set; }
408✔
25

26
    /// <summary>
27
    /// The visibility of the element.
28
    /// </summary>
29
    /// <remarks>This property is sealed. The visibility of an element is managed by the <see cref="ToggleVisibility"/> method.</remarks>
30
    public bool Visibility { get; private set; } = Window.DEFAULT_ELEMENT_VISIBILITY;
498✔
31

32
    /// <summary>
33
    /// The placement of the element.
34
    /// </summary>
35
    /// <remarks>This property is marked as virtual. It is recommended to override this property in derived classes to make it more specific.</remarks>
36
    public virtual Placement Placement { get; set; } = Placement.TopCenter;
87✔
37

38
    /// <summary>
39
    /// The text alignment of the text of the element.
40
    /// </summary>
41
    /// <remarks>This property is marked as virtual. It is recommended to override this property in derived classes to make it more specific.</remarks>
42
    public virtual TextAlignment TextAlignment { get; set; } = TextAlignment.Center;
6✔
43

44
    /// <summary>
45
    /// Whether the element is executable or not.
46
    /// </summary>
47
    public virtual bool IsInteractive { get; } = false;
180✔
48

49
    /// <summary>
50
    /// The line of the element in the console.
51
    /// </summary>
52
    /// <remarks>ATTENTION: This property is not marked as virtual. Override this property only to give it a constant value.</remarks>
53
    public virtual int Line
54
    {
55
        get
56
        {
57
            var elements = Window.GetRange(0, Id);
99✔
58
            return Placement switch
99!
59
            {
99✔
60
                Placement.TopCenterFullWidth
99✔
61
                    => elements
18✔
62
                        .Where(e => e.Placement == Placement.TopCenterFullWidth && e.Visibility)
9✔
63
                        .Sum(e => e.Height)
3✔
64
                        + elements
18✔
65
                            .Where(e => e.Placement == Placement.TopCenter && e.Visibility)
9✔
66
                            .Sum(e => e.Height)
3✔
67
                        + elements
18✔
68
                            .Where(e => e.Placement == Placement.TopLeft && e.Visibility)
9!
69
                            .Sum(e => e.Height)
×
70
                        + elements
18✔
71
                            .Where(e => e.Placement == Placement.TopRight && e.Visibility)
9!
72
                            .Sum(e => e.Height),
18✔
73
                Placement.TopCenter
99✔
74
                    => elements
48✔
75
                        .Where(e => e.Placement == Placement.TopCenterFullWidth && e.Visibility)
21✔
76
                        .Sum(e => e.Height)
6✔
77
                        + elements
48✔
78
                            .Where(e => e.Placement == Placement.TopCenter && e.Visibility)
21✔
79
                            .Sum(e => e.Height),
51✔
80
                Placement.TopLeft
99✔
81
                    => elements
21✔
82
                        .Where(e => e.Placement == Placement.TopCenterFullWidth && e.Visibility)
15✔
83
                        .Sum(e => e.Height)
6✔
84
                        + elements
21✔
85
                            .Where(e => e.Placement == Placement.TopLeft && e.Visibility)
15!
86
                            .Sum(e => e.Height),
21✔
87

99✔
88
                Placement.TopRight
99✔
89
                    => elements
6✔
90
                        .Where(e => e.Placement == Placement.TopCenterFullWidth && e.Visibility)
18✔
91
                        .Sum(e => e.Height)
6✔
92
                        + elements
6✔
93
                            .Where(e => e.Placement == Placement.TopRight && e.Visibility)
18!
94
                            .Sum(e => e.Height),
6✔
95
                Placement.BottomCenterFullWidth
99✔
96
                    => (Console.WindowHeight == 0 ? 0 : Console.WindowHeight - 1)
6!
97
                        - (this.Height - 1)
6✔
98
                        - elements
6✔
99
                            .Where(e =>
6✔
100
                                e.Placement == Placement.BottomCenterFullWidth && e.Visibility
12!
101
                            )
6✔
102
                            .Sum(e => e.Height),
6✔
UNCOV
103
                _ => throw new ArgumentOutOfRangeException(nameof(Placement), "Invalid placement.")
×
104
            };
99✔
105
        }
106
    }
107

108
    /// <summary>
109
    /// The height of the element.
110
    /// </summary>
111
    /// <remarks>This property is marked as virtual. It is recommended to override this property in derived classes to make it more specific.</remarks>
112
    public virtual int Height { get; } = 0;
9✔
113

114
    /// <summary>
115
    /// The width of the element.
116
    /// </summary>
117
    /// <remarks>This property is marked as virtual. It is recommended to override this property in derived classes to make it more specific.</remarks>
118
    public virtual int Width { get; } = 0;
3✔
119

120
    /// <summary>
121
    /// The maximum number of this element that can be drawn on the console.
122
    /// </summary>
123
    /// <remarks>This property is marked as virtual. It is recommended to override this property in derived classes to make it more specific.</remarks>
124
    public virtual int MaxNumberOfThisElement { get; } = 1;
1,539✔
125
    #endregion
126

127
    #region Methods
128
    /// <summary>
129
    /// This method is used to toggle the visibility of the element.This method is used to toggle the visibility of the element. If the maximum number of this element is reached, an exception is thrown.
130
    /// </summary>
131
    /// <exception cref="InvalidOperationException">Thrown when the maximum number of this element is reached.</exception>
132
    /// <remarks>This method is effectively sealed. The only way to change the visibility of an element is to use this method.</remarks>
133
    public void ToggleVisibility()
134
    {
135
        if (Visibility)
90✔
136
        {
137
            Visibility = false;
15✔
138
        }
139
        else if (Window.AllowVisibilityToggle(Id))
75✔
140
        {
141
            Visibility = true;
72✔
142
        }
143
        else
144
        {
145
            throw new InvalidOperationException(
3✔
146
                $"Operation not allowed, too many elements of {GetType()} already toggled from the maximum of {MaxNumberOfThisElement}. Consider turning off one element of this type."
3✔
147
            );
3✔
148
        }
149
    }
150

151
    /// <summary>
152
    /// This method is used to draw the element on the console.
153
    /// </summary>
154
    /// <remarks>
155
    /// For more information, refer to the following resources:
156
    /// <list type="bullet">
157
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
158
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
159
    /// </list>
160
    /// </remarks>
161
    [Visual]
162
    public void RenderElement()
163
    {
164
        if (Visibility)
165
        {
166
            RenderOptionsBeforeHand();
167
            RenderElementActions();
168
            RenderOptionsAfterHand();
169
        }
170
    }
171

172
    /// <summary>
173
    /// This method is used to define the actions to perform when the element is drawn on the console.
174
    /// </summary>
175
    /// <remarks>This method is marked as virtual. It is recommended to override this method in derived classes to make it more specific.</remarks>
176
    [Visual]
177
    protected virtual void RenderElementActions()
178
    {
179
        throw new NotImplementedException("Consider overriding this method in the derived class.");
180
    }
181

182
    /// <summary>
183
    /// This method is used to set options before drawing the element on the console.
184
    /// </summary>
185
    [Visual]
186
    protected virtual void RenderOptionsBeforeHand() { }
187

188
    /// <summary>
189
    /// This method is used to set options after drawing the element on the console.
190
    /// </summary>
191
    [Visual]
192
    protected virtual void RenderOptionsAfterHand() { }
193

194
    /// <summary>
195
    /// This method is used to draw the space taken by the element on the console.
196
    /// </summary>
197
    /// <param name="ignoreVisibility">Whether to ignore the visibility of the element or not.</param>
198
    /// <remarks>
199
    /// For more information, refer to the following resources:
200
    /// <list type="bullet">
201
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
202
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
203
    /// </list>
204
    /// </remarks>
205
    [Visual]
206
    public void RenderElementSpace(bool ignoreVisibility = false)
207
    {
208
        if (Visibility || ignoreVisibility)
209
        {
210
            Core.SaveColorPanel();
211
            Core.SetForegroundColor(Core.GetRandomColor());
212
            Core.WriteMultiplePositionedLines(
213
                false,
214
                Placement.ToTextAlignment(),
215
                true,
216
                Line,
217
                GetRenderSpace()
218
            );
219
            Core.LoadSavedColorPanel();
220
        }
221
    }
222

223
    /// <summary>
224
    /// This method is used to simulate the drawing space of the element on the console.
225
    /// </summary>
226
    /// <returns>The space taken by the element.</returns>
227
    /// <remarks>This method is marked as virtual. It is recommended to override this method in derived classes to make it more specific.</remarks>
228
    [Visual]
229
    protected virtual string[] GetRenderSpace()
230
    {
231
        var space = new string[Height];
232
        for (int i = 0; i < space.Length; i++)
233
        {
234
            space[i] = new string(' ', Width);
235
        }
236
        return space;
237
    }
238

239
    /// <summary>
240
    /// This method is used to clear the space taken by the element on the console.
241
    /// </summary>
242
    [Visual]
243
    public void Clear()
244
    {
245
        Core.ClearMultiplePositionedLines(Placement, Line, GetRenderSpace());
246
    }
247
    #endregion
248
}
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