Skip to content

Commit

Permalink
Use string parameters for auto-quit option
Browse files Browse the repository at this point in the history
Accepting {0,1,2} as parameters is as non-descriptive as it gets.
Improve on this usability problem by renaming the parameters to actual
strings that one is able to work with and that can be understood without
having to read the man page:
0 -> never
1 -> first
2 -> all

For now we accept both the old format and the new format, but this patch
already deprecates the old format and eventually we will want to only
accept the new string parameter format. But that will likely require a
major version bump.
  • Loading branch information
vimpostor committed Aug 9, 2023
1 parent 221fdc7 commit 052a049
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 13 deletions.
2 changes: 1 addition & 1 deletion assets/completions/bash-completion/completions/blobdrop
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ _blobdrop() {

case "$prev" in
-x|--auto-quit)
COMPREPLY=($(compgen -W '0 1 2' -- "$cur"))
COMPREPLY=($(compgen -W 'never first all' -- "$cur"))
return
;;
esac
Expand Down
2 changes: 1 addition & 1 deletion assets/completions/fish/vendor_completions.d/blobdrop.fish
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ complete -c blobdrop -s '-l' -l 'link' -d 'print OSC8 hyperlinks'
complete -c blobdrop -s '-n' -l 'notification' -d 'send notification'
complete -c blobdrop -s '-p' -l 'persistent' -d 'disable autohiding during drag'
complete -c blobdrop -s '-t' -l 'ontop' -d 'keep window on top'
complete -c blobdrop -s '-x' -l 'auto-quit' -d 'autoquit behaviour' -x -a "0\t'do not autoquit' 1\t'after first drag' 2\t'after all items have been dragged'"
complete -c blobdrop -s '-x' -l 'auto-quit' -d 'autoquit behaviour' -x -a "never\t'do not autoquit' first\t'after first drag' all\t'after all items have been dragged'"
2 changes: 1 addition & 1 deletion assets/completions/zsh/site-functions/_blobdrop
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#compdef blobdrop

_blobdrop() {
_arguments {-h,--help}'[show help]' {-v,--version}'[show version]' {-f,--frameless}'[show frameless window]' {-k,--keep}'[keep dropped files]' {-l,--link}'[print OSC8 hyperlinks]' {-n,--notification}'[send notification]' {-p,--persistent}'[disable autohiding during drag]' {-t,--ontop}'[keep window on top]' {-x,--auto-quit}'[autoquit behaviour]:num:((0\:"do not autoquit" 1\:"after first drag" 2\:"after all items have been dragged"))' '*: arg:_files'
_arguments {-h,--help}'[show help]' {-v,--version}'[show version]' {-f,--frameless}'[show frameless window]' {-k,--keep}'[keep dropped files]' {-l,--link}'[print OSC8 hyperlinks]' {-n,--notification}'[send notification]' {-p,--persistent}'[disable autohiding during drag]' {-t,--ontop}'[keep window on top]' {-x,--auto-quit}'[autoquit behaviour]:num:((never\:"do not autoquit" first\:"after first drag" all\:"after all items have been dragged"))' '*: arg:_files'
return 0
}

Expand Down
2 changes: 1 addition & 1 deletion doc/man/man1/blobdrop.1
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Do not auto-hide the main window while dragging.
Keep the window on top of other windows.
.TP
.B \-x, \-\-auto-quit \fINUM
Changes the conditions when blobdrop will automatically quit. For 0 it will never automatically quit, for 1 it will quit after the first drag and for 2 (default value) it will quit once every path has been dragged at least once.
Changes the conditions when blobdrop will automatically quit. Must be one of {never, first, all}. For never it will never automatically quit, for first it will quit after the first drag and for all (default value) it will quit once every path has been dragged at least once.

.SH EXIT STATUS
Returns zero on success.
25 changes: 16 additions & 9 deletions src/getopts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,31 @@ bool parse(QCoreApplication &app) {
"Keep the window on top of other windows.");
QCommandLineOption auto_quit_opt(QStringList() << "x"
<< "auto-quit",
"Whether to autoquit after a drag is finished. 0 = disable, 1 = after first drag, 2 (default) = after all paths have been used",
"The amount of drags after which the program should automatically close. Must be one of {never, first, all (default)}",
"number");

p.addOptions({frameless_opt, keep_opt, link_opt, notify_opt, persistent_opt, ontop_opt, auto_quit_opt});
p.process(app);

if (p.isSet(auto_quit_opt)) {
const auto opt = p.value(auto_quit_opt);
if (std::ranges::all_of(opt.toStdString(), ::isdigit)) {
if (opt.toInt() > 2) {
std::cerr << "auto-quit needs to be one of {0,1,2}" << std::endl;
return false;
}
Settings::get()->auto_quit_behavior = static_cast<Settings::AutoQuitBehavior>(opt.toInt());
} else {
std::cerr << "auto-quit needs to be a number" << std::endl;
constexpr std::array str_repr = {"never", "first", "all"};
int choice = std::ranges::find(str_repr, opt.toStdString()) - str_repr.cbegin();
if (std::ranges::all_of(opt.toStdString(), ::isdigit) && opt.toInt() < 3) {
/**
* Previously this option used {0,1,2} as parameter instead of strings.
* Eventually we only want to accept the string parameters,
* but for compatibility reasons we accept both for now.
* TODO: Remove this for version 3.0
*/
std::cerr << "The integer parameters {0,1,2} are deprecated and will be removed in a future release." << std::endl
<< "Please use the equivalent string options {never,first,all} instead." << std::endl;
choice = opt.toInt();
} else if (choice > 2) {
std::cerr << "auto-quit needs to be one of {never,first,all}" << std::endl;
return false;
}
Settings::get()->auto_quit_behavior = static_cast<Settings::AutoQuitBehavior>(choice);
}
if (p.isSet(ontop_opt)) {
Settings::get()->always_on_top = true;
Expand Down

0 comments on commit 052a049

Please sign in to comment.