From cf7abed1e66edad4cf37246b6b13f1fcebf8d3ef Mon Sep 17 00:00:00 2001 From: Inhere Date: Sat, 11 Feb 2023 22:00:31 +0800 Subject: [PATCH] :necktie: up: update the cmd event fire, will notify parents and app --- cmd.go | 17 ++++++++++++++++- events/events.go | 2 +- ext.go | 1 - show/table/table.go | 2 +- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/cmd.go b/cmd.go index 71c08d2..e48bd8c 100644 --- a/cmd.go +++ b/cmd.go @@ -658,9 +658,24 @@ func (c *Command) goodName() string { // Fire event handler by name func (c *Command) Fire(event string, data map[string]any) (stop bool) { + hookCtx := newHookCtx(event, c, data) Debugf("cmd: %s - trigger the event: %s", c.Name, event) - return c.Hooks.Fire(event, newHookCtx(event, c, data)) + // notify all parent commands + p := c.parent + for p != nil { + if p.Hooks.Fire(event, hookCtx) { + return true + } + p = p.parent + } + + // notify to app + if c.app != nil && c.app.Hooks.Fire(event, hookCtx) { + return + } + + return c.Hooks.Fire(event, hookCtx) } // On add hook handler for a hook event diff --git a/events/events.go b/events/events.go index 1e72940..a95c1f4 100644 --- a/events/events.go +++ b/events/events.go @@ -53,7 +53,7 @@ const ( // {args: command-args} OnCmdOptParsed = "cmd.opts.parsed" - // OnCmdRunBefore cmd run + // OnCmdRunBefore cmd run, flags has been parsed. OnCmdRunBefore = "cmd.run.before" // OnCmdRunAfter after cmd success run OnCmdRunAfter = "cmd.run.after" diff --git a/ext.go b/ext.go index 8dbbed9..884ebb7 100644 --- a/ext.go +++ b/ext.go @@ -41,7 +41,6 @@ const ( EvtCmdExecError = events.OnCmdExecError EvtGOptionsParsed = events.OnGlobalOptsParsed - // EvtStop = "stop" ) // HookFunc definition. diff --git a/show/table/table.go b/show/table/table.go index 9b14065..bc3b077 100644 --- a/show/table/table.go +++ b/show/table/table.go @@ -211,7 +211,7 @@ type Cell struct { Align strutil.PosFlag // Val is the cell data - Val interface{} + Val any str string // string cache of Val }