Immediately before processing begins, the event OnRun is called. Immediately after processing has ended, the event OnQuit called.
OnRun
OnQuit
static void DisabledCommands() { var m = new CMenu (); /* * In this example, a global flag is used to determine the visibility of disabled commands. * It is initially cleared, the 'enable' command sets it. */ Enabled = false; m.Add ("enable", s => Enabled = true); /* * Create a new inline command, then set its enabledness function so it returns the above flag. */ var mi = m.Add ("inline", s => Console.WriteLine ("Disabled inline command was enabled!")); mi.SetEndablednessCondition (() => Enabled); /* * Command abbreviations do not change when hidden items become visible, i.e. it is made sure they are already long * enough. This avoids confusion about abbreviations suddenly changing. */ m.Add ("incollision", s => Console.WriteLine ("The abbreviation of 'incollision' is longer to account for the hidden 'inline' command.")); /* * It is also possible to override the visibility by subclassing. */ m.Add (new DisabledItem ()); m.Run (); }
static void TreeInitialization () { /* * It may be useful to create complex menu trees using collection initializers */ var m = new CMenu () { new CMenuItem ("1") { new CMenuItem ("1", s => Console.WriteLine ("1-1")), new CMenuItem ("2", s => Console.WriteLine ("1-2")), }, new CMenuItem ("2") { new CMenuItem ("1", s => Console.WriteLine ("2-1")), new CMenuItem ("2", s => Console.WriteLine ("2-2")), }, }; m.PromptCharacter = "tree>"; m.Run (); /* * You can also combine object and collection initializers */ m = new CMenu () { PromptCharacter = "combined>", MenuItem = { new CMenuItem ("1", s => Console.WriteLine ("1")), new CMenuItem ("2", s => Console.WriteLine ("2")), } }; m.Run (); }
static void ImmediateMode() { var m = new CMenu (); m.ImmediateMenuMode = true; m.Add ("foo", s => Console.WriteLine ("foo")); m.Add ("bar", s => Console.WriteLine ("bar")); m.Run (); }
public void Run() { var menu = new CMenu(); menu.Add("view", (name) => RenderSystem(name)); menu.Run(); Console.WriteLine(">>>> DONE"); }
static void Main (string[] args) { Console.WriteLine ("Simple CMenu demonstration"); var mainmenu = new CMenu (); mainmenu.PromptCharacter = "main>"; mainmenu.Add ("tutorial", s => new Tutorial ().Run ()); mainmenu.Add ("tree-init", s => TreeInitialization ()); mainmenu.Add ("disabled", s => DisabledCommands ()); mainmenu.Add ("passive", s => PassiveMode ()); mainmenu.Add ("immediate", s => ImmediateMode ()); mainmenu.Add (new ExamplesMenu ()); mainmenu.CQ.ImmediateInput ("help"); mainmenu.Run (); }
private void Basics () { // Create menu menu = new CMenu (); // Add simple Hello World command menu.Add ("hello", s => Console.WriteLine ("Hello world!")); /* * If the command happens to be more complex, you can just put it in a separate method. */ menu.Add ("len", s => PrintLen (s)); /* * It is also possible to return an exit code to signal that processing should be stopped. * By default, the command "quit" exists for this purpose. Let's add an alternative way to stop processing input. */ menu.Add ("exit", s => menu.Quit ()); /* * To create a command with help text, simply add it during definition. */ menu.Add ("time", s => Console.WriteLine (DateTime.UtcNow), "Help for \"time\": Writes the current time"); /* * You can also access individual commands to edit them later, though this is rarely required. */ menu["time"].HelpText += " (UTC)."; // Run menu. The menu will run until quit by the user. Console.WriteLine ("Enter \"help\" for help."); Console.WriteLine ("Enter \"quit\" to quit (in this case, the next step of this demo will be started)."); menu.Run (); Console.WriteLine ("(First menu example completed, starting the next one...)"); }
static void PassiveMode() { var m = new CMenu (); m.Add ("passive", s => { m.CQ.PassiveMode = true; Console.WriteLine ("Passive mode selected. Input will be ignored."); Console.WriteLine ("A timer will be set which will input 'active' in 5 seconds."); new Thread (() => { for (int i = 5; i >= 0; i--) { Console.WriteLine (i + "..."); Thread.Sleep (1000); } Console.WriteLine ("Sending input 'active' to the IO queue."); m.CQ.ImmediateInput ("active"); }).Start (); }); m.Add ("active", s => { m.CQ.PassiveMode = false; Console.WriteLine ("Active mode selected."); }); Console.WriteLine ("IO is currently in active mode - you will be prompted for input."); Console.WriteLine ("The 'passive' command will turn passive mode on, which disables interactive input."); Console.WriteLine ("The 'active' command will turn active mode back on."); Console.WriteLine ("Please enter 'passive'."); m.Run (); }