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

MorganKryze / ConsoleAppVisuals / 7972444880

pending completion
7972444880

push

github

MorganKryze
📖 add legacy docs

827 of 916 branches covered (90.28%)

Branch coverage included in aggregate %.

1676 of 1694 relevant lines covered (98.94%)

105.1 hits per line

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

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

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

31
    #region Properties
32
    /// <summary>
33
    /// The line of the loading bar in the console.
34
    /// </summary>
35
    /// <remarks>We add 2 because so the loading bar does not overlap with the title.</remarks>
36
    [Visual]
37
    public override int Line => _line;
38

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

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

51
    #region Getters and Setters
52
    
53
    /// <summary>
54
    /// Getter and setter of the text of the loading bar.
55
    /// </summary>
56
    public string Text { get => _text; set => _text = value; }
18✔
57

58
    /// <summary>
59
    /// Getter of the placement of the loading bar.
60
    /// </summary>
61
    public override Placement Placement {get => _placement;}
3✔
62

63
    /// <summary>
64
    /// Getter of the duration of the loading bar.
65
    /// </summary>
66
    public int ProcessDuration {get => _processDuration;}
3✔
67

68
    /// <summary>
69
    /// Getter of the additional duration of the loading bar at the end.
70
    /// </summary>
71
    public int AdditionalDuration {get => _additionalDuration;}
3✔
72

73
    #endregion
74

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

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

129
    /// <summary>
130
    /// This method is used to draw the loading bar on the console.
131
    /// </summary>
132
    
133
    [Visual]
134
    protected override void RenderElementActions()
135
    {
136
        Core.WritePositionedString(_text, _placement.ToTextAlignment(), false, _line, false);
137
        StringBuilder loadingBar = new();
138
        for (int j = 0; j < _text.Length; j++)
139
        {
140
            loadingBar.Append(LOADING_BAR_CHAR);
141
        }
142
        Core.WriteContinuousString(
143
            loadingBar.ToString(),
144
            _line + 2,
145
            false,
146
            _processDuration,
147
            _additionalDuration,
148
            Width,
149
            default,
150
            false
151
        );
152
        Window.DeactivateElement(this);
153
    }
154
    #endregion
155
}
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