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

MorganKryze / ConsoleAppVisuals / 8158191062

05 Mar 2024 02:47PM UTC coverage: 86.165% (+0.9%) from 85.231%
8158191062

push

github

MorganKryze
📖 (readme) update roadmap

931 of 1144 branches covered (81.38%)

Branch coverage included in aggregate %.

1803 of 2029 relevant lines covered (88.86%)

412.64 hits per line

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

60.0
/src/ConsoleAppVisuals/elements/static_elements/Banner.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.Elements;
6

7
/// <summary>
8
/// Defines the banner 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/">Example Project</a></description></item>
15
/// </list>
16
/// </remarks>
17
public class Banner : Element
18
{
19
    #region Fields
20
    private (string, string, string) _text;
21
    private int _upperMargin;
22
    private int _lowerMargin;
23
    private Placement _placement;
24
    #endregion
25

26
    #region Properties
27
    /// <summary>
28
    /// The placement of the banner.
29
    /// </summary>
30
    public override Placement Placement => _placement;
12✔
31

32
    /// <summary>
33
    /// The height of the banner.
34
    /// </summary>
35
    public override int Height => UpperMargin + 1 + LowerMargin;
4✔
36

37
    /// <summary>
38
    /// The width of the banner.
39
    /// </summary>
40
    public override int Width => Console.WindowWidth;
4✔
41

42
    /// <summary>
43
    /// The text of the banner.
44
    /// </summary>
45
    public (string, string, string) Text => _text;
18✔
46

47
    /// <summary>
48
    /// The upper margin of the banner.
49
    /// </summary>
50
    public int UpperMargin => _upperMargin;
12✔
51

52
    /// <summary>
53
    /// The lower margin of the banner.
54
    /// </summary>
55
    public int LowerMargin => _lowerMargin;
12✔
56
    #endregion
57

58

59
    #region Constructor
60
    /// <summary>
61
    /// The natural constructor of the banner.
62
    /// </summary>
63
    /// <param name="leftText">The text on the left of the banner.</param>
64
    /// <param name="centerText">The text in the center of the banner.</param>
65
    /// <param name="rightText">The text on the right of the banner.</param>
66
    /// <param name="upperMargin">The upper margin of the banner.</param>
67
    /// <param name="lowerMargin">The lower margin of the banner.</param>
68
    /// <param name="placement">The placement of the banner.</param>
69
    /// <remarks>
70
    /// For more information, refer to the following resources:
71
    /// <list type="bullet">
72
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
73
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
74
    /// </list>
75
    /// </remarks>
76
    public Banner(
38✔
77
        string leftText = "Banner Left",
38✔
78
        string centerText = "Banner Center",
38✔
79
        string rightText = "Banner Right",
38✔
80
        int upperMargin = 0,
38✔
81
        int lowerMargin = 0,
38✔
82
        Placement placement = Placement.TopCenterFullWidth
38✔
83
    )
38✔
84
    {
85
        _text.Item1 = leftText;
38✔
86
        _text.Item2 = centerText;
38✔
87
        _text.Item3 = rightText;
38✔
88
        _upperMargin = upperMargin;
38✔
89
        _lowerMargin = lowerMargin;
38✔
90
        _placement = CheckPlacement(placement);
38✔
91
    }
34✔
92

93
    private static Placement CheckPlacement(Placement placement)
94
    {
95
        if (placement is not (Placement.BottomCenterFullWidth or Placement.TopCenterFullWidth))
38✔
96
        {
97
            throw new ArgumentException(
4✔
98
                "The placement of the banner must be TopCenterFullWidth or BottomCenterFullWidth."
4✔
99
            );
4✔
100
        }
101
        return placement;
34✔
102
    }
103
    #endregion
104

105
    #region Methods
106
    /// <summary>
107
    /// This method is used to update the text on the left of the banner.
108
    /// </summary>
109
    /// <param name="leftText">The new text on the left of the banner.</param>
110
    /// <remarks>
111
    /// For more information, refer to the following resources:
112
    /// <list type="bullet">
113
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
114
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
115
    /// </list>
116
    /// </remarks>
117
    public void UpdateLeftText(string leftText)
118
    {
119
        _text.Item1 = leftText;
4✔
120
    }
4✔
121

122
    /// <summary>
123
    /// This method is used to update the text in the center of the banner.
124
    /// </summary>
125
    /// <param name="centerText">The new text in the center of the banner.</param>
126
    /// <remarks>
127
    /// For more information, refer to the following resources:
128
    /// <list type="bullet">
129
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
130
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
131
    /// </list>
132
    /// </remarks>
133
    public void UpdateCenterText(string centerText)
134
    {
135
        _text.Item2 = centerText;
4✔
136
    }
4✔
137

138
    /// <summary>
139
    /// This method is used to update the text on the right of the banner.
140
    /// </summary>
141
    /// <param name="rightText">The new text on the right of the banner.</param>
142
    /// <remarks>
143
    /// For more information, refer to the following resources:
144
    /// <list type="bullet">
145
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
146
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
147
    /// </list>
148
    /// </remarks>
149
    public void UpdateRightText(string rightText)
150
    {
151
        _text.Item3 = rightText;
4✔
152
    }
4✔
153

154
    /// <summary>
155
    /// This method is used to update the placement of the banner.
156
    /// </summary>
157
    /// <param name="placement">The new placement of the banner.</param>
158
    /// <remarks>
159
    /// For more information, refer to the following resources:
160
    /// <list type="bullet">
161
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
162
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
163
    /// </list>
164
    /// </remarks>
165
    public void UpdatePlacement(Placement placement)
166
    {
167
        _placement = CheckPlacement(placement);
×
168
    }
×
169

170
    /// <summary>
171
    /// This method is used to update the upper margin of the banner.
172
    /// </summary>
173
    /// <param name="upperMargin">The new upper margin of the banner.</param>
174
    /// <exception cref="ArgumentOutOfRangeException">The upper margin of the banner must be between 0 and the height of the console window.</exception>
175
    /// <remarks>
176
    /// For more information, refer to the following resources:
177
    /// <list type="bullet">
178
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
179
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
180
    /// </list>
181
    /// </remarks>
182
    public void UpdateUpperMargin(int upperMargin)
183
    {
184
        if (upperMargin < 0 || upperMargin > Console.WindowHeight - 1)
×
185
        {
186
            throw new ArgumentOutOfRangeException(
×
187
                nameof(upperMargin),
×
188
                "The upper margin of the banner must be between 0 and the height of the console window."
×
189
            );
×
190
        }
191
        _upperMargin = upperMargin;
×
192
    }
×
193

194
    /// <summary>
195
    /// This method is used to update the lower margin of the banner.
196
    /// </summary>
197
    /// <param name="lowerMargin">The new lower margin of the banner.</param>
198
    /// <exception cref="ArgumentOutOfRangeException">The lower margin of the banner must be between 0 and the height of the console window.</exception>
199
    /// <remarks>
200
    /// For more information, refer to the following resources:
201
    /// <list type="bullet">
202
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
203
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
204
    /// </list>
205
    /// </remarks>
206
    public void UpdateLowerMargin(int lowerMargin)
207
    {
208
        if (lowerMargin < 0 || lowerMargin > Console.WindowHeight - 1)
×
209
        {
210
            throw new ArgumentOutOfRangeException(
×
211
                nameof(lowerMargin),
×
212
                "The lower margin of the banner must be between 0 and the height of the console window."
×
213
            );
×
214
        }
215
        _lowerMargin = lowerMargin;
×
216
    }
×
217

218
    /// <summary>
219
    /// This method is used to render the banner on the console.
220
    /// </summary>
221
    [Visual]
222
    protected override void RenderElementActions()
223
    {
224
        for (int i = 0; i < UpperMargin; i++)
225
        {
226
            Core.WritePositionedString(string.Empty, TextAlignment.Center, true, Line + i, false);
227
        }
228
        Core.WritePositionedString(Text.BannerToString(), TextAlignment.Center, true, Line, false);
229
        for (int i = 0; i < LowerMargin; i++)
230
        {
231
            Core.WritePositionedString(
232
                string.Empty,
233
                TextAlignment.Center,
234
                true,
235
                Line + Height - 1 - i,
236
                false
237
            );
238
        }
239
    }
240
    #endregion
241
}
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