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

MorganKryze / ConsoleAppVisuals / 8114215569

01 Mar 2024 04:29PM UTC coverage: 85.767% (-9.3%) from 95.093%
8114215569

push

github

MorganKryze
🤖 moved to publish

865 of 1072 branches covered (80.69%)

Branch coverage included in aggregate %.

1708 of 1928 relevant lines covered (88.59%)

273.5 hits per line

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

69.7
/src/ConsoleAppVisuals/elements/static_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.Elements;
6

7
/// <summary>
8
/// Defines the loading bar of the console window.
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 : Element
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;
3✔
39

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

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

50
    /// <summary>
51
    /// Getters and setters of the text of the loading bar.
52
    /// </summary>
53
    public string Text => _text;
30✔
54

55
    /// <summary>
56
    /// Getters and setters of the progress of the loading bar.
57
    /// </summary>
58
    public float Progress => _progress;
21✔
59

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

66
    #region Constructor
67
    /// <summary>
68
    /// The natural constructor of the loading bar.
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, refer to the following resources:
76
    /// <list type="bullet">
77
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
78
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
79
    /// </list>
80
    /// </remarks>
81
    public LoadingBar(
63✔
82
        string text,
63✔
83
        ref float progress,
63✔
84
        Placement placement = Placement.TopCenter,
63✔
85
        int additionalDuration = 1000
63✔
86
    )
63✔
87
    {
88
        if (Console.WindowWidth - 1 < 0)
63!
89
        {
90
            _text = text[..Math.Min(text.Length, 0)];
63✔
91
        }
92
        else
93
        {
94
            _text = text[..Math.Min(text.Length, Console.WindowWidth - 1)];
×
95
        }
96
        _progress = progress;
63✔
97
        _placement = placement;
63✔
98
        _additionalDuration = additionalDuration;
63✔
99
    }
63✔
100
    #endregion
101

102
    #region Methods
103
    /// <summary>
104
    /// This method is used to update the text of the loading bar.
105
    /// </summary>
106
    /// <param name="text">The new text of the loading bar.</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>
114
    public void UpdateText(string text)
115
    {
116
        _text = text;
9✔
117
    }
9✔
118

119
    /// <summary>
120
    /// This method is used to update the placement of the loading bar.
121
    /// </summary>
122
    /// <param name="placement">The new placement of the loading bar.</param>
123
    /// <remarks>
124
    /// For more information, refer to the following resources:
125
    /// <list type="bullet">
126
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
127
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
128
    /// </list>
129
    /// </remarks>
130
    public void UpdatePlacement(Placement placement)
131
    {
132
        _placement = placement;
×
133
    }
×
134

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

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

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

196
        Core.WritePositionedString(_text, _placement.ToTextAlignment(), false, Line, false);
197
        while (_progress < MAX_PROGRESS)
198
        {
199
            BuildBar(_text, _progress, Line);
200
        }
201
        BuildBar(_text, MAX_PROGRESS, Line);
202
        Thread.Sleep(_additionalDuration);
203
        _progress = MIN_PROGRESS;
204
        Window.DeactivateElement(this);
205
    }
206
    #endregion
207
}
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