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

MorganKryze / ConsoleAppVisuals / 8119668611

02 Mar 2024 02:13AM CUT coverage: 85.188% (-9.9%) from 95.086%
8119668611

push

github

MorganKryze
🤖 update action to node 20

866 of 1080 branches covered (80.19%)

Branch coverage included in aggregate %.

1699 of 1931 relevant lines covered (87.99%)

272.46 hits per line

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

61.76
/src/ConsoleAppVisuals/elements/static_elements/FakeLoadingBar.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 FakeLoadingBar : Element
18
{
19
    #region Fields
20
    private string _text;
21
    private Placement _placement;
22
    private readonly int _processDuration;
23
    private int _additionalDuration;
24
    #endregion
25

26
    #region Constants
27
    private const char LOADING_BAR_CHAR = 'â–ˆ';
28
    #endregion
29

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

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

42
    /// <summary>
43
    /// The text of the loading bar.
44
    /// </summary>
45
    public string Text => _text;
12✔
46

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

52
    /// <summary>
53
    /// Getter of the duration of the loading bar.
54
    /// </summary>
55
    public int ProcessDuration => _processDuration;
3✔
56

57
    /// <summary>
58
    /// Getter of the additional duration of the loading bar at the end.
59
    /// </summary>
60
    public int AdditionalDuration => _additionalDuration;
3✔
61

62
    #endregion
63

64
    #region Constructor
65
    /// <summary>
66
    /// The natural constructor of the loading bar.
67
    /// </summary>
68
    /// <param name="text">The text of the loading bar.</param>
69
    /// <param name="placement">The placement of the loading bar.</param>
70
    /// <param name="processDuration">The duration of the loading bar.</param>
71
    /// <param name="additionalDuration">The additional duration of the loading bar at the end.</param>
72
    /// <remarks>
73
    /// For more information, refer to the following resources:
74
    /// <list type="bullet">
75
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
76
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
77
    /// </list>
78
    /// </remarks>
79
    public FakeLoadingBar(
39✔
80
        string text = "[ Loading ...]",
39✔
81
        Placement placement = Placement.TopCenter,
39✔
82
        int processDuration = 2000,
39✔
83
        int additionalDuration = 1000
39✔
84
    )
39✔
85
    {
86
        if (Console.WindowWidth - 1 >= 0)
39!
87
        {
88
            _text = text[..Math.Min(text.Length, Console.WindowWidth - 1)];
×
89
        }
90
        else
91
        {
92
            _text = text[..Math.Min(text.Length, 0)];
39✔
93
        }
94
        _placement = placement;
39✔
95
        _processDuration = processDuration;
39✔
96
        _additionalDuration = additionalDuration;
39✔
97
    }
39✔
98
    #endregion
99

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

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

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

157
    /// <summary>
158
    /// This method is used to draw the loading bar on the console.
159
    /// </summary>
160

161
    [Visual]
162
    protected override void RenderElementActions()
163
    {
164
        Core.WritePositionedString(_text, _placement.ToTextAlignment(), false, Line, false);
165
        StringBuilder loadingBar = new();
166
        for (int j = 0; j < _text.Length; j++)
167
        {
168
            loadingBar.Append(LOADING_BAR_CHAR);
169
        }
170
        Core.WriteContinuousString(
171
            loadingBar.ToString(),
172
            Line + 2,
173
            false,
174
            _processDuration,
175
            _additionalDuration,
176
            Width,
177
            default,
178
            false
179
        );
180
        Window.DeactivateElement(this);
181
    }
182
    #endregion
183
}
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