new: ble, can, hid and wifi modules will now set a custom prompt (closes #1117)

This commit is contained in:
Simone Margaritelli 2024-08-17 12:10:38 +02:00
commit 6282fe3451
7 changed files with 80 additions and 36 deletions

View file

@ -17,6 +17,7 @@ type Module interface {
Name() string
Description() string
Author() string
Prompt() string
Handlers() []ModuleHandler
Parameters() map[string]*ModuleParam
@ -29,33 +30,6 @@ type Module interface {
type ModuleList []Module
type moduleJSON struct {
Name string `json:"name"`
Description string `json:"description"`
Author string `json:"author"`
Parameters map[string]*ModuleParam `json:"parameters"`
Handlers []ModuleHandler `json:"handlers"`
Running bool `json:"running"`
State map[string]interface{} `json:"state"`
}
func (mm ModuleList) MarshalJSON() ([]byte, error) {
mods := []moduleJSON{}
for _, m := range mm {
mJSON := moduleJSON{
Name: m.Name(),
Description: m.Description(),
Author: m.Author(),
Parameters: m.Parameters(),
Handlers: m.Handlers(),
Running: m.Running(),
State: m.Extra(),
}
mods = append(mods, mJSON)
}
return json.Marshal(mods)
}
type SessionModule struct {
Name string
Session *Session
@ -67,6 +41,7 @@ type SessionModule struct {
params map[string]*ModuleParam
requires []string
tag string
prompt string
}
func AsTag(name string) string {
@ -215,7 +190,7 @@ func (m SessionModule) DecParam(name string) (error, float64) {
}
} else {
return fmt.Errorf("Parameter %s does not exist.", name), 0
return fmt.Errorf("parameter %s does not exist", name), 0
}
}
@ -227,6 +202,14 @@ func (m SessionModule) BoolParam(name string) (error, bool) {
}
}
func (m *SessionModule) SetPrompt(prompt string) {
m.prompt = prompt
}
func (m *SessionModule) Prompt() string {
return m.prompt
}
func (m *SessionModule) AddHandler(h ModuleHandler) {
m.handlers = append(m.handlers, h)
}
@ -302,3 +285,30 @@ func (m *SessionModule) SetRunning(running bool, cb func()) error {
return nil
}
type moduleJSON struct {
Name string `json:"name"`
Description string `json:"description"`
Author string `json:"author"`
Parameters map[string]*ModuleParam `json:"parameters"`
Handlers []ModuleHandler `json:"handlers"`
Running bool `json:"running"`
State map[string]interface{} `json:"state"`
}
func (mm ModuleList) MarshalJSON() ([]byte, error) {
mods := []moduleJSON{}
for _, m := range mm {
mJSON := moduleJSON{
Name: m.Name(),
Description: m.Description(),
Author: m.Author(),
Parameters: m.Parameters(),
Handlers: m.Handlers(),
Running: m.Running(),
State: m.Extra(),
}
mods = append(mods, mJSON)
}
return json.Marshal(mods)
}

View file

@ -458,10 +458,18 @@ func (s *Session) Run(line string) error {
}
// is it a module command?
for _, m := range s.Modules {
for _, h := range m.Handlers() {
if parsed, args := h.Parse(line); parsed {
return h.Exec(args)
for _, mod := range s.Modules {
for _, modHandler := range mod.Handlers() {
if parsed, args := modHandler.Parse(line); parsed {
if err := modHandler.Exec(args); err != nil {
return err
} else if prompt := mod.Prompt(); prompt != "" {
// if the module handler has been executed successfully and
// the module overrides the prompt, set it
s.Env.Set(PromptVariable, prompt)
s.Refresh()
}
return nil
}
}
}
@ -478,5 +486,5 @@ func (s *Session) Run(line string) error {
return nil
}
return fmt.Errorf("unknown or invalid syntax \"%s%s%s\", type %shelp%s for the help menu.", tui.BOLD, line, tui.RESET, tui.BOLD, tui.RESET)
return fmt.Errorf("unknown or invalid syntax \"%s%s%s\", type %shelp%s for the help menu", tui.BOLD, line, tui.RESET, tui.BOLD, tui.RESET)
}