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

MorganKryze / ConsoleAppVisuals / 8119668611

02 Mar 2024 02:13AM UTC coverage: 85.188% (-9.9%) from 95.086%
8119668611

push

github

MorganKryze
🤖 update action to node 20

866 of 1080 branches covered (80.19%)

Branch coverage included in aggregate %.

1699 of 1931 relevant lines covered (87.99%)

272.46 hits per line

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

59.0
/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; }
318✔
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;
303✔
31

32
    /// <summary>
33
    /// The placement of the element.
34
    /// </summary>
35
    public virtual Placement Placement { get; set; } = Placement.TopCenter;
21✔
36

37
    /// <summary>
38
    /// The text alignment of the text of the element.
39
    /// </summary>
40
    public virtual TextAlignment TextAlignment { get; set; } = TextAlignment.Center;
9✔
41

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

47
    /// <summary>
48
    /// The line of the element in the console.
49
    /// </summary>
50
    public virtual int Line
51
    {
52
        get
53
        {
54
            var elements = Window.GetRange(0, Id);
120✔
55
            return Placement switch
120!
56
            {
120✔
57
                Placement.TopCenterFullWidth
120✔
58
                    => elements
15✔
59
                        .Where(e => e.Placement == Placement.TopCenterFullWidth && e.Visibility)
×
60
                        .Sum(e => e.Height)
×
61
                        + elements
15✔
62
                            .Where(e => e.Placement == Placement.TopCenter && e.Visibility)
×
63
                            .Sum(e => e.Height)
×
64
                        + elements
15✔
65
                            .Where(e => e.Placement == Placement.TopLeft && e.Visibility)
×
66
                            .Sum(e => e.Height)
×
67
                        + elements
15✔
68
                            .Where(e => e.Placement == Placement.TopRight && e.Visibility)
×
69
                            .Sum(e => e.Height),
15✔
70
                Placement.TopCenter
120✔
71
                    => elements
81✔
72
                        .Where(e => e.Placement == Placement.TopCenterFullWidth && e.Visibility)
×
73
                        .Sum(e => e.Height)
×
74
                        + elements
81✔
75
                            .Where(e => e.Placement == Placement.TopCenter && e.Visibility)
×
76
                            .Sum(e => e.Height),
81✔
77
                Placement.TopLeft
120✔
78
                    => elements
18✔
79
                        .Where(e => e.Placement == Placement.TopCenterFullWidth && e.Visibility)
×
80
                        .Sum(e => e.Height)
×
81
                        + elements
18✔
82
                            .Where(e => e.Placement == Placement.TopLeft && e.Visibility)
×
83
                            .Sum(e => e.Height),
18✔
84

120✔
85
                Placement.TopRight
120✔
86
                    => elements
3✔
87
                        .Where(e => e.Placement == Placement.TopCenterFullWidth && e.Visibility)
×
88
                        .Sum(e => e.Height)
×
89
                        + elements
3✔
90
                            .Where(e => e.Placement == Placement.TopRight && e.Visibility)
×
91
                            .Sum(e => e.Height),
3✔
92
                Placement.BottomCenterFullWidth
120✔
93
                    => Console.WindowHeight
3✔
94
                        - elements
3✔
95
                            .Where(e =>
3✔
96
                                e.Placement == Placement.BottomCenterFullWidth && e.Visibility
×
97
                            )
3✔
98
                            .Sum(e => e.Height),
3✔
99
                _ => throw new ArgumentOutOfRangeException(nameof(Placement), "Invalid placement.")
×
100
            };
120✔
101
        }
102
    }
103

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

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

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

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

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

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

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

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

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

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

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