Added getInput() method for basic async input
This commit is contained in:
		
							
								
								
									
										31
									
								
								examples/input.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								examples/input.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,31 @@
 | 
				
			|||||||
 | 
					<?php
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 *  This example demonstrates how to use events to catch commands that
 | 
				
			||||||
 | 
					 *  have not been handled by any context or builtin.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					require_once __DIR__."/../vendor/autoload.php";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use NoccyLabs\Shell\Shell;
 | 
				
			||||||
 | 
					use NoccyLabs\Shell\Style;
 | 
				
			||||||
 | 
					use NoccyLabs\Shell\Context;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					$myShell = new Shell();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Set the initial prompt, not really needed.
 | 
				
			||||||
 | 
					$myShell->setPrompt("test>");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Create an anonymous context and add a command
 | 
				
			||||||
 | 
					$ctx = $myShell->createContext("root");
 | 
				
			||||||
 | 
					$ctx->addCommand("hello", function () use ($myShell) {
 | 
				
			||||||
 | 
					    $myShell->getInput("What is your name?", function ($name) use ($myShell) {
 | 
				
			||||||
 | 
					        echo "Hello, {$name}\n";
 | 
				
			||||||
 | 
					        $myShell->getInput("Who is your daddy and what does he do?", function ($daddy) {
 | 
				
			||||||
 | 
					            echo "{$daddy}? Oookay...\n";
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Run the shell
 | 
				
			||||||
 | 
					$myShell->run();
 | 
				
			||||||
@@ -54,6 +54,18 @@ class Shell
 | 
				
			|||||||
     * @var Style The style applied to the input text
 | 
					     * @var Style The style applied to the input text
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    protected $input_style = null;
 | 
					    protected $input_style = null;
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * @var callable The callback to pass the input on to
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    protected $input_callback = null;
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * @var string Question prompt for getInput()
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    protected $input_prompt = null;
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * @var string The prompt before changing to $input_prompt
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    protected $input_last_prompt = null;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Constructor
 | 
					     * Constructor
 | 
				
			||||||
@@ -532,6 +544,31 @@ class Shell
 | 
				
			|||||||
        return $event;
 | 
					        return $event;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function getInput($prompt, callable $callback)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $this->addListener(Shell::EVT_UPDATE_PROMPT, [ $this, "onInputPrompt" ], $prompt);
 | 
				
			||||||
 | 
					        $this->addListener(Shell::EVT_BEFORE_COMMAND, [ $this, "onInputHandler" ], $this->prompt, $callback);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    public function onInputPrompt(Event $e, $prompt)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $this->setPrompt($prompt);
 | 
				
			||||||
 | 
					        $e->stopPropagation();
 | 
				
			||||||
 | 
					        $this->removeListener(Shell::EVT_UPDATE_PROMPT, [ $this, "onInputPrompt" ], $prompt);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    public function onInputHandler(Event $e, $last_prompt, $callback)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        // Restore the prompt
 | 
				
			||||||
 | 
					        $this->setPrompt($last_prompt);
 | 
				
			||||||
 | 
					        // Remove the listeners and compose the result string
 | 
				
			||||||
 | 
					        $this->removeListener(Shell::EVT_BEFORE_COMMAND, [ $this, "onInputHandler" ], $last_prompt, $callback);
 | 
				
			||||||
 | 
					        $input = trim($e->command." ".join(" ",$e->args));
 | 
				
			||||||
 | 
					        $e->stopPropagation();
 | 
				
			||||||
 | 
					        // Call the callback
 | 
				
			||||||
 | 
					        call_user_func($callback, $input);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Stop the shell; calling this method will cause the main run() to return.
 | 
					     * Stop the shell; calling this method will cause the main run() to return.
 | 
				
			||||||
     *
 | 
					     *
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user