From e200cd56bf22ec48d1b0441be2355dc6dcb35cd4 Mon Sep 17 00:00:00 2001 From: Koni Marti Date: Mon, 11 Jul 2022 20:23:41 +0200 Subject: [PATCH] commands: implement a no-quit mode Add a mode that prevents aerc from quitting normally when an important task is performed, i.e. when sending a message. The no-quit mode will be ignored when quit is used with the -f option to force an exit. Suggested-by: ph14nix[m] Signed-off-by: Koni Marti Acked-by: Robin Jarry --- commands/mode/noquit.go | 26 ++++++++++++++++++++++++++ commands/quit.go | 23 ++++++++++++++++++++--- doc/aerc.1.scd | 6 ++++-- 3 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 commands/mode/noquit.go diff --git a/commands/mode/noquit.go b/commands/mode/noquit.go new file mode 100644 index 0000000..ad66416 --- /dev/null +++ b/commands/mode/noquit.go @@ -0,0 +1,26 @@ +package mode + +import "sync/atomic" + +// noquit is a counter for goroutines that requested the no-quit mode +var noquit int32 + +// NoQuit enters no-quit mode where aerc cannot be exited (unless the force +// option is used) +func NoQuit() { + atomic.AddInt32(&noquit, 1) +} + +// NoQuitDone leaves the no-quit mode +func NoQuitDone() { + atomic.AddInt32(&noquit, -1) +} + +// QuitAllowed checks if aerc can exit normally (only when all goroutines that +// requested a no-quit mode were done and called the NoQuitDone() function) +func QuitAllowed() bool { + if atomic.LoadInt32(&noquit) > 0 { + return false + } + return true +} diff --git a/commands/quit.go b/commands/quit.go index fbec2b7..ee5f46c 100644 --- a/commands/quit.go +++ b/commands/quit.go @@ -2,8 +2,11 @@ package commands import ( "errors" + "fmt" + "git.sr.ht/~rjarry/aerc/commands/mode" "git.sr.ht/~rjarry/aerc/widgets" + "git.sr.ht/~sircmpwn/getopt" ) type Quit struct{} @@ -27,8 +30,22 @@ func (err ErrorExit) Error() string { } func (Quit) Execute(aerc *widgets.Aerc, args []string) error { - if len(args) != 1 { - return errors.New("Usage: quit") + force := false + opts, optind, err := getopt.Getopts(args, "f") + if err != nil { + return err } - return ErrorExit(1) + for _, opt := range opts { + switch opt.Option { + case 'f': + force = true + } + } + if len(args) != optind { + return errors.New("Usage: quit [-f]") + } + if force || mode.QuitAllowed() { + return ErrorExit(1) + } + return fmt.Errorf("A task is not done yet. Use -f to force an exit.") } diff --git a/doc/aerc.1.scd b/doc/aerc.1.scd index 17e9042..b06dd43 100644 --- a/doc/aerc.1.scd +++ b/doc/aerc.1.scd @@ -113,8 +113,10 @@ These commands work in any context. *choose* -o [-o ]... Prompts the user to choose from various options. -*quit* - Exits aerc. +*quit* [-f] + Exits aerc. If a task is being performed that should not be interrupted + (like sending a message), a normal quit call might fail. In this case, + closing aerc can be forced with the -f option. ## MESSAGE COMMANDS