protected IMenuOption <IMenuFunc> CreateMenuFuncOption(MenuOptionKey key, string label, Action action, bool handleException = true, PostInvokeCommand postInvokeCommand = PostInvokeCommand.StayInSubmenu, bool waitForKeyPress = true) { return(this.CreateMenuFuncOption(key, label, MenuFunc)); PostInvokeCommand MenuFunc() { try { action(); } catch (Exception exception) { if (!handleException) { throw exception; } Print.Error("Uh-oh! Something unexpected happened and an exception was thrown."); if (this.Confirm("Do you want to view the exception?")) { Print.Exception(exception, PostPrintOption.PrintNewLineAndWaitForKeyPress); } } return(waitForKeyPress ? this.WaitForKeyPressAndReturn(postInvokeCommand) : postInvokeCommand); } }
public void RemoveBy(MenuOptionKey key) { if (this._submenuMapper.ContainsKey(key)) { this._submenuMapper.Remove(key); } else { this._menuFuncMapper.Remove(key); } }
public IInvokable this[MenuOptionKey key] { get { if (this._submenuMapper.TryGetValue(key, out var value)) { return(value); } return(this._menuFuncMapper[key]); } }
private void AddToMapper <T>(MenuOptionKey key, T item) where T : IInvokable, IMenuItem { if (this._submenuMapper.ContainsKey(key) || this._menuFuncMapper.ContainsKey(key)) { throw new InvalidOperationException($"An {nameof(IMenuOption)} is already mapped to that key."); } switch (item) { case IMenu _: this._submenuMapper.Add(key, (IMenu)item); break; case IMenuFunc _: this._menuFuncMapper.Add(key, (IMenuFunc)item); break; default: throw new InvalidOperationException($"{typeof(T)} is not a valid type."); } }
public MenuOption(MenuOptionKey key, IMenuItem item) { this.Key = key; this.Item = item; }
public IMenuItem this[MenuOptionKey key] => this._optionses[key] as IMenuItem;
public INonInvokableMenuFunc GetMenuFuncBy(MenuOptionKey key) => this[key] as INonInvokableMenuFunc;
public INonInvokableMenu GetSubmenuBy(MenuOptionKey key) => this[key] as INonInvokableMenu;
protected INonInvokableMenuFunc GetMenuFuncBy(MenuOptionKey key) => this._rootMenu.GetMenuFuncBy(key);
protected INonInvokableMenu GetSubmenuBy(MenuOptionKey key) => this._rootMenu.GetSubmenuBy(key);
protected IMenuOption <IMenuFunc> CreateMenuFuncOption(MenuOptionKey key, string label, Func <PostInvokeCommand> func) { return(new MenuOption <IMenuFunc>(key, new MenuFunc(label, func))); }
protected IMenuOption <IMenu> CreateSubMenuOption(MenuOptionKey key, IMenu menu) { return(new MenuOption <IMenu>(key, menu)); }
public static string ToCleanString(this MenuOptionKey key) { var keyNumber = (int)key; return(keyNumber <= 9 ? keyNumber.ToString() : key.ToString()); }