forked from chocolatey/choco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInteractivePrompt.cs
144 lines (126 loc) · 5.94 KB
/
InteractivePrompt.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright © 2011 - Present RealDimensions Software, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
//
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
namespace chocolatey.infrastructure.commandline
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using adapters;
using guards;
using logging;
using Console = adapters.Console;
public class InteractivePrompt
{
private static Lazy<IConsole> _console = new Lazy<IConsole>(() => new Console());
[EditorBrowsable(EditorBrowsableState.Never)]
public static void initialize_with(Lazy<IConsole> console)
{
_console = console;
}
private static IConsole Console
{
get { return _console.Value; }
}
public static string prompt_for_confirmation_short(string prompt, IEnumerable<string> choices, int repeat = 10)
{
if (repeat < 0) throw new ApplicationException("Too many bad attempts. Stopping before application crash.");
Ensure.that(() => prompt).is_not_null();
Ensure.that(() => choices).is_not_null();
Ensure
.that(() => choices)
.meets(
c => c.Any(),
(name, value) => { throw new ApplicationException("No choices passed in. Please ensure you pass choices."); });
Ensure
.that(() => choices)
.meets(
c => c.Select(entry => entry.FirstOrDefault()).Distinct().Count() == c.Count(),
(name, value) => { throw new ApplicationException("Multiple choices have the same first letter. Please ensure you pass choices with different first letters."); });
"chocolatey".Log().Info(ChocolateyLoggers.Important, "{0} ({1}): ".format_with(prompt, String.Join("/", choices)));
var selection = Console.ReadLine();
// check to see if value was passed
foreach (var choice in choices)
{
if (choice.is_equal_to(selection) || choice.FirstOrDefault().ToString().is_equal_to(selection))
{
selection = choice;
return selection;
}
}
"chocolatey".Log().Error(ChocolateyLoggers.Important, "Your choice of '{0}' is not a valid selection.".format_with(selection));
return prompt_for_confirmation_short(prompt, choices, repeat - 1);
}
public static string prompt_for_confirmation(string prompt, IEnumerable<string> choices, string defaultChoice, bool requireAnswer, int repeat = 10)
{
if (repeat < 0) throw new ApplicationException("Too many bad attempts. Stopping before application crash.");
Ensure.that(() => prompt).is_not_null();
Ensure.that(() => choices).is_not_null();
Ensure
.that(() => choices)
.meets(
c => c.Count() > 0,
(name, value) => { throw new ApplicationException("No choices passed in. Please ensure you pass choices"); });
if (defaultChoice != null)
{
Ensure
.that(() => choices)
.meets(
c => c.Contains(defaultChoice),
(name, value) => { throw new ApplicationException("Default choice value must be one of the given choices."); });
}
"chocolatey".Log().Info(ChocolateyLoggers.Important, prompt);
int counter = 1;
IDictionary<int, string> choiceDictionary = new Dictionary<int, string>();
foreach (var choice in choices.or_empty_list_if_null())
{
choiceDictionary.Add(counter, choice);
"chocolatey".Log().Info(" {0}) {1}{2}".format_with(counter, choice.to_string(), choice == defaultChoice ? " [Default - Press Enter]" : ""));
counter++;
}
var selection = Console.ReadLine();
if (string.IsNullOrWhiteSpace(selection) && defaultChoice != null)
{
return defaultChoice;
}
int selected = -1;
if (!int.TryParse(selection, out selected) || selected <= 0 || selected > (counter - 1))
{
// check to see if value was passed
var selectionFound = false;
foreach (var pair in choiceDictionary)
{
if (pair.Value.is_equal_to(selection))
{
selected = pair.Key;
selectionFound = true;
break;
}
}
if (!selectionFound)
{
"chocolatey".Log().Error(ChocolateyLoggers.Important, "Your choice of '{0}' is not a valid selection.".format_with(selection));
if (requireAnswer)
{
"chocolatey".Log().Warn(ChocolateyLoggers.Important, "You must select an answer");
return prompt_for_confirmation(prompt, choices, defaultChoice, requireAnswer, repeat - 1);
}
return null;
}
}
return choiceDictionary[selected];
}
}
}