Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -650,18 +650,22 @@ class ScreenOperatorAccessibilityService : AccessibilityService() {
?: intent.getBundleExtra("result")

val extras = intent.extras
val stdout = sequenceOf(
resultBundle?.getString("com.termux.app.extra.TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE_STDOUT"),
resultBundle?.getString("stdout"),
extras?.getString("com.termux.app.extra.TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE_STDOUT"),
extras?.getString("stdout")
).firstOrNull { !it.isNullOrBlank() }.orEmpty()
val stderr = sequenceOf(
resultBundle?.getString("com.termux.app.extra.TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE_STDERR"),
resultBundle?.getString("stderr"),
extras?.getString("com.termux.app.extra.TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE_STDERR"),
extras?.getString("stderr")
).firstOrNull { !it.isNullOrBlank() }.orEmpty()
val stdout = TermuxOutputPreferences.removeProcessCompletedPrompt(
sequenceOf(
resultBundle?.getString("com.termux.app.extra.TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE_STDOUT"),
resultBundle?.getString("stdout"),
extras?.getString("com.termux.app.extra.TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE_STDOUT"),
extras?.getString("stdout")
).firstOrNull { !it.isNullOrBlank() }.orEmpty()
)
val stderr = TermuxOutputPreferences.removeProcessCompletedPrompt(
sequenceOf(
resultBundle?.getString("com.termux.app.extra.TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE_STDERR"),
resultBundle?.getString("stderr"),
extras?.getString("com.termux.app.extra.TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE_STDERR"),
extras?.getString("stderr")
).firstOrNull { !it.isNullOrBlank() }.orEmpty()
)
val exitCode = when {
resultBundle?.containsKey("com.termux.app.extra.TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE_EXIT_CODE") == true -> {
resultBundle.getInt("com.termux.app.extra.TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE_EXIT_CODE", Int.MIN_VALUE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import android.content.Context
object TermuxOutputPreferences {
private const val PREF_NAME = "termux_output_prefs"
private const val KEY_PENDING_OUTPUT = "pending_output"
private const val PROCESS_COMPLETED_PROMPT = "[Process completed - press Enter]"

fun appendOutput(context: Context, output: String) {
if (output.isBlank()) return
val sanitizedOutput = removeProcessCompletedPrompt(output).trim()
if (sanitizedOutput.isBlank()) return
val prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
val existing = prefs.getString(KEY_PENDING_OUTPUT, "").orEmpty()
val merged = if (existing.isBlank()) output else "$existing\n\n$output"
val merged = if (existing.isBlank()) sanitizedOutput else "$existing\n\n$sanitizedOutput"
val committed = prefs.edit().putString(KEY_PENDING_OUTPUT, merged).commit()
if (!committed) {
throw IllegalStateException("Failed to persist pending Termux output")
Expand All @@ -19,12 +21,21 @@ object TermuxOutputPreferences {

fun consumeOutput(context: Context): String? {
val prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
val value = prefs.getString(KEY_PENDING_OUTPUT, "").orEmpty().trim()
val value = removeProcessCompletedPrompt(prefs.getString(KEY_PENDING_OUTPUT, "").orEmpty()).trim()
if (value.isBlank()) return null
val committed = prefs.edit().remove(KEY_PENDING_OUTPUT).commit()
if (!committed) {
throw IllegalStateException("Failed to clear consumed Termux output")
}
return value
}

fun removeProcessCompletedPrompt(output: String): String {
val lines = output.lineSequence().toList()
val promptIndex = lines.indexOfLast { it.isNotBlank() }
if (promptIndex < 0 || lines[promptIndex].trim() != PROCESS_COMPLETED_PROMPT) {
return output
}
return lines.take(promptIndex).joinToString("\n")
}
}
Loading