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

MorganKryze / ConsoleAppVisuals / 8114215569

01 Mar 2024 04:29PM UTC coverage: 85.767% (-9.3%) from 95.093%
8114215569

push

github

MorganKryze
🤖 moved to publish

865 of 1072 branches covered (80.69%)

Branch coverage included in aggregate %.

1708 of 1928 relevant lines covered (88.59%)

273.5 hits per line

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

63.64
/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
    #endregion
24

25
    #region Properties
26
    /// <summary>
27
    /// The placement of the prompt element.
28
    /// </summary>
29
    public override Placement Placement => _placement;
9✔
30

31
    /// <summary>
32
    /// The height of the prompt element.
33
    /// </summary>
34
    public override int Height => 3;
9✔
35

36
    /// <summary>
37
    /// The width of the prompt element.
38
    /// </summary>
39
    /// <remarks>We add a margin of 2 to be sur to take in account odd question lengths.</remarks>
40
    public override int Width => _question.Length + 2;
27✔
41

42
    /// <summary>
43
    /// The question of the prompt element.
44
    /// </summary>
45
    public string Question => _question;
×
46

47
    /// <summary>
48
    /// The default value of the response.
49
    /// </summary>
50
    public string DefaultValue => _defaultValue;
×
51
    #endregion
52

53
    #region Constructor
54
    /// <summary>
55
    /// The natural constructor of the prompt element.
56
    /// </summary>
57
    /// <param name="question">The text on the left of the prompt element.</param>
58
    /// <param name="defaultValue">The text in the center of the prompt element.</param>
59
    /// <param name="placement">The placement of the prompt element.</param>
60
    /// <remarks>
61
    /// For more information, refer to the following resources:
62
    /// <list type="bullet">
63
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
64
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
65
    /// </list>
66
    /// </remarks>
67
    public Prompt(
78✔
68
        string question,
78✔
69
        string? defaultValue = null,
78✔
70
        Placement placement = Placement.TopCenter
78✔
71
    )
78✔
72
    {
73
        _question = question;
78✔
74
        _defaultValue = defaultValue ?? string.Empty;
78✔
75
        _placement = placement;
78✔
76
    }
78✔
77
    #endregion
78

79
    #region Methods
80
    /// <summary>
81
    /// This method is used to update the question of the prompt element.
82
    /// </summary>
83
    /// <param name="question">The new question of the prompt element.</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/">Example Project</a></description></item>
89
    /// </list>
90
    /// </remarks>
91
    public void UpdateQuestion(string question)
92
    {
93
        _question = question;
×
94
    }
×
95

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

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

128
    /// <summary>
129
    /// This method is used to render the prompt element on the console.
130
    /// </summary>
131
    [Visual]
132
    protected override void RenderElementActions()
133
    {
134
        Core.WriteContinuousString(
135
            _question,
136
            Line,
137
            false,
138
            1500,
139
            50,
140
            -1,
141
            _placement.ToTextAlignment()
142
        );
143
        var field = new StringBuilder(_defaultValue);
144
        ConsoleKeyInfo key;
145
        do
146
        {
147
            Console.CursorVisible = false;
148
            Core.WritePositionedString(GetRenderSpace()[0], TextAlignment.Center, false, Line + 2);
149
            Console.SetCursorPosition(0, Console.CursorTop);
150
            Console.Write("{0," + (Console.WindowWidth / 2 - _question.Length / 2 + 2) + "}", "> ");
151
            Console.Write($"{field}");
152
            Console.CursorVisible = true;
153
            key = Console.ReadKey();
154
            if (key.Key == ConsoleKey.Backspace && field.Length > 0)
155
                field.Remove(field.Length - 1, 1);
156
            else if (key.Key != ConsoleKey.Enter && key.Key != ConsoleKey.Escape)
157
                field.Append(key.KeyChar);
158
        } while (key.Key != ConsoleKey.Enter && key.Key != ConsoleKey.Escape);
159
        Console.CursorVisible = false;
160
        SendResponse(
161
            this,
162
            new InteractionEventArgs<string>(
163
                key.Key == ConsoleKey.Enter ? Output.Selected : Output.Escaped,
164
                field.ToString()
165
            )
166
        );
167
    }
168
    #endregion
169
}
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

© 2026 Coveralls, Inc