Rewrite, cleanup and bugfixes
* More events added, constants cleaned up * Events now handled using noccylabs/tinyevent * Fixed magenta/cyan mixup in style * Fixed LineRead not resetting history pointer on command
This commit is contained in:
		@@ -1,12 +1,36 @@
 | 
			
		||||
<?php
 | 
			
		||||
/*
 | 
			
		||||
 *  Basic shell example, demonstrates adding a listener to update the prompt with
 | 
			
		||||
 *  the current time as well as creating an "anonymous" context and adding a
 | 
			
		||||
 *  command to it. It also shows how to change the style of the prompt.
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
require_once __DIR__."/../vendor/autoload.php";
 | 
			
		||||
 | 
			
		||||
use NoccyLabs\Shell\Shell;
 | 
			
		||||
use NoccyLabs\Shell\Style;
 | 
			
		||||
use NoccyLabs\Shell\Context;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
$myShell = new Shell();
 | 
			
		||||
 | 
			
		||||
// Set the style of prompt and input
 | 
			
		||||
$myShell->setPromptStyle(new Style(Style::BR_GREEN, Style::GREEN));
 | 
			
		||||
$myShell->setInputStyle(new Style(Style::BR_CYAN));
 | 
			
		||||
 | 
			
		||||
// Add a listener to update the prompt
 | 
			
		||||
$myShell->addListener(Shell::EVT_UPDATE_PROMPT, function ($e) {
 | 
			
		||||
    $e->shell->setPrompt(date("H:i:s").">");
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
// Set the initial prompt, not really needed.
 | 
			
		||||
$myShell->setPrompt("test>");
 | 
			
		||||
$myShell->pushContext(new Context());
 | 
			
		||||
 | 
			
		||||
// Create an anonymous context and add a command
 | 
			
		||||
$ctx = $myShell->createContext("root");
 | 
			
		||||
$ctx->addCommand("hello", function () {
 | 
			
		||||
    echo "Hello World!\n";
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
// Run the shell
 | 
			
		||||
$myShell->run();
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user