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

MorganKryze / ConsoleAppVisuals / 8119668611

02 Mar 2024 02:13AM UTC 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

44.44
/src/ConsoleAppVisuals/elements/interactive_elements/Prompt.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 prompt element 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 Prompt : InteractiveElement<string>
18
{
19
    #region Fields
20
    private string _question;
21
    private string _defaultValue;
22
    private Placement _placement;
23
    private int _maxLength;
24
    #endregion
25

26
    #region Constants
27
    private const int PROMPT_HEIGHT = 3;
28
    private const int PROMPT_LEFT_MARGIN = 4;
29
    #endregion
30

31
    #region Properties
32
    /// <summary>
33
    /// The placement of the prompt element.
34
    /// </summary>
35
    public override Placement Placement => _placement;
9✔
36

37
    /// <summary>
38
    /// The height of the prompt element.
39
    /// </summary>
40
    public override int Height => PROMPT_HEIGHT;
9✔
41

42
    /// <summary>
43
    /// The width of the prompt element.
44
    /// </summary>
45
    public override int Width => Math.Max(_question.Length + 1, PROMPT_LEFT_MARGIN + _maxLength);
27✔
46

47
    /// <summary>
48
    /// The question of the prompt element.
49
    /// </summary>
50
    public string Question => _question;
×
51

52
    /// <summary>
53
    /// The default value of the response.
54
    /// </summary>
55
    public string DefaultValue => _defaultValue;
×
56

57
    /// <summary>
58
    /// The maximum length of the response.
59
    /// </summary>
60
    public int MaxLength => _maxLength;
×
61
    #endregion
62

63
    #region Constructor
64
    /// <summary>
65
    /// The natural constructor of the prompt element.
66
    /// </summary>
67
    /// <param name="question">The text on the left of the prompt element.</param>
68
    /// <param name="defaultValue">The text in the center of the prompt element.</param>
69
    /// <param name="placement">The placement of the prompt element.</param>
70
    /// <param name="maxLength">The maximum length of the response.</param>
71
    /// <remarks>
72
    /// For more information, refer to the following resources:
73
    /// <list type="bullet">
74
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
75
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
76
    /// </list>
77
    /// </remarks>
78
    public Prompt(
78✔
79
        string question,
78✔
80
        string? defaultValue = null,
78✔
81
        Placement placement = Placement.TopCenter,
78✔
82
        int maxLength = 30
78✔
83
    )
78✔
84
    {
85
        _question = question;
78✔
86
        _defaultValue = defaultValue ?? string.Empty;
78✔
87
        _placement = placement;
78✔
88
        _maxLength = maxLength;
78✔
89
    }
78✔
90
    #endregion
91

92
    #region Methods
93
    /// <summary>
94
    /// This method is used to update the question of the prompt element.
95
    /// </summary>
96
    /// <param name="question">The new question of the prompt element.</param>
97
    /// <remarks>
98
    /// For more information, refer to the following resources:
99
    /// <list type="bullet">
100
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
101
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
102
    /// </list>
103
    /// </remarks>
104
    public void UpdateQuestion(string question)
105
    {
106
        _question = question;
×
107
    }
×
108

109
    /// <summary>
110
    /// This method is used to update the default value of the prompt element.
111
    /// </summary>
112
    /// <param name="defaultValue">The new default value of the prompt element.</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/">Example Project</a></description></item>
118
    /// </list>
119
    /// </remarks>
120
    public void UpdateDefaultValue(string defaultValue)
121
    {
122
        _defaultValue = defaultValue;
×
123
    }
×
124

125
    /// <summary>
126
    /// This method is used to update the placement of the prompt element.
127
    /// </summary>
128
    /// <param name="placement">The new placement of the prompt element.</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/">Example Project</a></description></item>
134
    /// </list>
135
    /// </remarks>
136
    public void UpdatePlacement(Placement placement)
137
    {
138
        _placement = placement;
×
139
    }
×
140

141
    /// <summary>
142
    /// This method is used to update the maximum length of the response.
143
    /// </summary>
144
    /// <param name="maxLength">The new maximum length of the response.</param>
145
    /// <exception cref="ArgumentOutOfRangeException">The maximum length of the response must be greater than 0 and less than the width of the console window.</exception>
146
    /// <remarks>
147
    /// For more information, refer to the following resources:
148
    /// <list type="bullet">
149
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
150
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
151
    /// </list>
152
    /// </remarks>
153
    public void UpdateMaxLength(int maxLength)
154
    {
155
        if (maxLength < 1 || maxLength > Console.WindowWidth)
×
156
            throw new ArgumentOutOfRangeException(
×
157
                nameof(maxLength),
×
158
                "The maximum length of the response must be greater than 0 and less than the width of the console window."
×
159
            );
×
160
        _maxLength = maxLength;
×
161
    }
×
162

163
    /// <summary>
164
    /// This method is used to render the prompt element on the console.
165
    /// </summary>
166
    [Visual]
167
    protected override void RenderElementActions()
168
    {
169
        Core.WriteContinuousString(
170
            _question,
171
            Line,
172
            false,
173
            1500,
174
            50,
175
            -1,
176
            _placement.ToTextAlignment()
177
        );
178
        var field = new StringBuilder(_defaultValue);
179
        ConsoleKeyInfo key;
180
        do
181
        {
182
            Console.CursorVisible = false;
183
            Core.WritePositionedString(GetRenderSpace()[0], TextAlignment.Center, false, Line + 2);
184
            Console.SetCursorPosition(0, Console.CursorTop);
185
            Console.Write("{0," + (Console.WindowWidth / 2 - _question.Length / 2 + 2) + "}", "> ");
186
            Console.Write($"{field}");
187
            Console.CursorVisible = true;
188
            key = Console.ReadKey();
189
            if (key.Key == ConsoleKey.Backspace && field.Length > 0)
190
                field.Remove(field.Length - 1, 1);
191
            else if (key.Key != ConsoleKey.Enter && key.Key != ConsoleKey.Escape)
192
                field.Append(key.KeyChar);
193
        } while (key.Key != ConsoleKey.Enter && key.Key != ConsoleKey.Escape);
194
        Console.CursorVisible = false;
195
        SendResponse(
196
            this,
197
            new InteractionEventArgs<string>(
198
                key.Key == ConsoleKey.Enter ? Output.Selected : Output.Escaped,
199
                field.ToString()
200
            )
201
        );
202
    }
203
    #endregion
204
}
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