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

MorganKryze / ConsoleAppVisuals / 7626781713

23 Jan 2024 01:56PM UTC coverage: 59.44% (+0.9%) from 58.519%
7626781713

push

github

robinmoon2
2 solution action TestLoadingBar

575 of 1044 branches covered (0.0%)

Branch coverage included in aggregate %.

1188 of 1922 relevant lines covered (61.81%)

208.14 hits per line

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

60.98
/src/ConsoleAppVisuals/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;
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/Program.cs">Example Project</a></description></item>
15
/// </list>
16
/// </remarks>
17
public class LoadingBar : Element
18
{
19
    #region Fields
20
    private string _text;
21
    private readonly Placement _placement;
22
    private float _progress;
23
    private readonly int _line;
24
    private readonly int _additionalDuration;
25
    #endregion
26

27
    #region Constants
28
    private const char LOADING_BAR_CHAR = '█';
29
    private const float MIN_PROGRESS = 0f;
30
    private const float MAX_PROGRESS = 1f;
31

32
    #endregion
33

34
    #region Properties
35
    /// <summary>
36
    /// The line of the loading bar in the console.
37
    /// </summary>
38
    /// <remarks>We add 2 because so the loading bar does not overlap with the title.</remarks>
39
    public override int Line => _line;
3✔
40

41
    /// <summary>
42
    /// The height of the loading bar.
43
    /// </summary>
44
    /// <remarks>One line for the text,one line for the space between and one line for the progress.</remarks>
45
    public override int Height => 3;
3✔
46

47
    /// <summary>
48
    /// The width of the loading bar.
49
    /// </summary>
50
    public override int Width => _text.Length;
9✔
51
    #endregion
52

53
    #region Getters & Setters
54

55
    /// <summary>
56
    /// Getters and setters of the text of the loading bar.
57
    /// </summary>
58
    public string Text { get => _text; set => _text = value; }
24✔
59

60
    /// <summary>
61
    /// Getters and setters of the progress of the loading bar.
62
    /// </summary>
63
    public float Progress { get => _progress; set => _progress = value; }
9✔
64

65
    #endregion
66

67

68

69

70

71
    #region Constructor
72
    /// <summary>
73
    /// The natural constructor of the loading bar.
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="line">The line of the loading bar.</param>
79
    /// <param name="additionalDuration">The duration of the loading bar after the process.</param>
80
    /// <remarks>
81
    /// For more information, refer to the following resources:
82
    /// <list type="bullet">
83
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
84
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
85
    /// </list>
86
    /// </remarks>
87
    public LoadingBar(
48✔
88
        string text,
48✔
89
        ref float progress,
48✔
90
        Placement placement = Placement.TopCenter,
48✔
91
        int? line = null,
48✔
92
        int additionalDuration = 1000
48✔
93
    )
48✔
94
    {
95
        if(Console.WindowWidth -1 <0){
48!
96
            _text = text[..Math.Min(text.Length, 0)];
48✔
97
        }
98
        else{
99
            _text = text[..Math.Min(text.Length, Console.WindowWidth - 1)];
×
100
        }
101
        _progress = progress;
45✔
102
        _placement = placement;
45✔
103
        _line = Window.CheckLine(line) ?? Window.GetLineAvailable(placement);
45!
104
        _additionalDuration = additionalDuration;
39✔
105
    }
39✔
106
    #endregion
107

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

125
    /// <summary>
126
    /// This method is used to update the progress of the loading bar.
127
    /// </summary>
128
    /// <param name="progress">The new progress of the loading bar.</param>
129
    /// <remarks>
130
    /// For more information, refer to the following resources:
131
    /// <list type="bullet">
132
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
133
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/Program.cs">Example Project</a></description></item>
134
    /// </list>
135
    /// </remarks>
136
    public void UpdateProgress(float progress)
137
    {
138
        _progress = progress;
9✔
139
    }
9✔
140

141
    /// <summary>
142
    /// This method is used to draw the loading bar on the console.
143
    /// </summary>
144
    [Visual]
145
    protected override void RenderElementActions()
146
    {
147
        void BuildBar(string text, float progress, int line)
148
        {
149
            StringBuilder loadingBar = new();
×
150
            for (int j = 0; j <= (int)(text.Length * progress); j++)
×
151
            {
152
                loadingBar.Append(LOADING_BAR_CHAR);
×
153
            }
154
            Core.WritePositionedString(
×
155
                loadingBar.ToString().ResizeString(text.Length, TextAlignment.Left),
×
156
                _placement.ToTextAlignment(),
×
157
                false,
×
158
                line + 2,
×
159
                false
×
160
            );
×
161
        }
×
162

163
        Core.WritePositionedString(_text, _placement.ToTextAlignment(), false, _line, false);
164
        while (_progress < MAX_PROGRESS)
165
        {
166
            BuildBar(_text, _progress, _line);
167
        }
168
        BuildBar(_text, MAX_PROGRESS, _line);
169
        Thread.Sleep(_additionalDuration);
170
        _progress = MIN_PROGRESS;
171
        Window.DeactivateElement(this);
172
    }
173
    #endregion
174
}
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