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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
//! ref: composer/vendor/symfony/console/Helper/SymfonyQuestionHelper.php
use crate::symfony::console::formatter::output_formatter::OutputFormatter;
use crate::symfony::console::helper::question_helper::QuestionHelper;
use crate::symfony::console::output::output_interface;
use crate::symfony::console::output::output_interface::OutputInterface;
use crate::symfony::console::question::choice_question::ChoiceQuestion;
use crate::symfony::console::question::confirmation_question::ConfirmationQuestion;
use crate::symfony::console::question::question::Question;
use crate::symfony::console::style::symfony_style::SymfonyStyle;
use shirabe_php_shim::AsAny;
use shirabe_php_shim::PhpMixed;
use std::cell::RefCell;
use std::ops::{Deref, DerefMut};
use std::rc::Rc;
/// Symfony Style Guide compliant question helper.
#[derive(Debug, Default)]
pub struct SymfonyQuestionHelper {
inner: QuestionHelper,
}
impl SymfonyQuestionHelper {
pub fn new() -> Self {
Self::default()
}
pub(crate) fn write_prompt(
&self,
output: Rc<RefCell<dyn OutputInterface>>,
question: &Question,
) {
let mut text = OutputFormatter::escape_trailing_backslash(question.get_question());
let default = question.get_default();
if question.is_multiline() {
text += &format!(
" (press {} to continue)",
PhpMixed::String(self.get_eof_shortcut()),
);
}
// switch (true)
if matches!(default, PhpMixed::Null) {
text = format!(" <info>{}</info>:", PhpMixed::String(text));
} else if question
.as_any()
.downcast_ref::<ConfirmationQuestion>()
.is_some()
{
text = format!(
" <info>{} (yes/no)</info> [<comment>{}</comment>]:",
PhpMixed::String(text),
PhpMixed::String(
if shirabe_php_shim::boolval(&default) {
"yes"
} else {
"no"
}
.to_string(),
),
);
} else if let Some(choice_question) = question
.as_any()
.downcast_ref::<ChoiceQuestion>()
.filter(|q| q.is_multiselect())
{
let choices = choice_question.get_choices();
let default_parts = shirabe_php_shim::explode(",", &default.to_string());
let resolved: Vec<String> = default_parts
.iter()
.map(|value| {
choices
.get(&shirabe_php_shim::trim(value, None))
.map(|v| v.to_string())
.unwrap()
})
.collect();
text = format!(
" <info>{}</info> [<comment>{}</comment>]:",
PhpMixed::String(text),
PhpMixed::String(OutputFormatter::escape(&resolved.join(", ")).unwrap()),
);
} else if let Some(choice_question) = question.as_any().downcast_ref::<ChoiceQuestion>() {
let choices = choice_question.get_choices();
text = format!(
" <info>{}</info> [<comment>{}</comment>]:",
PhpMixed::String(text),
PhpMixed::String(
OutputFormatter::escape(
&choices
.get(&default.to_string())
.cloned()
.unwrap_or(default.clone())
.to_string(),
)
.unwrap(),
),
);
} else {
text = format!(
" <info>{}</info> [<comment>{}</comment>]:",
PhpMixed::String(text),
PhpMixed::String(OutputFormatter::escape(&default.to_string()).unwrap()),
);
}
output
.borrow()
.writeln(&[text], output_interface::OUTPUT_NORMAL);
let mut prompt = " > ".to_string();
if let Some(choice_question) = question.as_any().downcast_ref::<ChoiceQuestion>() {
output.borrow().writeln(
&self
.inner
.format_choice_question_choices(choice_question, "comment"),
output_interface::OUTPUT_NORMAL,
);
prompt = choice_question.get_prompt().to_string();
}
output
.borrow()
.write(&[prompt], false, output_interface::OUTPUT_NORMAL);
}
pub(crate) fn write_error(
&self,
output: Rc<RefCell<dyn OutputInterface>>,
error: &shirabe_php_shim::Exception,
) {
let is_symfony_style = {
let borrowed = output.borrow();
(*borrowed)
.as_any()
.downcast_ref::<SymfonyStyle>()
.is_some()
};
if is_symfony_style {
// $output->newLine(); $output->error($error->getMessage());
// SymfonyStyle's newLine()/error() require mutable access to the
// concrete type; mutable downcasting through the trait object is
// resolved in a later phase.
todo!("SymfonyStyle newLine()/error() require &mut SymfonyStyle");
}
self.inner.write_error(output, error);
}
fn get_eof_shortcut(&self) -> String {
if shirabe_php_shim::php_os_family() == "Windows" {
return "<comment>Ctrl+Z</comment> then <comment>Enter</comment>".to_string();
}
"<comment>Ctrl+D</comment>".to_string()
}
}
impl Deref for SymfonyQuestionHelper {
type Target = QuestionHelper;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl DerefMut for SymfonyQuestionHelper {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}
|