• 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

82.24
/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.Elements;
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 readonly 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;
8✔
34

35
    /// <summary>
36
    /// The height of the Embed text.
37
    /// </summary>
38
    public override int Height => _textToDisplay!.Count;
2✔
39

40
    /// <summary>
41
    /// The width of the Embed text.
42
    /// </summary>
43
    public override int Width => _textToDisplay!.Max((string s) => s.Length) - 8;
14✔
44

45
    /// <summary>
46
    /// The text of the Embed text.
47
    /// </summary>
48
    public List<string> Text => _text;
6✔
49

50
    /// <summary>
51
    /// The text of the button.
52
    /// </summary>
53
    public string ButtonText => _button;
4✔
54

55
    /// <summary>
56
    /// The text to display.
57
    /// </summary>
58
    public List<string>? TextToDisplay => _textToDisplay;
14✔
59

60
    /// <summary>
61
    /// Wether the corners are rounded or not.
62
    /// </summary>
63
    public bool RoundedCorners => _roundedCorners;
×
64
    private string GetCorners => _roundedCorners ? "╭╮╰╯" : "┌┐└┘";
176!
65

66
    #endregion
67

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

102
    #region Methods
103
    private bool CheckIntegrity()
104
    {
105
        if (_text.Count == 0)
90✔
106
        {
107
            return false;
2✔
108
        }
109

110
        if (_text.Max((string s) => s.Length) < _button.Length)
268!
111
        {
112
            return false;
×
113
        }
114
        return true;
88✔
115
    }
116

117
    /// <summary>
118
    /// This method updates the text of the button.
119
    /// </summary>
120
    /// <param name="newButton">The new text of the button.</param>
121
    /// <remarks>
122
    /// For more information, refer to the following resources:
123
    /// <list type="bullet">
124
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
125
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
126
    /// </list>
127
    /// </remarks>
128
    public void UpdateButtonText(string newButton)
129
    {
130
        _button = newButton;
×
131
    }
×
132

133
    /// <summary>
134
    /// This method updates the text of the Embed text.
135
    /// </summary>
136
    /// <param name="newText">The new text of the Embed text.</param>
137
    /// <remarks>
138
    /// For more information, refer to the following resources:
139
    /// <list type="bullet">
140
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
141
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
142
    /// </list>
143
    /// </remarks>
144
    public void UpdateText(List<string> newText)
145
    {
146
        _text.Clear();
×
147
        _text.AddRange(newText);
×
148
    }
×
149

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

166
    /// <summary>
167
    /// This method updates the alignment of the Embed text.
168
    /// </summary>
169
    /// <param name="newAlignment">The new alignment of the Embed text.</param>
170
    /// <remarks>
171
    /// For more information, refer to the following resources:
172
    /// <list type="bullet">
173
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
174
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
175
    /// </list>
176
    /// </remarks>
177
    public void UpdateAlignment(TextAlignment newAlignment)
178
    {
179
        _align = newAlignment;
×
180
    }
×
181

182
    /// <summary>
183
    /// This method updates the rounded corners of the Embed text.
184
    /// </summary>
185
    /// <param name="roundedCorners">Wether the corners are rounded or not.</param>
186
    /// <remarks>
187
    /// For more information, refer to the following resources:
188
    /// <list type="bullet">
189
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
190
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
191
    /// </list>
192
    /// </remarks>
193
    public void SetRoundedCorners(bool roundedCorners = true)
194
    {
195
        _roundedCorners = roundedCorners;
×
196
    }
×
197

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

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

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

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

271
    /// <summary>
272
    /// Renders the Embed text.
273
    /// </summary>
274
    [Visual]
275
    protected override void RenderElementActions()
276
    {
277
        BuildText();
278
        Core.WriteMultiplePositionedLines(
279
            false,
280
            _placement.ToTextAlignment(),
281
            false,
282
            Line,
283
            _textToDisplay!.ToArray()
284
        );
285
        Window.Freeze();
286
    }
287

288
    private void BuildText()
289
    {
290
        if (!CheckIntegrity())
44!
291
        {
292
            throw new FormatException(
×
293
                "Check that the text is not empty and that the button is not longer than the text."
×
294
            );
×
295
        }
296
        var maxLength = _text.Max((string s) => s.Length);
134✔
297
        _textToDisplay = new List<string>();
44✔
298
        foreach (var line in _text)
268✔
299
        {
300
            var lineToDisplay = "│ ";
90✔
301
            switch (_align)
90✔
302
            {
303
                case TextAlignment.Center:
304
                    int totalPadding = maxLength - line.Length;
4✔
305
                    int padLeft = totalPadding / 2;
4✔
306
                    lineToDisplay += line.PadLeft(line.Length + padLeft).PadRight(maxLength);
4✔
307
                    break;
4✔
308
                case TextAlignment.Left:
309
                    lineToDisplay += line.PadRight(maxLength);
82✔
310
                    break;
82✔
311
                case TextAlignment.Right:
312
                    lineToDisplay += line.PadLeft(maxLength);
4✔
313
                    break;
314
            }
315
            lineToDisplay += " │";
90✔
316
            _textToDisplay.Add(lineToDisplay);
90✔
317
        }
318
        _textToDisplay.Insert(0, GetCorners[0] + new string('─', maxLength + 2) + GetCorners[1]);
44✔
319
        _textToDisplay.Add("│ " + new string(' ', maxLength) + " │");
44✔
320
        _textToDisplay.Add(
44✔
321
            "│ "
44✔
322
                + "".PadRight(maxLength - _button.Length - 2)
44✔
323
                + Core.NEGATIVE_ANCHOR
44✔
324
                + " "
44✔
325
                + _button
44✔
326
                + " "
44✔
327
                + Core.NEGATIVE_ANCHOR
44✔
328
                + " │"
44✔
329
        );
44✔
330
        _textToDisplay.Add(GetCorners[2] + new string('─', maxLength + 2) + GetCorners[3]);
44✔
331
    }
44✔
332
    #endregion
333
}
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