Skip to content

Commit 1c1567f

Browse files
Added: Add support for %stdout_original_length and %stderr_original_length result variables as per termux/termux-app@f62febbf and termux/termux-app@a2209ddd
1 parent d9a172d commit 1c1567f

File tree

3 files changed

+53
-10
lines changed

3 files changed

+53
-10
lines changed

app/src/main/java/com/termux/tasker/EditConfigurationActivity.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,9 @@ public void finish() {
209209
if (TaskerPlugin.hostSupportsRelevantVariables(getIntent().getExtras())) {
210210
TaskerPlugin.addRelevantVariableList(resultIntent, new String[]{
211211
PluginUtils.PLUGIN_VARIABLE_STDOUT + "\nStandard Output\nThe <B>stdout</B> of the command.",
212+
PluginUtils.PLUGIN_VARIABLE_STDOUT_ORIGINAL_LENGTH + "\nStandard Output Original Length\nThe original length of <B>stdout</B>.",
212213
PluginUtils.PLUGIN_VARIABLE_STDERR + "\nStandard Error\nThe <B>stderr</B> of the command.",
214+
PluginUtils.PLUGIN_VARIABLE_STDERR_ORIGINAL_LENGTH + "\nStandard Error Original Length\nThe original length of <B>stderr</B>.",
213215
PluginUtils.PLUGIN_VARIABLE_EXIT_CODE + "\nExit Code\nThe <B>exit code</B> of the command. " +
214216
"0 often means success and anything else is usually a failure of some sort."
215217
});

app/src/main/java/com/termux/tasker/FireReceiver.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void onReceive(final Context context, final Intent intent) {
6161
errmsg = PluginBundleManager.isBundleValid(context, bundle);
6262
if (errmsg != null) {
6363
Logger.logError(LOG_TAG, errmsg);
64-
PluginUtils.sendImmediateResultToPluginHostApp(this, intent, null, null, null, TaskerPlugin.Setting.RESULT_CODE_FAILED, errmsg);
64+
PluginUtils.sendImmediateResultToPluginHostApp(this, intent, TaskerPlugin.Setting.RESULT_CODE_FAILED, errmsg);
6565
return;
6666
}
6767

@@ -79,7 +79,7 @@ public void onReceive(final Context context, final Intent intent) {
7979
errmsg = TermuxUtils.isTermuxAppAccessible(context);
8080
if (errmsg != null) {
8181
Logger.logError(LOG_TAG, errmsg);
82-
PluginUtils.sendImmediateResultToPluginHostApp(this, intent, null, null, null, TaskerPlugin.Setting.RESULT_CODE_FAILED, errmsg);
82+
PluginUtils.sendImmediateResultToPluginHostApp(this, intent, TaskerPlugin.Setting.RESULT_CODE_FAILED, errmsg);
8383
return;
8484
}
8585

@@ -88,7 +88,7 @@ public void onReceive(final Context context, final Intent intent) {
8888
if (executionCommand.executable == null || executionCommand.executable.isEmpty()) {
8989
errmsg = context.getString(R.string.error_null_or_empty_executable);
9090
Logger.logError(LOG_TAG, errmsg);
91-
PluginUtils.sendImmediateResultToPluginHostApp(this, intent, null, null, null, TaskerPlugin.Setting.RESULT_CODE_FAILED, errmsg);
91+
PluginUtils.sendImmediateResultToPluginHostApp(this, intent, TaskerPlugin.Setting.RESULT_CODE_FAILED, errmsg);
9292
return;
9393
}
9494

app/src/main/java/com/termux/tasker/utils/PluginUtils.java

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,12 @@ public class PluginUtils {
102102

103103
/** Plugin variable for stdout value of termux command */
104104
public static final String PLUGIN_VARIABLE_STDOUT = "%stdout"; // Default: "%stdout"
105+
/** Plugin variable for original length of stdout value of termux command */
106+
public static final String PLUGIN_VARIABLE_STDOUT_ORIGINAL_LENGTH = "%stdout_original_length"; // Default: "%stdout_original_length"
105107
/** Plugin variable for stderr value of termux command */
106108
public static final String PLUGIN_VARIABLE_STDERR = "%stderr"; // Default: "%stderr"
109+
/** Plugin variable for original length of stderr value of termux command */
110+
public static final String PLUGIN_VARIABLE_STDERR_ORIGINAL_LENGTH = "%stderr_original_length"; // Default: "%stderr_original_length"
107111
/** Plugin variable for exit code value of termux command */
108112
public static final String PLUGIN_VARIABLE_EXIT_CODE = "%result"; // Default: "%result"
109113
/** Plugin variable for err value of termux command */
@@ -162,7 +166,7 @@ public static void sendExecuteIntentToExecuteService(final Context context, fina
162166
executionIntent.putExtra(TERMUX_SERVICE.EXTRA_PENDING_INTENT, pendingIntent);
163167
} else {
164168
// If execute in background is not enabled, do not expect results back from execution service and return result now so that plugin action does not timeout
165-
sendImmediateResultToPluginHostApp(receiver, originalIntent, null, null, null, TaskerPlugin.Setting.RESULT_CODE_OK, null);
169+
sendImmediateResultToPluginHostApp(receiver, originalIntent, TaskerPlugin.Setting.RESULT_CODE_OK, null);
166170
}
167171

168172
try {
@@ -176,22 +180,43 @@ public static void sendExecuteIntentToExecuteService(final Context context, fina
176180
} catch (Exception e) {
177181
String errmsg = Logger.getMessageAndStackTraceString("Failed to send execution intent to " + executionIntent.getComponent().toString(), e);
178182
Logger.logErrorAndShowToast(context, LOG_TAG, errmsg);
179-
PluginUtils.sendImmediateResultToPluginHostApp(receiver, originalIntent, null, null, null, TaskerPlugin.Setting.RESULT_CODE_FAILED, errmsg);
183+
PluginUtils.sendImmediateResultToPluginHostApp(receiver, originalIntent, TaskerPlugin.Setting.RESULT_CODE_FAILED, errmsg);
180184
}
181185
}
182186

187+
/**
188+
* Send immediate result to plugin host app in a variables bundle.
189+
*
190+
* @param receiver The {@link BroadcastReceiver} of the originalIntent.
191+
* @param originalIntent The original {@link Intent} received by {@link FireReceiver}.
192+
* @param errCode The value for {@link #PLUGIN_VARIABLE_ERR} variable of plugin action.
193+
* @param errmsg The value for {@link #PLUGIN_VARIABLE_ERRMSG} variable of plugin action.
194+
*/
195+
public static void sendImmediateResultToPluginHostApp(final BroadcastReceiver receiver, final Intent originalIntent,
196+
final int errCode, final String errmsg) {
197+
sendImmediateResultToPluginHostApp(receiver, originalIntent, null, null,
198+
null, null, null, errCode, errmsg);
199+
}
200+
183201
/**
184202
* Send immediate result to plugin host app in a variables bundle.
185203
*
186204
* @param receiver The {@link BroadcastReceiver} of the originalIntent.
187205
* @param originalIntent The original {@link Intent} received by {@link FireReceiver}.
188206
* @param stdout The value for {@link #PLUGIN_VARIABLE_STDOUT} variable of plugin action.
207+
* @param stdoutOriginalLength The value for {@link #PLUGIN_VARIABLE_STDOUT_ORIGINAL_LENGTH}
208+
* variable of plugin action.
189209
* @param stderr The value for {@link #PLUGIN_VARIABLE_STDERR} variable of plugin action.
210+
* @param stderrOriginalLength The value for {@link #PLUGIN_VARIABLE_STDERR_ORIGINAL_LENGTH}
211+
* variable of plugin action.
190212
* @param exitCode The value for {@link #PLUGIN_VARIABLE_EXIT_CODE} variable of plugin action.
191213
* @param errCode The value for {@link #PLUGIN_VARIABLE_ERR} variable of plugin action.
192214
* @param errmsg The value for {@link #PLUGIN_VARIABLE_ERRMSG} variable of plugin action.
193215
*/
194-
public static void sendImmediateResultToPluginHostApp(final BroadcastReceiver receiver, final Intent originalIntent, final String stdout, final String stderr, final String exitCode, final int errCode, final String errmsg) {
216+
public static void sendImmediateResultToPluginHostApp(final BroadcastReceiver receiver, final Intent originalIntent,
217+
final String stdout, String stdoutOriginalLength,
218+
final String stderr, String stderrOriginalLength,
219+
final String exitCode, final int errCode, final String errmsg) {
195220
if (receiver == null) return;
196221

197222
// If timeout for plugin action is 0, then don't send anything
@@ -202,7 +227,8 @@ public static void sendImmediateResultToPluginHostApp(final BroadcastReceiver re
202227
Logger.logInfo(LOG_TAG, "Sending immediate result to plugin host app. " + PLUGIN_VARIABLE_ERR + ": " + ((err == TaskerPlugin.Setting.RESULT_CODE_OK) ? "success" : "failed") + " (" + err + ")");
203228

204229
if (TaskerPlugin.Setting.hostSupportsVariableReturn(originalIntent.getExtras())) {
205-
final Bundle varsBundle = createVariablesBundle(stdout, stderr, exitCode, err, errmsg);
230+
final Bundle varsBundle = createVariablesBundle(stdout, stdoutOriginalLength,
231+
stderr, stderrOriginalLength, exitCode, err, errmsg);
206232
TaskerPlugin.addVariableBundle(receiver.getResultExtras(true), varsBundle);
207233
}
208234

@@ -248,7 +274,9 @@ public static void sendPendingResultToPluginHostApp(final Context context, final
248274

249275
final Bundle varsBundle = createVariablesBundle(
250276
resultBundle.getString(TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE_STDOUT, ""),
277+
resultBundle.getString(TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE_STDOUT_ORIGINAL_LENGTH, ""),
251278
resultBundle.getString(TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE_STDERR, ""),
279+
resultBundle.getString(TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE_STDERR_ORIGINAL_LENGTH, ""),
252280
exitCode, err, resultBundle.getString(TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE_ERRMSG, ""));
253281

254282
if(context != null)
@@ -287,8 +315,7 @@ public static void processPluginExecutionCommandError(final Context context, fin
287315
true, isExecutionCommandLoggingEnabled));
288316

289317
PluginUtils.sendImmediateResultToPluginHostApp(receiver, originalIntent,
290-
null, null, null, errCode,
291-
ResultData.getErrorsListMinimalString(executionCommand.resultData));
318+
errCode, ResultData.getErrorsListMinimalString(executionCommand.resultData));
292319
}
293320

294321

@@ -297,17 +324,25 @@ public static void processPluginExecutionCommandError(final Context context, fin
297324
* Create variables bundle to send back to plugin host app.
298325
*
299326
* @param stdout The value for {@link #PLUGIN_VARIABLE_STDOUT} variable of plugin action.
327+
* @param stdoutOriginalLength The value for {@link #PLUGIN_VARIABLE_STDOUT_ORIGINAL_LENGTH}
328+
* variable of plugin action.
300329
* @param stderr The value for {@link #PLUGIN_VARIABLE_STDERR} variable of plugin action.
330+
* @param stderrOriginalLength The value for {@link #PLUGIN_VARIABLE_STDERR_ORIGINAL_LENGTH}
331+
* variable of plugin action.
301332
* @param exitCode The value for {@link #PLUGIN_VARIABLE_EXIT_CODE} variable of plugin action.
302333
* @param errCode The value for {@link #PLUGIN_VARIABLE_ERR} variable of plugin action.
303334
* @param errmsg The value for {@link #PLUGIN_VARIABLE_ERRMSG} variable of plugin action.
304335
* @return Returns the variables {@code Bundle}.
305336
*/
306-
public static Bundle createVariablesBundle(String stdout, String stderr, String exitCode, int errCode, String errmsg) {
337+
public static Bundle createVariablesBundle(String stdout, String stdoutOriginalLength,
338+
String stderr, String stderrOriginalLength,
339+
String exitCode, int errCode, String errmsg) {
307340

308341
Logger.logDebugExtended(LOG_TAG, "Variables bundle for plugin host app:\n" +
309342
PLUGIN_VARIABLE_STDOUT + ": `" + stdout + "`\n" +
343+
PLUGIN_VARIABLE_STDOUT_ORIGINAL_LENGTH + ": `" + stdoutOriginalLength + "`\n" +
310344
PLUGIN_VARIABLE_STDERR + ": `" + stderr + "`\n" +
345+
PLUGIN_VARIABLE_STDERR_ORIGINAL_LENGTH + ": `" + stderrOriginalLength + "`\n" +
311346
PLUGIN_VARIABLE_EXIT_CODE + ": `" + exitCode + "`\n" +
312347
PLUGIN_VARIABLE_ERR + ": `" + errCode + "`\n" +
313348
PLUGIN_VARIABLE_ERRMSG + ": `" + errmsg + "`");
@@ -322,16 +357,22 @@ public static Bundle createVariablesBundle(String stdout, String stderr, String
322357
// since if multiple actions are run in the same task, some variables from previous actions
323358
// may still be set and get mixed in with current ones.
324359
if (stdout == null) stdout = "";
360+
if (stdoutOriginalLength == null) stdoutOriginalLength = "";
325361
if (stderr == null) stderr = "";
362+
if (stderrOriginalLength == null) stderrOriginalLength = "";
326363
if (exitCode == null) exitCode = "";
327364
if (errmsg == null) errmsg = "";
328365

329366
final Bundle variablesBundle = new Bundle();
330367

331368
if (isPluginHostAppVariableNameValid(PLUGIN_VARIABLE_STDOUT))
332369
variablesBundle.putString(PLUGIN_VARIABLE_STDOUT, stdout);
370+
if (isPluginHostAppVariableNameValid(PLUGIN_VARIABLE_STDOUT_ORIGINAL_LENGTH))
371+
variablesBundle.putString(PLUGIN_VARIABLE_STDOUT_ORIGINAL_LENGTH, stdoutOriginalLength);
333372
if (isPluginHostAppVariableNameValid(PLUGIN_VARIABLE_STDERR))
334373
variablesBundle.putString(PLUGIN_VARIABLE_STDERR, stderr);
374+
if (isPluginHostAppVariableNameValid(PLUGIN_VARIABLE_STDERR_ORIGINAL_LENGTH))
375+
variablesBundle.putString(PLUGIN_VARIABLE_STDERR_ORIGINAL_LENGTH, stderrOriginalLength);
335376
if (isPluginHostAppVariableNameValid(PLUGIN_VARIABLE_EXIT_CODE))
336377
variablesBundle.putString(PLUGIN_VARIABLE_EXIT_CODE, exitCode);
337378
if (isPluginHostAppVariableNameValid(PLUGIN_VARIABLE_ERRMSG))

0 commit comments

Comments
 (0)