88use PhpSchool \CliMenu \MenuItem \LineBreakItem ;
99use PhpSchool \CliMenu \MenuItem \MenuItemInterface ;
1010use PhpSchool \CliMenu \MenuItem \StaticItem ;
11+ use PhpSchool \CliMenu \Dialogue \Confirm ;
12+ use PhpSchool \CliMenu \Dialogue \Flash ;
1113use PhpSchool \CliMenu \Terminal \TerminalFactory ;
1214use PhpSchool \CliMenu \Terminal \TerminalInterface ;
1315use PhpSchool \CliMenu \Util \StringUtil as s ;
@@ -55,6 +57,11 @@ class CliMenu
5557 */
5658 protected $ parent ;
5759
60+ /**
61+ * @var Frame|null
62+ */
63+ private $ currentFrame ;
64+
5865 /**
5966 * @param string $title
6067 * @param array $items
@@ -258,28 +265,37 @@ protected function draw()
258265 $ this ->terminal ->clean ();
259266 $ this ->terminal ->moveCursorToTop ();
260267
261- echo "\n\n" ;
268+ $ frame = new Frame ;
269+
270+ $ frame ->newLine (2 );
262271
263272 if (is_string ($ this ->title )) {
264- $ this ->drawMenuItem (new LineBreakItem ());
265- $ this ->drawMenuItem (new StaticItem ($ this ->title ));
266- $ this ->drawMenuItem (new LineBreakItem ($ this ->style ->getTitleSeparator ()));
273+ $ frame -> addRows ( $ this ->drawMenuItem (new LineBreakItem () ));
274+ $ frame -> addRows ( $ this ->drawMenuItem (new StaticItem ($ this ->title ) ));
275+ $ frame -> addRows ( $ this ->drawMenuItem (new LineBreakItem ($ this ->style ->getTitleSeparator () )));
267276 }
268277
269- array_map (function ($ item , $ index ) {
270- $ this ->drawMenuItem ($ item , $ index === $ this ->selectedItem );
278+ array_map (function ($ item , $ index ) use ( $ frame ) {
279+ $ frame -> addRows ( $ this ->drawMenuItem ($ item , $ index === $ this ->selectedItem ) );
271280 }, $ this ->items , array_keys ($ this ->items ));
272281
273- $ this ->drawMenuItem (new LineBreakItem ());
282+ $ frame ->addRows ($ this ->drawMenuItem (new LineBreakItem ()));
283+
284+ $ frame ->newLine (2 );
285+
286+ foreach ($ frame ->getRows () as $ row ) {
287+ echo $ row ;
288+ }
274289
275- echo "\n\n" ;
290+ $ this -> currentFrame = $ frame ;
276291 }
277292
278293 /**
279294 * Draw a menu item
280295 *
281296 * @param MenuItemInterface $item
282297 * @param bool|false $selected
298+ * @return array
283299 */
284300 protected function drawMenuItem (MenuItemInterface $ item , $ selected = false )
285301 {
@@ -293,9 +309,9 @@ protected function drawMenuItem(MenuItemInterface $item, $selected = false)
293309 ? $ this ->style ->getSelectedUnsetCode ()
294310 : $ this ->style ->getUnselectedUnsetCode ();
295311
296- foreach ( $ rows as $ row ) {
297- echo sprintf (
298- "%s%s%s%s%s%s%s " ,
312+ return array_map ( function ( $ row ) use ( $ setColour , $ unsetColour ) {
313+ return sprintf (
314+ "%s%s%s%s%s%s%s \n\r " ,
299315 str_repeat (' ' , $ this ->style ->getMargin ()),
300316 $ setColour ,
301317 str_repeat (' ' , $ this ->style ->getPadding ()),
@@ -304,9 +320,7 @@ protected function drawMenuItem(MenuItemInterface $item, $selected = false)
304320 $ unsetColour ,
305321 str_repeat (' ' , $ this ->style ->getMargin ())
306322 );
307-
308- echo "\n\r" ;
309- }
323+ }, $ rows );
310324 }
311325
312326 /**
@@ -379,4 +393,43 @@ public function getStyle()
379393 {
380394 return $ this ->style ;
381395 }
396+
397+ public function getCurrentFrame ()
398+ {
399+ return $ this ->currentFrame ;
400+ }
401+
402+ /**
403+ * @param string $text
404+ * @return Flash
405+ */
406+ public function flash ($ text )
407+ {
408+ if (strpos ($ text , "\n" ) !== false ) {
409+ throw new \InvalidArgumentException ;
410+ }
411+
412+ $ style = (new MenuStyle ($ this ->terminal ))
413+ ->setBg ('yellow ' )
414+ ->setFg ('red ' );
415+
416+ return new Flash ($ this , $ style , $ this ->terminal , $ text );
417+ }
418+
419+ /**
420+ * @param string $text
421+ * @return Confirm
422+ */
423+ public function confirm ($ text )
424+ {
425+ if (strpos ($ text , "\n" ) !== false ) {
426+ throw new \InvalidArgumentException ;
427+ }
428+
429+ $ style = (new MenuStyle ($ this ->terminal ))
430+ ->setBg ('yellow ' )
431+ ->setFg ('red ' );
432+
433+ return new Confirm ($ this , $ style , $ this ->terminal , $ text );
434+ }
382435}
0 commit comments