From 99a66767b45caff297cc187528704a5afefbd231 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B8=D1=80=D0=B8=D0=BB=D0=BB=20=D0=A1=D1=8B=D1=81?= =?UTF-8?q?=D0=BE=D0=B5=D0=B2?= Date: Mon, 6 Nov 2023 22:08:07 +0800 Subject: [PATCH] Adds command exit, and make execution of consiquent command as a priority --- pkg/cli/cli.go | 20 ++++++++------------ pkg/cli/commands.go | 10 ++++++++++ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/pkg/cli/cli.go b/pkg/cli/cli.go index a731b04..2720696 100644 --- a/pkg/cli/cli.go +++ b/pkg/cli/cli.go @@ -101,24 +101,20 @@ func (c *CLI) Run(opts RunOptions) error { for { select { - case cmd, ok := <-c.commands: - if !ok { - return nil - } + case cmd := <-c.commands: + nextCmd := cmd - nextCmd, err := cmd.Execute(exCtx) + for nextCmd != nil { + nextCmd, err = cmd.Execute(exCtx) - if err != nil { - return err - } - - if nextCmd != nil { - c.commands <- nextCmd + if err != nil { + return err + } } case event := <-keysEvents: switch event.Key { case keyboard.KeyEsc, keyboard.KeyCtrlC, keyboard.KeyCtrlD: - return nil + c.commands <- NewCommandExit() case keyboard.KeyEnter: c.commands <- NewCommandEdit("") default: diff --git a/pkg/cli/commands.go b/pkg/cli/commands.go index c77296d..5292781 100644 --- a/pkg/cli/commands.go +++ b/pkg/cli/commands.go @@ -100,3 +100,13 @@ func (c *CommandPrintMsg) Execute(exCtx *ExecutionContext) (Executer, error) { return nil, nil } + +type CommandExit struct{} + +func NewCommandExit() *CommandExit { + return &CommandExit{} +} + +func (c *CommandExit) Execute(_ *ExecutionContext) (Executer, error) { + return nil, fmt.Errorf("interrupted") +}