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

MorganKryze / ConsoleAppVisuals / 7993638304

pending completion
7993638304

push

github

MorganKryze
📖 add files and contributing recommendation

827 of 916 branches covered (90.28%)

Branch coverage included in aggregate %.

1676 of 1694 relevant lines covered (98.94%)

104.0 hits per line

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

90.91
/src/ConsoleAppVisuals/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
using System.Runtime.CompilerServices;
6

7
namespace ConsoleAppVisuals;
8

9
/// <summary>
10
/// Defines the banner of the console window.
11
/// </summary>
12
/// <remarks>
13
/// For more information, refer to the following resources:
14
/// <list type="bullet">
15
/// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
16
/// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
17
/// </list>
18
/// </remarks>
19
public class Banner : Element
20
{
21
    #region Fields
22
    private (string, string, string) _text;
23
    private int _upperMargin;
24
    private int _lowerMargin;
25
    private Placement _placement;
26
    private int _line;
27
    #endregion
28

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

35
    /// <summary>
36
    /// The line of the banner in the console.
37
    /// </summary>
38
    /// <remarks>We add 2 because so the banner does not overlap with the title.</remarks>
39
    public override int Line => _line;
6✔
40

41
    /// <summary>
42
    /// The height of the banner.
43
    /// </summary>
44
    public override int Height => UpperMargin + 1 + LowerMargin;
×
45

46
    /// <summary>
47
    /// The width of the banner.
48
    /// </summary>
49
    public override int Width => Console.WindowWidth;
×
50
    #endregion
51

52
    #region Getters and Setters
53
    /// <summary>
54
    /// Getter and setter of the text of the banner.
55
    /// </summary>
56
    public (string, string, string) Text
57
    {
58
        get => _text;
27✔
59
        set => _text = value;
×
60
    }
61

62
    /// <summary>
63
    /// Getter and setter of the upper margin of the banner.
64
    /// </summary>
65
    public int UpperMargin
66
    {
67
        get => _upperMargin;
12✔
68
        set => _upperMargin = value;
51✔
69
    }
70

71
    /// <summary>
72
    /// Getter and setter of the lower margin of the banner.
73
    /// </summary>
74
    public int LowerMargin
75
    {
76
        get => _lowerMargin;
12✔
77
        set => _lowerMargin = value;
51✔
78
    }
79
    #endregion
80

81

82
    #region Constructor
83
    /// <summary>
84
    /// The natural constructor of the banner.
85
    /// </summary>
86
    /// <param name="leftText">The text on the left of the banner.</param>
87
    /// <param name="centerText">The text in the center of the banner.</param>
88
    /// <param name="rightText">The text on the right of the banner.</param>
89
    /// <param name="upperMargin">The upper margin of the banner.</param>
90
    /// <param name="lowerMargin">The lower margin of the banner.</param>
91
    /// <param name="placement">The placement of the banner.</param>
92
    /// <param name="line">The line of the banner in the console.</param>
93
    /// <remarks>
94
    /// For more information, refer to the following resources:
95
    /// <list type="bullet">
96
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
97
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
98
    /// </list>
99
    /// </remarks>
100
    public Banner(
51✔
101
        string leftText = "Banner Left",
51✔
102
        string centerText = "Banner Center",
51✔
103
        string rightText = "Banner Right",
51✔
104
        int upperMargin = 0,
51✔
105
        int lowerMargin = 0,
51✔
106
        Placement placement = Placement.TopCenterFullWidth,
51✔
107
        int? line = null
51✔
108
    )
51✔
109
    {
110
        _text.Item1 = leftText;
51✔
111
        _text.Item2 = centerText;
51✔
112
        _text.Item3 = rightText;
51✔
113
        UpperMargin = upperMargin;
51✔
114
        LowerMargin = lowerMargin;
51✔
115
        _placement = CheckPlacement(placement);
51✔
116
        _line = Window.CheckLine(line) ?? Window.GetLineAvailable(_placement);
45!
117
    }
45✔
118

119
    private static Placement CheckPlacement(Placement placement)
120
    {
121
        if (placement is not (Placement.BottomCenterFullWidth or Placement.TopCenterFullWidth))
51✔
122
        {
123
            throw new ArgumentException(
6✔
124
                "The placement of the banner must be TopCenterFullWidth or BottomCenterFullWidth."
6✔
125
            );
6✔
126
        }
127
        return placement;
45✔
128
    }
129
    #endregion
130

131
    #region Methods
132
    /// <summary>
133
    /// This method is used to update the text on the left of the banner.
134
    /// </summary>
135
    /// <param name="leftText">The new text on the left of the banner.</param>
136
    /// <remarks>
137
    /// For more information, refer to the following resources:
138
    /// <list type="bullet">
139
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
140
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
141
    /// </list>
142
    /// </remarks>
143
    public void UpdateLeftText(string leftText)
144
    {
145
        _text.Item1 = leftText;
6✔
146
    }
6✔
147

148
    /// <summary>
149
    /// This method is used to update the text in the center of the banner.
150
    /// </summary>
151
    /// <param name="centerText">The new text in the center of the banner.</param>
152
    /// <remarks>
153
    /// For more information, refer to the following resources:
154
    /// <list type="bullet">
155
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
156
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
157
    /// </list>
158
    /// </remarks>
159
    public void UpdateCenterText(string centerText)
160
    {
161
        _text.Item2 = centerText;
6✔
162
    }
6✔
163

164
    /// <summary>
165
    /// This method is used to update the text on the right of the banner.
166
    /// </summary>
167
    /// <param name="rightText">The new text on the right of the banner.</param>
168
    /// <remarks>
169
    /// For more information, refer to the following resources:
170
    /// <list type="bullet">
171
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
172
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
173
    /// </list>
174
    /// </remarks>
175
    public void UpdateRightText(string rightText)
176
    {
177
        _text.Item3 = rightText;
6✔
178
    }
6✔
179

180
    /// <summary>
181
    /// This method is used to render the banner on the console.
182
    /// </summary>
183
    [Visual]
184
    protected override void RenderElementActions()
185
    {
186
        for (int i = 0; i < UpperMargin; i++)
187
        {
188
            Core.WritePositionedString(string.Empty, TextAlignment.Center, true, Line + i, false);
189
        }
190
        Core.WritePositionedString(Text.BannerToString(), TextAlignment.Center, true, Line, false);
191
        for (int i = 0; i < LowerMargin; i++)
192
        {
193
            Core.WritePositionedString(
194
                string.Empty,
195
                TextAlignment.Center,
196
                true,
197
                Line + Height - 1 - i,
198
                false
199
            );
200
        }
201
    }
202
    #endregion
203
}
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

© 2025 Coveralls, Inc