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

MorganKryze / ConsoleAppVisuals / 7993638304

pending completion
7993638304

push

github

MorganKryze
📖 add files and contributing recommendation

827 of 916 branches covered (90.28%)

Branch coverage included in aggregate %.

1676 of 1694 relevant lines covered (98.94%)

104.0 hits per line

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

93.33
/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;
6✔
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

52
    /// <summary>
53
    /// The placement of the loading bar.
54
    /// </summary>
55
    public override Placement Placement => _placement;
12✔
56
    #endregion
57

58
    #region Getters & Setters
59

60
    /// <summary>
61
    /// Getters and setters of the text of the loading bar.
62
    /// </summary>
63
    public string Text => _text;
30✔
64

65
    /// <summary>
66
    /// Getters and setters of the progress of the loading bar.
67
    /// </summary>
68
    public float Progress => _progress;
21✔
69

70
    /// <summary>
71
    /// Getters of the additional duration of the loading bar at the end.
72
    /// </summary>
73
    public int AdditionalDuration => _additionalDuration;
6✔
74
    #endregion
75

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

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

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

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

170
        Core.WritePositionedString(_text, _placement.ToTextAlignment(), false, _line, false);
171
        while (_progress < MAX_PROGRESS)
172
        {
173
            BuildBar(_text, _progress, _line);
174
        }
175
        BuildBar(_text, MAX_PROGRESS, _line);
176
        Thread.Sleep(_additionalDuration);
177
        _progress = MIN_PROGRESS;
178
        Window.DeactivateElement(this);
179
    }
180
    #endregion
181
}
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

© 2025 Coveralls, Inc