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

MorganKryze / ConsoleAppVisuals / 8418737641

25 Mar 2024 10:35AM UTC coverage: 93.788%. Remained the same
8418737641

push

github

MorganKryze
🚑 (TableSelector) fix render issue with first display not rendering properly

917 of 1042 branches covered (88.0%)

Branch coverage included in aggregate %.

2012 of 2081 relevant lines covered (96.68%)

252.64 hits per line

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

0.0
/src/ConsoleAppVisuals/elements/passive_elements/Text.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
/// A <see cref="Text"/> is an passive element that displays one or multiple lines of 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 Text : PassiveElement
18
{
19
    #region Fields
20
    private List<string> _lines;
21
    private TextAlignment _align;
22
    private Placement _placement;
23
    private List<string>? _textToDisplay;
24

25
    #endregion
26

27
    #region Properties
28
    /// <summary>
29
    /// The position of the Text element.
30
    /// </summary>
31
    public override Placement Placement => _placement;
×
32

33
    /// <summary>
34
    /// The alignment of the Text element.
35
    /// </summary>
36
    public override TextAlignment TextAlignment => _align;
×
37

38
    /// <summary>
39
    /// The height of the Text element.
40
    /// </summary>
41
    public override int Height => _textToDisplay!.Count;
×
42

43
    /// <summary>
44
    /// The width of the Text element.
45
    /// </summary>
46
    public override int Width => _textToDisplay!.Max(s => s.Length);
×
47

48
    /// <summary>
49
    /// The text of the Text element.
50
    /// </summary>
51
    public List<string> Lines => _lines;
×
52

53
    /// <summary>
54
    /// The text to display.
55
    /// </summary>
56
    public List<string>? TextToDisplay => _textToDisplay;
×
57

58
    #endregion
59

60
    #region Constructor
61
    /// <summary>
62
    /// A <see cref="Text"/> is an passive element that displays one or multiple lines of text.
63
    /// </summary>
64
    /// <param name="lines">The text to display.</param>
65
    /// <param name="align">The alignment of the Text element.</param>
66
    /// <param name="placement">The placement of the Text element.</param>
67
    /// <remarks>
68
    /// For more information, refer to the following resources:
69
    /// <list type="bullet">
70
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
71
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
72
    /// </list>
73
    /// </remarks>
74
    public Text(
×
75
        List<string> lines,
×
76
        TextAlignment align = TextAlignment.Left,
×
77
        Placement placement = Placement.TopCenter
×
78
    )
×
79
    {
80
        _lines = lines;
×
81
        _align = align;
×
82
        _placement = placement;
×
83
        if (CheckIntegrity())
×
84
            Build();
×
85
    }
×
86
    #endregion
87

88
    #region Methods
89
    private bool CheckIntegrity() => _lines.Count != 0;
×
90

91
    /// <summary>
92
    /// This method updates the lines of the Text element.
93
    /// </summary>
94
    /// <param name="lines">The new text of the Text element.</param>
95
    /// <remarks>
96
    /// For more information, refer to the following resources:
97
    /// <list type="bullet">
98
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
99
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
100
    /// </list>
101
    /// </remarks>
102
    public void UpdateLines(List<string> lines)
103
    {
104
        _lines.Clear();
×
105
        _lines = lines;
×
106
        Build();
×
107
    }
×
108

109
    /// <summary>
110
    /// This method updates the placement of the Text element.
111
    /// </summary>
112
    /// <param name="newPlacement">The new placement of the Text element.</param>
113
    /// <remarks>
114
    /// For more information, refer to the following resources:
115
    /// <list type="bullet">
116
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
117
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
118
    /// </list>
119
    /// </remarks>
120
    public void UpdatePlacement(Placement newPlacement)
121
    {
122
        _placement = newPlacement;
×
123
    }
×
124

125
    /// <summary>
126
    /// This method updates the alignment of the Text element.
127
    /// </summary>
128
    /// <param name="newAlignment">The new alignment of the Text element.</param>
129
    /// <remarks>
130
    /// For more information, refer to the following resources:
131
    /// <list type="bullet">
132
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
133
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
134
    /// </list>
135
    /// </remarks>
136
    public void UpdateTextAlignment(TextAlignment newAlignment)
137
    {
138
        _align = newAlignment;
×
139
        Build();
×
140
    }
×
141

142
    /// <summary>
143
    /// Adds a line to the Text element.
144
    /// </summary>
145
    /// <param name="line">The line to add.</param>
146
    /// <remarks>
147
    /// For more information, refer to the following resources:
148
    /// <list type="bullet">
149
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
150
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
151
    /// </list>
152
    /// </remarks>
153
    public void AddLine(string line)
154
    {
155
        _lines.Add(line);
×
156
        Build();
×
157
    }
×
158

159
    /// <summary>
160
    /// Inserts a line to the Text element.
161
    /// </summary>
162
    /// <param name="line">The line to insert.</param>
163
    /// <param name="index">The index where to insert the line.</param>
164
    /// <remarks>
165
    /// For more information, refer to the following resources:
166
    /// <list type="bullet">
167
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
168
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
169
    /// </list>
170
    /// </remarks>
171
    public void InsertLine(string line, int index)
172
    {
173
        _lines.Insert(index, line);
×
174
        Build();
×
175
    }
×
176

177
    /// <summary>
178
    /// Removes a line from the Text element.
179
    /// </summary>
180
    /// <param name="line">The line to remove.</param>
181
    /// <remarks>
182
    /// For more information, refer to the following resources:
183
    /// <list type="bullet">
184
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
185
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
186
    /// </list>
187
    /// </remarks>
188
    public void RemoveLine(string line)
189
    {
190
        if (!_lines.Contains(line))
×
191
        {
192
            throw new ArgumentException("The line is not in the text.");
×
193
        }
194
        _lines.Remove(line);
×
195
    }
×
196

197
    /// <summary>
198
    /// Removes a line from the Text element.
199
    /// </summary>
200
    /// <param name="index">The index of the line to remove.</param>
201
    /// <remarks>
202
    /// For more information, refer to the following resources:
203
    /// <list type="bullet">
204
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
205
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
206
    /// </list>
207
    /// </remarks>
208
    public void RemoveLine(int index)
209
    {
210
        if (index < 0 || index >= _lines.Count)
×
211
        {
212
            throw new ArgumentOutOfRangeException("The index is out of range.");
×
213
        }
214
        _lines.RemoveAt(index);
×
215
    }
×
216

217
    /// <summary>
218
    /// Renders the Text element.
219
    /// </summary>
220
    [Visual]
221
    protected override void RenderElementActions()
222
    {
223
        Build();
224
        Core.WriteMultiplePositionedLines(
225
            false,
226
            TextAlignment,
227
            Placement,
228
            false,
229
            Line,
230
            _textToDisplay!.ToArray()
231
        );
232
    }
233

234
    private void Build()
235
    {
236
        if (_lines.Count == 0)
×
237
        {
238
            throw new ArgumentException("The element is empty.");
×
239
        }
240

241
        var maxLength = _lines.Max(s => s.Length);
×
242
        _textToDisplay = new List<string>();
×
243

244
        foreach (var line in _lines)
×
245
        {
246
            string lineToDisplay;
247
            switch (_align)
×
248
            {
249
                case TextAlignment.Center:
250
                    int totalPadding = maxLength - line.Length;
×
251
                    int padLeft = totalPadding / 2;
×
252
                    lineToDisplay = line.PadLeft(line.Length + padLeft).PadRight(maxLength);
×
253
                    break;
×
254
                case TextAlignment.Left:
255
                    lineToDisplay = line.PadRight(maxLength);
×
256
                    break;
×
257
                case TextAlignment.Right:
258
                    lineToDisplay = line.PadLeft(maxLength);
×
259
                    break;
×
260
                default:
261
                    lineToDisplay = line;
×
262
                    break;
263
            }
264
            _textToDisplay.Add(lineToDisplay);
×
265
        }
266
    }
×
267
    #endregion
268
}
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