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

MorganKryze / ConsoleAppVisuals / 8520738714

02 Apr 2024 09:44AM UTC coverage: 96.257%. Remained the same
8520738714

push

github

MorganKryze
📖 (Elements options) update videos

942 of 1051 branches covered (89.63%)

Branch coverage included in aggregate %.

2118 of 2128 relevant lines covered (99.53%)

251.1 hits per line

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

94.29
/src/ConsoleAppVisuals/elements/animated_elements/LoadingBar.cs
1
/*
2
    Copyright (c) 2024 Yann M. Vidamment (MorganKryze)
3
    Licensed under GNU GPL v2.0. See full license at: https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/LICENSE.md
4
*/
5
namespace ConsoleAppVisuals.AnimatedElements;
6

7
/// <summary>
8
/// The <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, consider visiting the documentation available <a href="https://morgankryze.github.io/ConsoleAppVisuals/">here</a>.
12
/// </remarks>
13
public class LoadingBar : AnimatedElement
14
{
15
    #region Constants
16
    const char LOADING_BAR_CHAR = 'â–ˆ';
17
    const float MIN_PROGRESS = 0f;
18
    const float MAX_PROGRESS = 1f;
19
    const int DEFAULT_HEIGHT = 3;
20
    const Placement DEFAULT_PLACEMENT = Placement.TopCenter;
21
    const int DEFAULT_ADDITIONAL_DURATION = 1000;
22
    #endregion
23

24
    #region Fields
25
    private string _text;
26
    private Placement _placement;
27
    private float _progress;
28
    private int _additionalDuration;
29
    #endregion
30

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

38
    /// <summary>
39
    /// Gets the width of the loading bar.
40
    /// </summary>
41
    public override int Width => _text.Length;
3✔
42

43
    /// <summary>
44
    /// Gets the placement of the loading bar.
45
    /// </summary>
46
    public override Placement Placement => _placement;
9✔
47
    #endregion
48

49
    #region Properties
50
    /// <summary>
51
    /// Gets the text of the loading bar.
52
    /// </summary>
53
    public string Text => _text;
10✔
54

55
    /// <summary>
56
    /// Gets the progress of the loading bar.
57
    /// </summary>
58
    public float Progress => _progress;
7✔
59

60
    /// <summary>
61
    /// Gets the additional duration of the loading bar at the end.
62
    /// </summary>
63
    public int AdditionalDuration => _additionalDuration;
3✔
64
    #endregion
65

66
    #region Constructor
67
    /// <summary>
68
    /// The <see cref="LoadingBar"/> is a passive element that displays a loading bar using a reference to a progress variable.
69
    /// </summary>
70
    /// <param name="text">The text of the loading bar.</param>
71
    /// <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>
72
    /// <param name="placement">The placement of the loading bar.</param>
73
    /// <param name="additionalDuration">The duration of the loading bar after the process.</param>
74
    /// <remarks>
75
    /// For more information, consider visiting the documentation available <a href="https://morgankryze.github.io/ConsoleAppVisuals/">here</a>.
76
    /// </remarks>
77
    public LoadingBar(
27✔
78
        string text,
27✔
79
        ref float progress,
27✔
80
        Placement placement = DEFAULT_PLACEMENT,
27✔
81
        int additionalDuration = DEFAULT_ADDITIONAL_DURATION
27✔
82
    )
27✔
83
    {
84
        if (Console.WindowWidth - 1 < 0)
27!
85
        {
86
            _text = text[..Math.Min(text.Length, 0)];
27✔
87
        }
88
        else
89
        {
90
            _text = text[..Math.Min(text.Length, Console.WindowWidth - 1)];
×
91
        }
92
        _progress = progress;
27✔
93
        _placement = placement;
27✔
94
        _additionalDuration = additionalDuration;
27✔
95
    }
27✔
96
    #endregion
97

98
    #region Update Methods
99
    /// <summary>
100
    /// Updates the text of the loading bar.
101
    /// </summary>
102
    /// <param name="text">The new text of the loading bar.</param>
103
    /// <remarks>
104
    /// For more information, consider visiting the documentation available <a href="https://morgankryze.github.io/ConsoleAppVisuals/">here</a>.
105
    /// </remarks>
106
    public void UpdateText(string text)
107
    {
108
        _text = text;
3✔
109
    }
3✔
110

111
    /// <summary>
112
    /// Updates the placement of the loading bar.
113
    /// </summary>
114
    /// <param name="placement">The new placement of the loading bar.</param>
115
    /// <remarks>
116
    /// For more information, consider visiting the documentation available <a href="https://morgankryze.github.io/ConsoleAppVisuals/">here</a>.
117
    /// </remarks>
118
    public void UpdatePlacement(Placement placement)
119
    {
120
        _placement = placement;
3✔
121
    }
3✔
122

123
    /// <summary>
124
    /// Updates the additional duration of the loading bar.
125
    /// </summary>
126
    /// <param name="additionalDuration">The new additional duration of the loading bar.</param>
127
    /// <exception cref="ArgumentException">The additional duration of the loading bar cannot be negative.</exception>
128
    /// <remarks>
129
    /// For more information, consider visiting the documentation available <a href="https://morgankryze.github.io/ConsoleAppVisuals/">here</a>.
130
    /// </remarks>
131
    public void UpdateAdditionalDuration(int additionalDuration)
132
    {
133
        if (additionalDuration < 0)
2✔
134
        {
135
            throw new ArgumentException(
1✔
136
                "The additional duration of the loading bar cannot be negative."
1✔
137
            );
1✔
138
        }
139
        _additionalDuration = additionalDuration;
1✔
140
    }
1✔
141

142
    /// <summary>
143
    /// Updates the progress of the loading bar.
144
    /// </summary>
145
    /// <param name="progress">The new progress of the loading bar.</param>
146
    /// <remarks>
147
    /// For more information, consider visiting the documentation available <a href="https://morgankryze.github.io/ConsoleAppVisuals/">here</a>.
148
    /// </remarks>
149
    public void UpdateProgress(float progress)
150
    {
151
        _progress = progress;
3✔
152
    }
3✔
153
    #endregion
154

155
    #region Rendering
156
    /// <summary>
157
    /// Defines the actions to perform when the element is called to be rendered on the console.
158
    /// </summary>
159
    [Visual]
160
    protected override void RenderElementActions()
161
    {
162
        [Visual]
163
        void BuildBar(string text, float progress, int line)
164
        {
165
            StringBuilder loadingBar = new();
166
            for (int j = 0; j <= (int)(text.Length * progress); j++)
167
            {
168
                loadingBar.Append(LOADING_BAR_CHAR);
169
            }
170
            Core.WritePositionedString(
171
                loadingBar.ToString().ResizeString(text.Length, TextAlignment.Left),
172
                _placement,
173
                false,
174
                line + 2,
175
                false
176
            );
177
        }
178

179
        Core.WritePositionedString(_text, _placement, false, Line, false);
180
        while (_progress < MAX_PROGRESS)
181
        {
182
            BuildBar(_text, _progress, Line);
183
        }
184
        BuildBar(_text, MAX_PROGRESS, Line);
185
        Thread.Sleep(_additionalDuration);
186
        _progress = MIN_PROGRESS;
187
        Window.DeactivateElement(this);
188
    }
189
    #endregion
190
}
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