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

MorganKryze / ConsoleAppVisuals / 8192133574

07 Mar 2024 05:14PM UTC coverage: 88.799% (-1.1%) from 89.865%
8192133574

push

github

MorganKryze
🚑 (clear) fix method that clears the test console

954 of 1120 branches covered (85.18%)

Branch coverage included in aggregate %.

1797 of 1978 relevant lines covered (90.85%)

636.92 hits per line

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

84.31
/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; }
396✔
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_VISIBILITY;
486✔
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;
×
43

44
    /// <summary>
45
    /// Whether the element is executable or not.
46
    /// </summary>
47
    public virtual bool IsInteractive { get; } = false;
171✔
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
                        - elements
6✔
98
                            .Where(e =>
6✔
99
                                e.Placement == Placement.BottomCenterFullWidth && e.Visibility
12!
100
                            )
6✔
101
                            .Sum(e => e.Height),
6✔
102
                _ => throw new ArgumentOutOfRangeException(nameof(Placement), "Invalid placement.")
×
103
            };
99✔
104
        }
105
    }
106

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

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

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

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

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

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

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

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

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

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

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