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

MorganKryze / ConsoleAppVisuals / 7972444880

pending completion
7972444880

push

github

MorganKryze
📖 add legacy docs

827 of 916 branches covered (90.28%)

Branch coverage included in aggregate %.

1676 of 1694 relevant lines covered (98.94%)

105.1 hits per line

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

95.79
/src/ConsoleAppVisuals/elements/interactive/EmbeddedText.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;
6

7
/// <summary>
8
/// Defines the basic properties of an embedded 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/Program.cs">Example Project</a></description></item>
15
/// </list>
16
/// </remarks>
17
public class EmbeddedText : InteractiveElement<string>
18
{
19
    #region Fields
20
    private readonly List<string> _text;
21
    private readonly string _button;
22
    private readonly TextAlignment _align;
23
    private readonly Placement _placement;
24
    private readonly int _line;
25
    private List<string>? _textToDisplay;
26
    #endregion
27

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

34
    /// <summary>
35
    /// The Line of the Embedded text.
36
    /// </summary>
37
    public override int Line => _line;
3✔
38

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

44
    /// <summary>
45
    /// The width of the Embedded text.
46
    /// </summary>
47
    public override int Width => _textToDisplay!.Max((string s) => s.Length) - 8;
21✔
48
    #endregion
49

50
    #region Getters and setters
51
    /// <summary>
52
    /// The text of the Embedded text.
53
    /// </summary>
54
    public List<string> Text => _text;
9✔
55

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

61
    /// <summary>
62
    /// The text to display.
63
    /// </summary>
64
    public List<string>? TextToDisplay => _textToDisplay;
21✔
65
    #endregion
66

67
    #region Constructor
68
    /// <summary>
69
    /// The natural constructor of the Embedded text.
70
    /// </summary>
71
    /// <param name="text">The text to display.</param>
72
    /// <param name="button">The text of the button.</param>
73
    /// <param name="align">The alignment of the Embedded text.</param>
74
    /// <param name="placement">The placement of the Embedded text element.</param>
75
    /// <param name="line">The line of the Embedded text.</param>
76
    /// <remarks>
77
    /// For more information, refer to the following resources:
78
    /// <list type="bullet">
79
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
80
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
81
    /// </list>
82
    /// </remarks>
83
    public EmbeddedText(
69✔
84
        List<string> text,
69✔
85
        string? button = null,
69✔
86
        TextAlignment align = TextAlignment.Left,
69✔
87
        Placement placement = Placement.TopCenter,
69✔
88
        int? line = null
69✔
89
    )
69✔
90
    {
91
        _text = text;
69✔
92
        _button = button ?? "Press [Enter] to continue";
69✔
93
        _align = align;
69✔
94
        _placement = placement;
69✔
95
        _line = Window.CheckLine(line) ?? Window.GetLineAvailable(placement);
69✔
96
        if (CheckIntegrity())
69✔
97
            BuildText();
63✔
98
    }
69✔
99
    #endregion
100

101
    #region Methods
102

103
    private bool CheckIntegrity()
104
    {
105
        if (_text.Count == 0)
132✔
106
        {
107
            return false;
3✔
108
        }
109

110
        if (_text.Max((string s) => s.Length) < _button.Length)
393✔
111
        {
112
            return false;
3✔
113
        }
114
        return true;
126✔
115
    }
116

117
    /// <summary>
118
    /// Adds a line to the Embedded text.
119
    /// </summary>
120
    /// <param name="line">The line to add.</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/Program.cs">Example Project</a></description></item>
126
    /// </list>
127
    /// </remarks>
128
    public void AddLine(string line)
129
    {
130
        _text.Add(line);
3✔
131
    }
3✔
132

133
    /// <summary>
134
    /// Inserts a line to the Embedded text.
135
    /// </summary>
136
    /// <param name="line">The line to insert.</param>
137
    /// <param name="index">The index where to insert the line.</param>
138
    /// <remarks>
139
    /// For more information, refer to the following resources:
140
    /// <list type="bullet">
141
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
142
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
143
    /// </list>
144
    /// </remarks>
145
    public void InsertLine(string line, int index)
146
    {
147
        _text.Insert(index, line);
3✔
148
    }
3✔
149

150
    /// <summary>
151
    /// Removes a line from the Embedded text.
152
    /// </summary>
153
    /// <param name="line">The line to remove.</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/Program.cs">Example Project</a></description></item>
159
    /// </list>
160
    /// </remarks>
161
    public void RemoveLine(string line)
162
    {
163
        if (!_text.Contains(line))
6✔
164
        {
165
            throw new ArgumentException("The line is not in the text.");
3✔
166
        }
167
        _text.Remove(line);
3✔
168
    }
3✔
169

170
    /// <summary>
171
    /// Removes a line from the Embedded text.
172
    /// </summary>
173
    /// <param name="index">The index of the line to remove.</param>
174
    /// <remarks>
175
    /// For more information, refer to the following resources:
176
    /// <list type="bullet">
177
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
178
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
179
    /// </list>
180
    /// </remarks>
181
    public void RemoveLine(int index)
182
    {
183
        if (index < 0 || index >= _text.Count)
9✔
184
        {
185
            throw new ArgumentOutOfRangeException("The index is out of range.");
6✔
186
        }
187
        _text.RemoveAt(index);
3✔
188
    }
3✔
189

190
    /// <summary>
191
    /// Renders the Embedded text.
192
    /// </summary>
193
    [Visual]
194
    protected override void RenderElementActions()
195
    {
196
        BuildText();
197
        Core.WriteMultiplePositionedLines(
198
            false,
199
            _placement.ToTextAlignment(),
200
            false,
201
            _line,
202
            _textToDisplay!.ToArray()
203
        );
204
        Window.StopExecution();
205
        Window.DeactivateElement<EmbeddedText>();
206
    }
207

208
    private void BuildText()
209
    {
210
        if (!CheckIntegrity())
63!
211
        {
212
            throw new FormatException(
×
213
                "Check that the text is not empty and that the button is not longer than the text."
×
214
            );
×
215
        }
216
        var maxLength = _text.Max((string s) => s.Length);
192✔
217
        _textToDisplay = new List<string>();
63✔
218
        foreach (var line in _text)
384✔
219
        {
220
            var lineToDisplay = "│ ";
129✔
221
            switch (_align)
129✔
222
            {
223
                case TextAlignment.Center:
224
                    int totalPadding = maxLength - line.Length;
6✔
225
                    int padLeft = totalPadding / 2;
6✔
226
                    lineToDisplay += line.PadLeft(line.Length + padLeft).PadRight(maxLength);
6✔
227
                    break;
6✔
228
                case TextAlignment.Left:
229
                    lineToDisplay += line.PadRight(maxLength);
117✔
230
                    break;
117✔
231
                case TextAlignment.Right:
232
                    lineToDisplay += line.PadLeft(maxLength);
6✔
233
                    break;
234
            }
235
            lineToDisplay += " │";
129✔
236
            _textToDisplay.Add(lineToDisplay);
129✔
237
        }
238
        _textToDisplay.Insert(0, "┌" + new string('─', maxLength + 2) + "┐");
63✔
239
        _textToDisplay.Add("│ " + new string(' ', maxLength) + " │");
63✔
240
        _textToDisplay.Add(
63✔
241
            "│ "
63✔
242
                + "".PadRight(maxLength - _button.Length - 2)
63✔
243
                + Core.NEGATIVE_ANCHOR
63✔
244
                + " "
63✔
245
                + _button
63✔
246
                + " "
63✔
247
                + Core.NEGATIVE_ANCHOR
63✔
248
                + " │"
63✔
249
        );
63✔
250
        _textToDisplay.Add("└" + new string('─', maxLength + 2) + "┘");
63✔
251
    }
63✔
252
    #endregion
253
}
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