@@ -144,8 +144,8 @@ mod macros {
144144 #[ macro_export( local_inner_macros) ]
145145 /// # Confirm.
146146 ///
147- /// This is a convenience macro for generating a confirmation message,
148- /// handling the prompting , and returning the response `bool`.
147+ /// This convenience macro prints a message, prompts the user for a
148+ /// yes/no response , and interprets/returns that value as a `bool`.
149149 ///
150150 /// ## Example
151151 ///
@@ -161,47 +161,125 @@ mod macros {
161161 /// if confirm!("Do you like chickens?") {
162162 /// println!("That's great! They like you too!");
163163 /// }
164+ /// ```
165+ ///
166+ /// The following modifiers are supported:
167+ /// * `@indent $literal`: indent the message `$literal` "tabs";
168+ /// * `@stderr`: pop the question over STDERR (instead of STDOUT);
169+ /// * `@yes`: default to "Y" (instead of "N");
170+ ///
171+ /// ```no_run
172+ /// # use fyi_msg::confirm;
173+ /// // Indent one "tabs" (four spaces):
174+ /// if confirm!(@indent 1 "Do you like chickens?") {
175+ /// println!(" That's great! They like you too!");
176+ /// }
164177 ///
165- /// // If you want to default to yes, prefix thusly :
166- /// if confirm!(yes: "Do you like chickens?") {
178+ /// // Print to STDERR instead of STDOUT :
179+ /// if confirm!(@stderr "Do you like chickens?") {
167180 /// println!("That's great! They like you too!");
168181 /// }
169182 ///
170- /// // Indentation can be set with the macro too by appending a second
171- /// // argument:
172- /// if confirm!("Do you like chickens?", 1) {
173- /// println!(" That's great! They like you too!");
183+ /// // Default to yes instead of no.
184+ /// if confirm!(@yes "Do you like chickens?") {
185+ /// println!("That's great! They like you too!");
174186 /// }
187+ /// ```
175188 ///
176- /// // The "yes:" prefix also works here.
177- /// if confirm!(yes: "Do you like chickens?", 1) {
178- /// println!(" That's great! They like you too!");
189+ /// Modifiers can be stacked together any which way.
190+ ///
191+ /// ```no_run
192+ /// # use fyi_msg::confirm;
193+ /// // Indent three "tabs" _and_ print to STDERR _and_ default to yes:
194+ /// if confirm!(@indent 3 @stderr @yes "Do you like chickens?") {
195+ /// println!(" That's great! They like you too!");
179196 /// }
197+ ///
198+ /// // Same as above.
199+ /// if confirm!(@stderr @indent 3 @yes "Do you like chickens?") {
200+ /// println!(" That's great! They like you too!");
201+ /// }
202+ ///
203+ /// // Same again.
204+ /// if confirm!(@stderr @yes @indent 3 "Do you like chickens?") {
205+ /// println!(" That's great! They like you too!");
206+ /// }
207+ ///
208+ /// // …
180209 /// ```
181210 macro_rules! confirm {
182- ( yes: $text: expr) => (
183- $crate:: Msg :: new( $crate:: MsgKind :: Confirm , $text) . prompt_with_default( true )
211+ // Maybe-indent.
212+ ( $( @indent $indent: literal ) ? $text: expr) => (
213+ $crate:: Msg :: new( $crate:: MsgKind :: Confirm , $text)
214+ $( . with_indent( $indent) ) ?
215+ . prompt( )
184216 ) ;
185- ( yes: $text: expr, $indent: expr) => (
217+
218+ // Maybe-indent, yes.
219+ ( $( @indent $indent: literal ) ? @yes $text: expr) => (
186220 $crate:: Msg :: new( $crate:: MsgKind :: Confirm , $text)
187- . with_indent( $indent)
221+ $ ( . with_indent( $indent) ) ?
188222 . prompt_with_default( true )
189223 ) ;
190- ( no: $text: expr) => (
191- $crate:: Msg :: new( $crate:: MsgKind :: Confirm , $text) . prompt( )
224+ // Maybe-indent, yes.
225+ ( @yes $( @indent $indent: literal ) ? $text: expr) => (
226+ $crate:: Msg :: new( $crate:: MsgKind :: Confirm , $text)
227+ $( . with_indent( $indent) ) ?
228+ . prompt_with_default( true )
192229 ) ;
193- ( no: $text: expr, $indent: expr) => (
230+
231+ // Maybe-indent, STDERR.
232+ ( $( @indent $indent: literal ) ? @stderr $text: expr) => (
233+ $crate:: Msg :: new( $crate:: MsgKind :: Confirm , $text)
234+ $( . with_indent( $indent) ) ?
235+ . eprompt( )
236+ ) ;
237+ ( @stderr $( @indent $indent: literal ) ? $text: expr) => (
238+ $crate:: Msg :: new( $crate:: MsgKind :: Confirm , $text)
239+ $( . with_indent( $indent) ) ?
240+ . eprompt( )
241+ ) ;
242+
243+ // STDERR, yes.
244+ ( @stderr @yes $text: expr) => (
245+ $crate:: Msg :: new( $crate:: MsgKind :: Confirm , $text)
246+ . eprompt_with_default( true )
247+ ) ;
248+ ( @yes @stderr $text: expr) => (
249+ $crate:: Msg :: new( $crate:: MsgKind :: Confirm , $text)
250+ . eprompt_with_default( true )
251+ ) ;
252+
253+ // Indent, STDERR, yes.
254+ ( @indent $indent: literal @stderr @yes $text: expr) => (
194255 $crate:: Msg :: new( $crate:: MsgKind :: Confirm , $text)
195256 . with_indent( $indent)
196- . prompt ( )
257+ . eprompt_with_default ( true )
197258 ) ;
198- ( $text: expr) => (
199- $crate:: Msg :: new( $crate:: MsgKind :: Confirm , $text) . prompt( )
259+ ( @indent $indent: literal @yes @stderr $text: expr) => (
260+ $crate:: Msg :: new( $crate:: MsgKind :: Confirm , $text)
261+ . with_indent( $indent)
262+ . eprompt_with_default( true )
200263 ) ;
201- ( $text : expr , $indent : expr) => (
264+ ( @stderr @indent $indent : literal @yes $text : expr) => (
202265 $crate:: Msg :: new( $crate:: MsgKind :: Confirm , $text)
203266 . with_indent( $indent)
204- . prompt( )
267+ . eprompt_with_default( true )
268+ ) ;
269+ ( @stderr @yes @indent $indent: literal $text: expr) => (
270+ $crate:: Msg :: new( $crate:: MsgKind :: Confirm , $text)
271+ . with_indent( $indent)
272+ . eprompt_with_default( true )
273+ ) ;
274+ ( @yes @indent $indent: literal @stderr $text: expr) => (
275+ $crate:: Msg :: new( $crate:: MsgKind :: Confirm , $text)
276+ . with_indent( $indent)
277+ . eprompt_with_default( true )
278+ ) ;
279+ ( @yes @stderr @indent $indent: literal $text: expr) => (
280+ $crate:: Msg :: new( $crate:: MsgKind :: Confirm , $text)
281+ . with_indent( $indent)
282+ . eprompt_with_default( true )
205283 ) ;
206284 }
207285}
0 commit comments