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

MorganKryze / ConsoleAppVisuals / 8471182871

28 Mar 2024 05:20PM UTC coverage: 96.244%. Remained the same
8471182871

push

github

MorganKryze
🚑 (CD) fix regex to accept -beta.1 format

946 of 1056 branches covered (89.58%)

Branch coverage included in aggregate %.

2129 of 2139 relevant lines covered (99.53%)

249.89 hits per line

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

95.74
/src/ConsoleAppVisuals/elements/animated_elements/FakeLoadingBar.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="FakeLoadingBar"/> is an animated element that simulates a loading bar with a fixed duration.
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 FakeLoadingBar : AnimatedElement
14
{
15
    #region Constants
16
    const char LOADING_BAR_CHAR = 'â–ˆ';
17
    const int DEFAULT_HEIGHT = 3;
18
    const string DEFAULT_TEXT = "[ Loading ...]";
19
    const Placement DEFAULT_PLACEMENT = Placement.TopCenter;
20
    const int DEFAULT_PROCESS_DURATION = 2000;
21
    const int DEFAULT_ADDITIONAL_DURATION = 1000;
22
    #endregion
23

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

31
    #region Default Properties
32
    /// <summary>
33
    /// Gets the height of the loading bar.
34
    /// </summary>
35
    public override int Height => DEFAULT_HEIGHT;
1✔
36

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

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

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

54
    /// <summary>
55
    /// Gets the duration of the loading bar.
56
    /// </summary>
57
    public int ProcessDuration => _processDuration;
2✔
58

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

65
    #region Constructor
66
    /// <summary>
67
    /// The <see cref="FakeLoadingBar"/> is an animated element that simulates a loading bar with a fixed duration.
68
    /// </summary>
69
    /// <param name="text">The text of the loading bar.</param>
70
    /// <param name="placement">The placement of the loading bar.</param>
71
    /// <param name="processDuration">The duration of the loading bar.</param>
72
    /// <param name="additionalDuration">The additional duration of the loading bar at the end.</param>
73
    /// <remarks>
74
    /// For more information, consider visiting the documentation available <a href="https://morgankryze.github.io/ConsoleAppVisuals/">here</a>.
75
    /// </remarks>
76
    public FakeLoadingBar(
25✔
77
        string text = DEFAULT_TEXT,
25✔
78
        Placement placement = DEFAULT_PLACEMENT,
25✔
79
        int processDuration = DEFAULT_PROCESS_DURATION,
25✔
80
        int additionalDuration = DEFAULT_ADDITIONAL_DURATION
25✔
81
    )
25✔
82
    {
83
        if (Console.WindowWidth - 1 >= 0)
25!
84
        {
85
            _text = text[..Math.Min(text.Length, Console.WindowWidth - 1)];
×
86
        }
87
        else
88
        {
89
            _text = text[..Math.Min(text.Length, 0)];
25✔
90
        }
91
        _placement = placement;
25✔
92
        _processDuration = processDuration;
25✔
93
        _additionalDuration = additionalDuration;
25✔
94
    }
25✔
95
    #endregion
96

97
    #region Update Methods
98
    /// <summary>
99
    /// Updates the text of the loading bar.
100
    /// </summary>
101
    /// <param name="text">The new text of the loading bar.</param>
102
    /// <remarks>
103
    /// For more information, consider visiting the documentation available <a href="https://morgankryze.github.io/ConsoleAppVisuals/">here</a>.
104
    /// </remarks>
105
    public void UpdateText(string text)
106
    {
107
        if (text.Length == 0)
5✔
108
        {
109
            throw new ArgumentException("The text cannot be empty.", nameof(text));
1✔
110
        }
111
        _text = text;
4✔
112
    }
4✔
113

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

126
    /// <summary>
127
    /// Updates the duration of the loading bar.
128
    /// </summary>
129
    /// <param name="processDuration">The new duration of the loading bar.</param>
130
    /// <exception cref="ArgumentOutOfRangeException">Throw when the process duration is negative.</exception>
131
    /// <remarks>
132
    /// For more information, consider visiting the documentation available <a href="https://morgankryze.github.io/ConsoleAppVisuals/">here</a>.
133
    /// </remarks>
134
    public void UpdateProcessDuration(int processDuration)
135
    {
136
        if (processDuration < 0)
2✔
137
        {
138
            throw new ArgumentOutOfRangeException(
1✔
139
                nameof(processDuration),
1✔
140
                "The process duration must be greater than or equal to 0."
1✔
141
            );
1✔
142
        }
143
        _processDuration = processDuration;
1✔
144
    }
1✔
145

146
    /// <summary>
147
    /// Updates the additional duration of the loading bar.
148
    /// </summary>
149
    /// <param name="additionalDuration">The new additional duration of the loading bar.</param>
150
    /// <exception cref="ArgumentOutOfRangeException">The additional duration of the loading bar cannot be negative.</exception>
151
    /// <remarks>
152
    /// For more information, consider visiting the documentation available <a href="https://morgankryze.github.io/ConsoleAppVisuals/">here</a>.
153
    /// </remarks>
154
    public void UpdateAdditionalDuration(int additionalDuration)
155
    {
156
        if (additionalDuration < 0)
2✔
157
        {
158
            throw new ArgumentOutOfRangeException(
1✔
159
                nameof(additionalDuration),
1✔
160
                "The additional duration must be greater than or equal to 0."
1✔
161
            );
1✔
162
        }
163
        _additionalDuration = additionalDuration;
1✔
164
    }
1✔
165
    #endregion
166

167
    #region Rendering
168
    /// <summary>
169
    /// Defines the actions to perform when the element is called to be rendered on the console.
170
    /// </summary>
171
    [Visual]
172
    protected override void RenderElementActions()
173
    {
174
        Core.WritePositionedString(_text, _placement, false, Line, false);
175
        StringBuilder loadingBar = new();
176
        for (int j = 0; j < _text.Length; j++)
177
        {
178
            loadingBar.Append(LOADING_BAR_CHAR);
179
        }
180
        Core.WriteContinuousString(
181
            loadingBar.ToString(),
182
            Line + 2,
183
            false,
184
            _processDuration,
185
            _additionalDuration,
186
            Width,
187
            _placement.ToTextAlignment()
188
        );
189
    }
190
    #endregion
191
}
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