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

MorganKryze / ConsoleAppVisuals / 8309517771

16 Mar 2024 06:01PM UTC coverage: 95.85% (+0.7%) from 95.189%
8309517771

push

github

MorganKryze
📖 PR template add tests section

911 of 1020 branches covered (89.31%)

Branch coverage included in aggregate %.

1907 of 1920 relevant lines covered (99.32%)

239.3 hits per line

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

96.43
/src/ConsoleAppVisuals/elements/interactive_elements/EmbedText.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.InteractiveElements;
6

7
/// <summary>
8
/// Defines the basic properties of an Embed text.
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 EmbedText : InteractiveElement<string>
18
{
19
    #region Fields
20
    private List<string> _text;
21
    private string? _button;
22
    private bool _roundedCorners;
23
    private TextAlignment _align;
24
    private Placement _placement;
25
    private List<string>? _textToDisplay;
26

27
    #endregion
28

29
    #region Properties
30
    /// <summary>
31
    /// The position of the Embed text.
32
    /// </summary>
33
    public override Placement Placement => _placement;
6✔
34

35
    /// <summary>
36
    /// The alignment of the Embed text.
37
    /// </summary>
38
    public override TextAlignment TextAlignment => _align;
2✔
39

40
    /// <summary>
41
    /// The height of the Embed text.
42
    /// </summary>
43
    public override int Height => _textToDisplay!.Count;
1✔
44

45
    /// <summary>
46
    /// The width of the Embed text.
47
    /// </summary>
48
    public override int Width => _textToDisplay!.Max((string s) => s.Length);
7✔
49

50
    /// <summary>
51
    /// The text of the Embed text.
52
    /// </summary>
53
    public List<string> Text => _text;
4✔
54

55
    /// <summary>
56
    /// The text of the button.
57
    /// </summary>
58
    public string? ButtonText => _button;
4✔
59

60
    /// <summary>
61
    /// The text to display.
62
    /// </summary>
63
    public List<string>? TextToDisplay => _textToDisplay;
8✔
64

65
    /// <summary>
66
    /// Wether the corners are rounded or not.
67
    /// </summary>
68
    public bool RoundedCorners => _roundedCorners;
4✔
69
    private string GetCorners => _roundedCorners ? "╭╮╰╯" : "┌┐└┘";
132✔
70

71
    #endregion
72

73
    #region Constructor
74
    /// <summary>
75
    /// The natural constructor of the Embed text.
76
    /// </summary>
77
    /// <param name="text">The text to display.</param>
78
    /// <param name="button">The text of the button. Null to not display a button.</param>
79
    /// <param name="align">The alignment of the Embed text.</param>
80
    /// <param name="placement">The placement of the Embed text element.</param>
81
    /// <param name="roundedCorners">Wether the corners are rounded or not.</param>
82
    /// <remarks>
83
    /// For more information, refer to the following resources:
84
    /// <list type="bullet">
85
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
86
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
87
    /// </list>
88
    /// </remarks>
89
    public EmbedText(
35✔
90
        List<string> text,
35✔
91
        string? button = null,
35✔
92
        TextAlignment align = TextAlignment.Left,
35✔
93
        Placement placement = Placement.TopCenter,
35✔
94
        bool roundedCorners = false
35✔
95
    )
35✔
96
    {
97
        _text = text;
35✔
98
        _button = button;
35✔
99
        _align = align;
35✔
100
        _placement = placement;
35✔
101
        _roundedCorners = roundedCorners;
35✔
102
        if (CheckIntegrity())
35✔
103
            BuildText();
33✔
104
    }
35✔
105
    #endregion
106

107
    #region Methods
108
    private bool CheckIntegrity()
109
    {
110
        if (_text.Count == 0)
68✔
111
        {
112
            return false;
1✔
113
        }
114
        if (_button is not null)
67✔
115
        {
116
            if (_text.Max((string s) => s.Length) < _button.Length)
197✔
117
            {
118
                return false;
1✔
119
            }
120
        }
121
        return true;
66✔
122
    }
123

124
    /// <summary>
125
    /// This method updates the text of the button.
126
    /// </summary>
127
    /// <param name="newButton">The new text of the button.</param>
128
    /// <remarks>
129
    /// For more information, refer to the following resources:
130
    /// <list type="bullet">
131
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
132
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
133
    /// </list>
134
    /// </remarks>
135
    public void UpdateButtonText(string? newButton)
136
    {
137
        _button = newButton;
2✔
138
    }
2✔
139

140
    /// <summary>
141
    /// This method updates the text of the Embed text.
142
    /// </summary>
143
    /// <param name="newText">The new text of the Embed text.</param>
144
    /// <remarks>
145
    /// For more information, refer to the following resources:
146
    /// <list type="bullet">
147
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
148
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
149
    /// </list>
150
    /// </remarks>
151
    public void UpdateText(List<string> newText)
152
    {
153
        _text.Clear();
1✔
154
        _text = newText;
1✔
155
    }
1✔
156

157
    /// <summary>
158
    /// This method updates the placement of the Embed text.
159
    /// </summary>
160
    /// <param name="newPlacement">The new placement of the Embed text.</param>
161
    /// <remarks>
162
    /// For more information, refer to the following resources:
163
    /// <list type="bullet">
164
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
165
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
166
    /// </list>
167
    /// </remarks>
168
    public void UpdatePlacement(Placement newPlacement)
169
    {
170
        _placement = newPlacement;
2✔
171
    }
2✔
172

173
    /// <summary>
174
    /// This method updates the alignment of the Embed text.
175
    /// </summary>
176
    /// <param name="newAlignment">The new alignment of the Embed text.</param>
177
    /// <remarks>
178
    /// For more information, refer to the following resources:
179
    /// <list type="bullet">
180
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
181
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
182
    /// </list>
183
    /// </remarks>
184
    public void UpdateTextAlignment(TextAlignment newAlignment)
185
    {
186
        _align = newAlignment;
2✔
187
    }
2✔
188

189
    /// <summary>
190
    /// This method updates the rounded corners of the Embed text.
191
    /// </summary>
192
    /// <param name="roundedCorners">Wether the corners are rounded or not.</param>
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
    public void SetRoundedCorners(bool roundedCorners = true)
201
    {
202
        _roundedCorners = roundedCorners;
2✔
203
    }
2✔
204

205
    /// <summary>
206
    /// Adds a line to the Embed text.
207
    /// </summary>
208
    /// <param name="line">The line to add.</param>
209
    /// <remarks>
210
    /// For more information, refer to the following resources:
211
    /// <list type="bullet">
212
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
213
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
214
    /// </list>
215
    /// </remarks>
216
    public void AddLine(string line)
217
    {
218
        _text.Add(line);
1✔
219
    }
1✔
220

221
    /// <summary>
222
    /// Inserts a line to the Embed text.
223
    /// </summary>
224
    /// <param name="line">The line to insert.</param>
225
    /// <param name="index">The index where to insert the line.</param>
226
    /// <remarks>
227
    /// For more information, refer to the following resources:
228
    /// <list type="bullet">
229
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
230
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
231
    /// </list>
232
    /// </remarks>
233
    public void InsertLine(string line, int index)
234
    {
235
        _text.Insert(index, line);
1✔
236
    }
1✔
237

238
    /// <summary>
239
    /// Removes a line from the Embed text.
240
    /// </summary>
241
    /// <param name="line">The line to remove.</param>
242
    /// <remarks>
243
    /// For more information, refer to the following resources:
244
    /// <list type="bullet">
245
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
246
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
247
    /// </list>
248
    /// </remarks>
249
    public void RemoveLine(string line)
250
    {
251
        if (!_text.Contains(line))
2✔
252
        {
253
            throw new ArgumentException("The line is not in the text.");
1✔
254
        }
255
        _text.Remove(line);
1✔
256
    }
1✔
257

258
    /// <summary>
259
    /// Removes a line from the Embed text.
260
    /// </summary>
261
    /// <param name="index">The index of the line to remove.</param>
262
    /// <remarks>
263
    /// For more information, refer to the following resources:
264
    /// <list type="bullet">
265
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
266
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
267
    /// </list>
268
    /// </remarks>
269
    public void RemoveLine(int index)
270
    {
271
        if (index < 0 || index >= _text.Count)
3✔
272
        {
273
            throw new ArgumentOutOfRangeException("The index is out of range.");
2✔
274
        }
275
        _text.RemoveAt(index);
1✔
276
    }
1✔
277

278
    /// <summary>
279
    /// Renders the Embed text.
280
    /// </summary>
281
    [Visual]
282
    protected override void RenderElementActions()
283
    {
284
        BuildText();
285
        Core.WriteMultiplePositionedLines(
286
            false,
287
            TextAlignment,
288
            Placement,
289
            false,
290
            Line,
291
            _textToDisplay!.ToArray()
292
        );
293
        Window.Freeze();
294
    }
295

296
    private void BuildText()
297
    {
298
        if (!CheckIntegrity())
33!
299
        {
300
            throw new FormatException(
×
301
                "Check that the text is not empty and that the button is not longer than the text."
×
302
            );
×
303
        }
304
        var maxLength = _text.Max((string s) => s.Length);
100✔
305
        _textToDisplay = new List<string>();
33✔
306
        foreach (var line in _text)
200✔
307
        {
308
            var lineToDisplay = "│ ";
67✔
309
            switch (_align)
67✔
310
            {
311
                case TextAlignment.Center:
312
                    int totalPadding = maxLength - line.Length;
2✔
313
                    int padLeft = totalPadding / 2;
2✔
314
                    lineToDisplay += line.PadLeft(line.Length + padLeft).PadRight(maxLength);
2✔
315
                    break;
2✔
316
                case TextAlignment.Left:
317
                    lineToDisplay += line.PadRight(maxLength);
63✔
318
                    break;
63✔
319
                case TextAlignment.Right:
320
                    lineToDisplay += line.PadLeft(maxLength);
2✔
321
                    break;
322
            }
323
            lineToDisplay += " │";
67✔
324
            _textToDisplay.Add(lineToDisplay);
67✔
325
        }
326
        _textToDisplay.Insert(0, GetCorners[0] + new string('─', maxLength + 2) + GetCorners[1]);
33✔
327
        if (_button is not null)
33✔
328
        {
329
            _textToDisplay.Add("│ " + new string(' ', maxLength) + " │");
32✔
330
            _textToDisplay.Add(
32✔
331
                "│ "
32✔
332
                    + "".PadRight(maxLength - _button.Length - 2)
32✔
333
                    + Core.NEGATIVE_ANCHOR
32✔
334
                    + " "
32✔
335
                    + _button
32✔
336
                    + " "
32✔
337
                    + Core.NEGATIVE_ANCHOR
32✔
338
                    + " │"
32✔
339
            );
32✔
340
        }
341
        _textToDisplay.Add(GetCorners[2] + new string('─', maxLength + 2) + GetCorners[3]);
33✔
342
    }
33✔
343
    #endregion
344
}
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