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

MorganKryze / ConsoleAppVisuals / 8426885247

25 Mar 2024 08:59PM UTC coverage: 89.709% (-4.1%) from 93.788%
8426885247

push

github

MorganKryze
🌟 work in progress, new dialog element

917 of 1072 branches covered (85.54%)

Branch coverage included in aggregate %.

0 of 112 new or added lines in 1 file covered. (0.0%)

2012 of 2193 relevant lines covered (91.75%)

240.0 hits per line

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

0.0
/src/ConsoleAppVisuals/elements/interactive_elements/Dialog.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="Dialog"/> is an interactive element that displays a dialog bow with one or two choices.
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 Dialog : InteractiveElement<int>
18
{
19
    #region Fields
20
    private List<string> _lines;
21
    private string? _leftOption;
22
    private string? _rightOption;
23
    private TextAlignment _align;
24
    private Placement _placement;
25
    private readonly Borders _borders;
26
    private List<string>? _textToDisplay;
27
    #endregion
28

29
    #region Constants
30
    private const int EMBED_MARGIN = 2;
31

32
    /// <summary>
33
    /// The width ratio between the two options of the Dialog.
34
    /// It ensures that the options are not too close to each other.
35
    /// </summary>
36
    private const double WIDTH_RATIO = 1.1;
37
    #endregion
38

39
    #region Properties
40
    /// <summary>
41
    /// The position of the Dialog.
42
    /// </summary>
NEW
43
    public override Placement Placement => _placement;
×
44

45
    /// <summary>
46
    /// The alignment of the Dialog.
47
    /// </summary>
NEW
48
    public override TextAlignment TextAlignment => _align;
×
49

50
    /// <summary>
51
    /// The height of the Dialog.
52
    /// </summary>
NEW
53
    public override int Height => _textToDisplay!.Count;
×
54

55
    /// <summary>
56
    /// The width of the Dialog.
57
    /// </summary>
NEW
58
    public override int Width => _textToDisplay!.Max(s => s.Length);
×
59

60
    /// <summary>
61
    /// The rows of the Dialog.
62
    /// </summary>
NEW
63
    public List<string> Lines => _lines;
×
64

65
    /// <summary>
66
    /// The text of the left option.
67
    /// </summary>
NEW
68
    public string? LeftOption => _leftOption;
×
69

70
    /// <summary>
71
    /// The text of the right option.
72
    /// </summary>
NEW
73
    public string? RightOption => _rightOption;
×
74

75
    /// <summary>
76
    /// The borders of the Dialog.
77
    /// </summary>
NEW
78
    public Borders Borders => _borders;
×
79

80
    /// <summary>
81
    /// The border type of the selector.
82
    /// </summary>
NEW
83
    public BordersType BordersType => _borders.Type;
×
84

85
    /// <summary>
86
    /// The text to display.
87
    /// </summary>
NEW
88
    public List<string>? TextToDisplay => _textToDisplay;
×
89

90
    private int MaxLineLength =>
NEW
91
        Math.Max(
×
NEW
92
            _lines.Max(s => s.Length),
×
NEW
93
            (int)((_rightOption?.Length ?? 0) + (_leftOption?.Length ?? 0) * WIDTH_RATIO)
×
NEW
94
        );
×
95
    #endregion
96

97
    #region Constructor
98
    /// <summary>
99
    /// A <see cref="Dialog"/> is an interactive element that displays a dialog bow with one or two choices.
100
    /// </summary>
101
    /// <param name="lines">The text to display.</param>
102
    /// <param name="leftOption">The text of the left option. Null for no button.</param>
103
    /// <param name="rightOption">The text of the right option. Null for no button.</param>
104
    /// <param name="align">The alignment of the Dialog.</param>
105
    /// <param name="placement">The placement of the Dialog element.</param>
106
    /// <param name="bordersType">The type of border to display.</param>
107
    /// <remarks>
108
    /// For more information, refer to the following resources:
109
    /// <list type="bullet">
110
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
111
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
112
    /// </list>
113
    /// </remarks>
NEW
114
    public Dialog(
×
NEW
115
        List<string> lines,
×
NEW
116
        string? leftOption = null,
×
NEW
117
        string? rightOption = null,
×
NEW
118
        TextAlignment align = TextAlignment.Left,
×
NEW
119
        Placement placement = Placement.TopCenter,
×
NEW
120
        BordersType bordersType = BordersType.SingleStraight
×
NEW
121
    )
×
122
    {
NEW
123
        _lines = lines;
×
NEW
124
        _leftOption = leftOption;
×
NEW
125
        _rightOption = rightOption;
×
NEW
126
        _align = align;
×
NEW
127
        _placement = placement;
×
NEW
128
        _borders = new Borders(bordersType);
×
NEW
129
        if (CheckIntegrity())
×
NEW
130
            Build();
×
NEW
131
    }
×
132
    #endregion
133

134
    #region Methods
135
    private bool CheckIntegrity()
136
    {
NEW
137
        if (_lines.Count == 0)
×
138
        {
NEW
139
            return false;
×
140
        }
NEW
141
        return true;
×
142
    }
143

144
    /// <summary>
145
    /// This method updates the text of the first option.
146
    /// </summary>
147
    /// <param name="text">The new text of the first option.</param>
148
    /// <remarks>
149
    /// For more information, refer to the following resources:
150
    /// <list type="bullet">
151
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
152
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
153
    /// </list>
154
    /// </remarks>
155
    public void UpdateRightOption(string? text)
156
    {
NEW
157
        _rightOption = text;
×
NEW
158
        Build();
×
NEW
159
    }
×
160

161
    /// <summary>
162
    /// This method updates the text of the second option.
163
    /// </summary>
164
    /// <param name="text">The new text of the second option.</param>
165
    /// <remarks>
166
    /// For more information, refer to the following resources:
167
    /// <list type="bullet">
168
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
169
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
170
    /// </list>
171
    /// </remarks>
172
    public void UpdateLeftOption(string? text)
173
    {
NEW
174
        _leftOption = text;
×
NEW
175
        Build();
×
NEW
176
    }
×
177

178
    /// <summary>
179
    /// This method updates the text of the Dialog.
180
    /// </summary>
181
    /// <param name="lines">The new text of the Dialog.</param>
182
    /// <remarks>
183
    /// For more information, refer to the following resources:
184
    /// <list type="bullet">
185
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
186
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
187
    /// </list>
188
    /// </remarks>
189
    public void UpdateLines(List<string> lines)
190
    {
NEW
191
        _lines.Clear();
×
NEW
192
        _lines = lines;
×
NEW
193
        Build();
×
NEW
194
    }
×
195

196
    /// <summary>
197
    /// This method updates the placement of the Dialog.
198
    /// </summary>
199
    /// <param name="placement">The new placement of the Dialog.</param>
200
    /// <remarks>
201
    /// For more information, refer to the following resources:
202
    /// <list type="bullet">
203
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
204
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
205
    /// </list>
206
    /// </remarks>
207
    public void UpdatePlacement(Placement placement)
208
    {
NEW
209
        _placement = placement;
×
NEW
210
    }
×
211

212
    /// <summary>
213
    /// This method updates the alignment of the Dialog.
214
    /// </summary>
215
    /// <param name="align">The new alignment of the Dialog.</param>
216
    /// <remarks>
217
    /// For more information, refer to the following resources:
218
    /// <list type="bullet">
219
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
220
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
221
    /// </list>
222
    /// </remarks>
223
    public void UpdateTextAlignment(TextAlignment align)
224
    {
NEW
225
        _align = align;
×
NEW
226
        Build();
×
NEW
227
    }
×
228

229
    /// <summary>
230
    /// This method updates the borders of the Dialog.
231
    /// </summary>
232
    /// <param name="bordersType">The new border type of the Dialog.</param>
233
    /// <remarks>
234
    /// For more information, refer to the following resources:
235
    /// <list type="bullet">
236
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
237
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
238
    /// </list>
239
    /// </remarks>
240
    public void UpdateBordersType(BordersType bordersType)
241
    {
NEW
242
        _borders.UpdateBordersType(bordersType);
×
NEW
243
        Build();
×
NEW
244
    }
×
245

246
    /// <summary>
247
    /// Adds a line to the Dialog.
248
    /// </summary>
249
    /// <param name="line">The line to add.</param>
250
    /// <remarks>
251
    /// For more information, refer to the following resources:
252
    /// <list type="bullet">
253
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
254
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
255
    /// </list>
256
    /// </remarks>
257
    public void AddLine(string line)
258
    {
NEW
259
        _lines.Add(line);
×
NEW
260
        Build();
×
NEW
261
    }
×
262

263
    /// <summary>
264
    /// Inserts a line to the Dialog.
265
    /// </summary>
266
    /// <param name="line">The line to insert.</param>
267
    /// <param name="index">The index where to insert the line.</param>
268
    /// <remarks>
269
    /// For more information, refer to the following resources:
270
    /// <list type="bullet">
271
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
272
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
273
    /// </list>
274
    /// </remarks>
275
    public void InsertLine(string line, int index)
276
    {
NEW
277
        _lines.Insert(index, line);
×
NEW
278
        Build();
×
NEW
279
    }
×
280

281
    /// <summary>
282
    /// Removes a line from the Dialog.
283
    /// </summary>
284
    /// <param name="line">The line to remove.</param>
285
    /// <remarks>
286
    /// For more information, refer to the following resources:
287
    /// <list type="bullet">
288
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
289
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
290
    /// </list>
291
    /// </remarks>
292
    public void RemoveLine(string line)
293
    {
NEW
294
        if (!_lines.Contains(line))
×
295
        {
NEW
296
            throw new ArgumentException("The line is not in the text.");
×
297
        }
NEW
298
        _lines.Remove(line);
×
NEW
299
        Build();
×
NEW
300
    }
×
301

302
    /// <summary>
303
    /// Removes a line from the Dialog.
304
    /// </summary>
305
    /// <param name="index">The index of the line to remove.</param>
306
    /// <remarks>
307
    /// For more information, refer to the following resources:
308
    /// <list type="bullet">
309
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
310
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
311
    /// </list>
312
    /// </remarks>
313
    public void RemoveLine(int index)
314
    {
NEW
315
        if (index < 0 || index >= _lines.Count)
×
316
        {
NEW
317
            throw new ArgumentOutOfRangeException("The index is out of range.");
×
318
        }
NEW
319
        _lines.RemoveAt(index);
×
NEW
320
        Build();
×
NEW
321
    }
×
322

323
    /// <summary>
324
    /// Renders the Dialog.
325
    /// </summary>
326
    [Visual]
327
    protected override void RenderElementActions()
328
    {
329
        // TODO: UPDATE Code to implement new feature
330
        Build();
331
        Core.WriteMultiplePositionedLines(
332
            false,
333
            TextAlignment,
334
            Placement,
335
            false,
336
            Line,
337
            _textToDisplay!.ToArray()
338
        );
339
        Window.Freeze();
340
    }
341

342
    private void Build()
343
    {
NEW
344
        if (!CheckIntegrity())
×
345
        {
NEW
346
            throw new InvalidOperationException(
×
NEW
347
                "The lines are empty. You must provide at least one line to display in the Dialog"
×
NEW
348
            );
×
349
        }
350

NEW
351
        _textToDisplay = new List<string>();
×
352

NEW
353
        _textToDisplay.Add(
×
NEW
354
            Borders.TopLeft
×
NEW
355
                + new string(Borders.Horizontal, MaxLineLength + EMBED_MARGIN)
×
NEW
356
                + Borders.TopRight
×
NEW
357
        );
×
358

NEW
359
        foreach (var line in _lines)
×
360
        {
NEW
361
            var lineToDisplay = $"{Borders.Vertical} ";
×
NEW
362
            switch (_align)
×
363
            {
364
                case TextAlignment.Center:
NEW
365
                    int totalPadding = MaxLineLength - line.Length;
×
NEW
366
                    int padLeft = totalPadding / 2;
×
NEW
367
                    lineToDisplay += line.PadLeft(line.Length + padLeft).PadRight(MaxLineLength);
×
NEW
368
                    break;
×
369
                case TextAlignment.Left:
NEW
370
                    lineToDisplay += line.PadRight(MaxLineLength);
×
NEW
371
                    break;
×
372
                case TextAlignment.Right:
NEW
373
                    lineToDisplay += line.PadLeft(MaxLineLength);
×
374
                    break;
375
            }
NEW
376
            lineToDisplay += $" {Borders.Vertical}";
×
NEW
377
            _textToDisplay.Add(lineToDisplay);
×
378
        }
379

NEW
380
        if (_rightOption is not null || _leftOption is not null)
×
381
        {
NEW
382
            AddOptions();
×
383
        }
384

NEW
385
        _textToDisplay.Add(
×
NEW
386
            Borders.BottomLeft
×
NEW
387
                + new string(Borders.Horizontal, MaxLineLength + EMBED_MARGIN)
×
NEW
388
                + Borders.BottomRight
×
NEW
389
        );
×
NEW
390
    }
×
391

392
    private void AddOptions()
393
    {
NEW
394
        _textToDisplay!.Add(
×
NEW
395
            $"{Borders.Vertical} " + new string(' ', MaxLineLength) + $" {Borders.Vertical}"
×
NEW
396
        );
×
397

NEW
398
        string optionLine =
×
NEW
399
            $"{Borders.Vertical} " + new string(' ', MaxLineLength) + $" {Borders.Vertical}";
×
400

NEW
401
        if (_leftOption is not null)
×
402
        {
NEW
403
            optionLine = optionLine.Remove(2, _leftOption.Length);
×
NEW
404
            optionLine = optionLine.Insert(2, _leftOption);
×
405
        }
406

NEW
407
        if (_rightOption is not null)
×
408
        {
NEW
409
            int insertPosition = optionLine.Length - _rightOption.Length - 2;
×
NEW
410
            optionLine = optionLine.Remove(insertPosition, _rightOption.Length);
×
NEW
411
            optionLine = optionLine.Insert(insertPosition, _rightOption);
×
412
        }
413

NEW
414
        _textToDisplay.Add(optionLine);
×
NEW
415
    }
×
416
    #endregion
417
}
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