• 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

94.44
/src/ConsoleAppVisuals/elements/passive_elements/LoadingBar.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.PassiveElements;
6

7
/// <summary>
8
/// A <see cref="LoadingBar"/> is a passive element that displays a loading bar using a reference to a progress variable.
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 LoadingBar : PassiveElement
18
{
19
    #region Fields
20
    private string _text;
21
    private Placement _placement;
22
    private float _progress;
23
    private int _additionalDuration;
24
    #endregion
25

26
    #region Constants
27
    private const char LOADING_BAR_CHAR = 'â–ˆ';
28
    private const float MIN_PROGRESS = 0f;
29
    private const float MAX_PROGRESS = 1f;
30

31
    #endregion
32

33
    #region Properties
34
    /// <summary>
35
    /// The height of the loading bar.
36
    /// </summary>
37
    /// <remarks>One line for the text,one line for the space between and one line for the progress.</remarks>
38
    public override int Height => 3;
1✔
39

40
    /// <summary>
41
    /// The width of the loading bar.
42
    /// </summary>
43
    public override int Width => _text.Length;
3✔
44

45
    /// <summary>
46
    /// The placement of the loading bar.
47
    /// </summary>
48
    public override Placement Placement => _placement;
9✔
49

50
    /// <summary>
51
    /// The maximum number of this element.
52
    /// </summary>
53
    public override int MaxNumberOfThisElement => 1;
1✔
54

55
    /// <summary>
56
    /// Getters and setters of the text of the loading bar.
57
    /// </summary>
58
    public string Text => _text;
10✔
59

60
    /// <summary>
61
    /// Getters and setters of the progress of the loading bar.
62
    /// </summary>
63
    public float Progress => _progress;
7✔
64

65
    /// <summary>
66
    /// Getters of the additional duration of the loading bar at the end.
67
    /// </summary>
68
    public int AdditionalDuration => _additionalDuration;
3✔
69
    #endregion
70

71
    #region Constructor
72
    /// <summary>
73
    /// A <see cref="LoadingBar"/> is a passive element that displays a loading bar using a reference to a progress variable.
74
    /// </summary>
75
    /// <param name="text">The text of the loading bar.</param>
76
    /// <param name="progress">The reference of the progress of the loading bar (that means that you should put a reference to a variable that will contain the percentage of progress of your process).</param>
77
    /// <param name="placement">The placement of the loading bar.</param>
78
    /// <param name="additionalDuration">The duration of the loading bar after the process.</param>
79
    /// <remarks>
80
    /// For more information, refer to the following resources:
81
    /// <list type="bullet">
82
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
83
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
84
    /// </list>
85
    /// </remarks>
86
    public LoadingBar(
27✔
87
        string text,
27✔
88
        ref float progress,
27✔
89
        Placement placement = Placement.TopCenter,
27✔
90
        int additionalDuration = 1000
27✔
91
    )
27✔
92
    {
93
        if (Console.WindowWidth - 1 < 0)
27!
94
        {
95
            _text = text[..Math.Min(text.Length, 0)];
27✔
96
        }
97
        else
98
        {
99
            _text = text[..Math.Min(text.Length, Console.WindowWidth - 1)];
×
100
        }
101
        _progress = progress;
27✔
102
        _placement = placement;
27✔
103
        _additionalDuration = additionalDuration;
27✔
104
    }
27✔
105
    #endregion
106

107
    #region Methods
108
    /// <summary>
109
    /// This method is used to update the text of the loading bar.
110
    /// </summary>
111
    /// <param name="text">The new text of the loading bar.</param>
112
    /// <remarks>
113
    /// For more information, refer to the following resources:
114
    /// <list type="bullet">
115
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
116
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
117
    /// </list>
118
    /// </remarks>
119
    public void UpdateText(string text)
120
    {
121
        _text = text;
3✔
122
    }
3✔
123

124
    /// <summary>
125
    /// This method is used to update the placement of the loading bar.
126
    /// </summary>
127
    /// <param name="placement">The new placement of the loading bar.</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 UpdatePlacement(Placement placement)
136
    {
137
        _placement = placement;
3✔
138
    }
3✔
139

140
    /// <summary>
141
    /// This method is used to update the additional duration of the loading bar.
142
    /// </summary>
143
    /// <param name="additionalDuration">The new additional duration of the loading bar.</param>
144
    /// <exception cref="ArgumentException">The additional duration of the loading bar cannot be negative.</exception>
145
    /// <remarks>
146
    /// For more information, refer to the following resources:
147
    /// <list type="bullet">
148
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
149
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
150
    /// </list>
151
    /// </remarks>
152
    public void UpdateAdditionalDuration(int additionalDuration)
153
    {
154
        if (additionalDuration < 0)
2✔
155
        {
156
            throw new ArgumentException(
1✔
157
                "The additional duration of the loading bar cannot be negative."
1✔
158
            );
1✔
159
        }
160
        _additionalDuration = additionalDuration;
1✔
161
    }
1✔
162

163
    /// <summary>
164
    /// This method is used to update the progress of the loading bar.
165
    /// </summary>
166
    /// <param name="progress">The new progress of the loading bar.</param>
167
    /// <remarks>
168
    /// For more information, refer to the following resources:
169
    /// <list type="bullet">
170
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
171
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
172
    /// </list>
173
    /// </remarks>
174
    public void UpdateProgress(float progress)
175
    {
176
        _progress = progress;
3✔
177
    }
3✔
178

179
    /// <summary>
180
    /// This method is used to draw the loading bar on the console.
181
    /// </summary>
182
    ///
183
    [Visual]
184
    protected override void RenderElementActions()
185
    {
186
        [Visual]
187
        void BuildBar(string text, float progress, int line)
188
        {
189
            StringBuilder loadingBar = new();
190
            for (int j = 0; j <= (int)(text.Length * progress); j++)
191
            {
192
                loadingBar.Append(LOADING_BAR_CHAR);
193
            }
194
            Core.WritePositionedString(
195
                loadingBar.ToString().ResizeString(text.Length, TextAlignment.Left),
196
                _placement,
197
                false,
198
                line + 2,
199
                false
200
            );
201
        }
202

203
        Core.WritePositionedString(_text, _placement, false, Line, false);
204
        while (_progress < MAX_PROGRESS)
205
        {
206
            BuildBar(_text, _progress, Line);
207
        }
208
        BuildBar(_text, MAX_PROGRESS, Line);
209
        Thread.Sleep(_additionalDuration);
210
        _progress = MIN_PROGRESS;
211
        Window.DeactivateElement(this);
212
    }
213
    #endregion
214
}
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