1
0

Implemented skip action (closes #31) (#32)

This commit is contained in:
Patrick Gaskin
2020-05-25 17:17:27 -04:00
committed by GitHub
parent fdc3ec4f50
commit 88adf88880
4 changed files with 47 additions and 10 deletions

14
res/doc
View File

@@ -20,6 +20,8 @@
# reader - the overflow menu in the reader
# <label> the label to show on the menu item (must not contain :)
# <action> the type of action to run, one of:
# cmd_spawn - starts another process or script in the background
# cmd_output - runs a command and displays the output
# dbg_syslog - writes a message to syslog (for testing)
# dbg_error - always returns an error (for testing)
# dbg_msg - shows a message (for testing)
@@ -29,9 +31,12 @@
# nickel_extras - opens one of the beta features
# nickel_misc - other stuff which isn't significant enough for its own category
# power - gracefully controls the power state
# cmd_spawn - starts another process or script in the background
# cmd_output - runs a command and displays the output
# skip - skips a number of actions after the current one (mainly useful for more complex conditional chains) (this action will not update the success/failure flag)
# <arg> the argument passed to the action:
# cmd_spawn - the command line to pass to /bin/sh -c (started in /)
# It can be prefixed with "quiet:" to prevent the toast with the process PID from being displayed.
# cmd_output - the timeout in milliseconds (0 < t < 10000), a colon, then the command line to pass to /bin/sh -c (started in /)
# It can be prefixed with "quiet:" to prevent the message box with the output from being displayed (i.e. you'd use this where you'd normally use >/dev/null 2>&1).
# dbg_syslog - the text to write
# dbg_error - the error message
# dbg_msg - the message
@@ -58,10 +63,7 @@
# power - one of:
# shutdown (4.13.12638+)
# reboot (4.13.12638+)
# cmd_spawn - the command line to pass to /bin/sh -c (started in /)
# It can be prefixed with "quiet:" to prevent the toast with the process PID from being displayed.
# cmd_output - the timeout in milliseconds (0 < t < 10000), a colon, then the command line to pass to /bin/sh -c (started in /)
# It can be prefixed with "quiet:" to prevent the message box with the output from being displayed (i.e. you'd use this where you'd normally use >/dev/null 2>&1).
# skip - the number of actions to skip, or -1 to skip all remaining ones (i.e. end the chain)
#
# chain_success:<action>:<arg>
# chain_failure:<action>:<arg>

View File

@@ -8,11 +8,13 @@ typedef enum {
NM_ACTION_RESULT_TYPE_SILENT = 0,
NM_ACTION_RESULT_TYPE_MSG = 1,
NM_ACTION_RESULT_TYPE_TOAST = 2,
NM_ACTION_RESULT_TYPE_SKIP = 3, // for use by skip only
} nm_action_result_type_t;
typedef struct {
nm_action_result_type_t type;
char *msg;
int skip; // for use by skip only
} nm_action_result_t;
typedef nm_action_result_t *(*nm_action_fn_t)(const char *arg, char **err);
@@ -31,6 +33,8 @@ void nm_action_result_free(nm_action_result_t *res);
#endif
#define NM_ACTIONS \
X(cmd_spawn) \
X(cmd_output) \
X(dbg_syslog) \
X(dbg_error) \
X(dbg_msg) \
@@ -41,8 +45,7 @@ void nm_action_result_free(nm_action_result_t *res);
X(nickel_extras) \
X(nickel_misc) \
X(power) \
X(cmd_spawn) \
X(cmd_output)
X(skip)
#define X(name) NM_ACTION_(name);
NM_ACTIONS

View File

@@ -1,4 +1,5 @@
#define _GNU_SOURCE // asprintf
#include <limits.h>
#include "action.h"
#include "kfmon.h"
@@ -29,6 +30,21 @@ NM_ACTION_(dbg_toast) {
#undef NM_ERR_RET
}
NM_ACTION_(skip) {
#define NM_ERR_RET NULL
char *tmp;
long n = strtol(arg, &tmp, 10);
NM_ASSERT(*arg && !*tmp && n != 0 && n >= -1 && n < INT_MAX, "invalid count '%s': must be a nonzero integer or -1", arg);
nm_action_result_t *res = calloc(1, sizeof(nm_action_result_t));
res->type = NM_ACTION_RESULT_TYPE_SKIP;
res->skip = (int)(n);
NM_RETURN_OK(res);
#undef NM_ERR_RET
}
NM_ACTION_(kfmon_id) {
// Start by watch ID (simpler, but IDs may not be stable across a single power cycle, given severe KFMon config shuffling)
int status = nm_kfmon_simple_request("start", arg);

View File

@@ -146,16 +146,25 @@ extern "C" MenuTextItem* _nm_menu_hook(void* _this, QMenu* menu, QString const&
NM_LOG("item '%s' pressed...", it->lbl);
char *err = NULL;
bool success = true;
int skip = 0;
for (nm_menu_action_t *cur = it->action; cur; cur = cur->next) {
NM_LOG("action %p with argument %s : ", cur->act, cur->arg);
NM_LOG("...success=%d ; on_success=%d on_failure=%d", success, cur->on_success, cur->on_failure);
NM_LOG("...success=%d ; on_success=%d on_failure=%d skip=%d", success, cur->on_success, cur->on_failure, skip);
if (skip) {
NM_LOG("...skipping action due to skip flag (remaining=%d)", skip);
if (skip > 0)
skip--;
continue;
}
if (!((success && cur->on_success) || (!success && cur->on_failure))) {
NM_LOG("...skipping action due to condition flags");
continue;
}
free(err);
nm_action_result_t *res = cur->act(cur->arg, &err);
if (!(success = err == NULL)) {
if (err == NULL && res && res->type == NM_ACTION_RESULT_TYPE_SKIP) {
NM_LOG("...not updating success flag (value=%d) for skip result", success);
} else if (!(success = err == NULL)) {
NM_LOG("...error: '%s'", err);
continue;
} else if (!res) {
@@ -178,6 +187,13 @@ extern "C" MenuTextItem* _nm_menu_hook(void* _this, QMenu* menu, QString const&
}
MainWindowController_toast(mwc, QLatin1String(res->msg), QStringLiteral(""), 1500);
break;
case NM_ACTION_RESULT_TYPE_SKIP:
skip = res->skip;
if (skip == -1)
NM_LOG("...skipping remaining actions");
else
NM_LOG("...skipping next %d actions", skip);
break;
}
nm_action_result_free(res);
}