mirror of
https://github.com/fankes/termux-app.git
synced 2025-09-07 19:14:04 +08:00
Update TextDataUtils.getTruncatedCommandOutput() to support truncation from start or end and for (truncated) prefix
This commit is contained in:
@@ -336,12 +336,13 @@ public class TermuxViewClient implements TerminalViewClient {
|
|||||||
if (session == null) return;
|
if (session == null) return;
|
||||||
|
|
||||||
String transcriptText = session.getEmulator().getScreen().getTranscriptTextWithoutJoinedLines().trim();
|
String transcriptText = session.getEmulator().getScreen().getTranscriptTextWithoutJoinedLines().trim();
|
||||||
|
if (transcriptText == null) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// See https://github.com/termux/termux-app/issues/1166.
|
// See https://github.com/termux/termux-app/issues/1166.
|
||||||
Intent intent = new Intent(Intent.ACTION_SEND);
|
Intent intent = new Intent(Intent.ACTION_SEND);
|
||||||
intent.setType("text/plain");
|
intent.setType("text/plain");
|
||||||
transcriptText = TextDataUtils.getTruncatedCommandOutput(transcriptText, 100_000);
|
transcriptText = TextDataUtils.getTruncatedCommandOutput(transcriptText, TextDataUtils.TRANSACTION_SIZE_LIMIT_IN_BYTES, false, true, false).trim();
|
||||||
intent.putExtra(Intent.EXTRA_TEXT, transcriptText);
|
intent.putExtra(Intent.EXTRA_TEXT, transcriptText);
|
||||||
intent.putExtra(Intent.EXTRA_SUBJECT, mActivity.getString(R.string.title_share_transcript));
|
intent.putExtra(Intent.EXTRA_SUBJECT, mActivity.getString(R.string.title_share_transcript));
|
||||||
mActivity.startActivity(Intent.createChooser(intent, mActivity.getString(R.string.title_share_transcript_with)));
|
mActivity.startActivity(Intent.createChooser(intent, mActivity.getString(R.string.title_share_transcript_with)));
|
||||||
|
@@ -8,18 +8,36 @@ import java.util.regex.Pattern;
|
|||||||
|
|
||||||
public class TextDataUtils {
|
public class TextDataUtils {
|
||||||
|
|
||||||
// https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:media2/media2-session/src/main/java/androidx/media2/session/MediaUtils.java
|
public static final int TRANSACTION_SIZE_LIMIT_IN_BYTES = 100 * 1024; // 100KB
|
||||||
public static final int TRANSACTION_SIZE_LIMIT_IN_BYTES = 256 * 1024; // 256KB
|
public static final int LOGGER_ENTRY_SIZE_LIMIT_IN_BYTES = 4 * 1024; // 4KB
|
||||||
|
|
||||||
public static String getTruncatedCommandOutput(String text, int maxLength) {
|
public static String getTruncatedCommandOutput(String text, int maxLength, boolean fromEnd, boolean onNewline, boolean addPrefix) {
|
||||||
if (text.length() > maxLength) {
|
if(text == null) return null;
|
||||||
|
|
||||||
|
String prefix = "(truncated) ";
|
||||||
|
|
||||||
|
if(addPrefix)
|
||||||
|
maxLength = maxLength - prefix.length();
|
||||||
|
|
||||||
|
if(maxLength < 0 || text.length() < maxLength) return text;
|
||||||
|
|
||||||
|
if (fromEnd) {
|
||||||
|
text = text.substring(0, Math.min(text.length(), maxLength));
|
||||||
|
} else {
|
||||||
int cutOffIndex = text.length() - maxLength;
|
int cutOffIndex = text.length() - maxLength;
|
||||||
int nextNewlineIndex = text.indexOf('\n', cutOffIndex);
|
|
||||||
if (nextNewlineIndex != -1 && nextNewlineIndex != text.length() - 1) {
|
if(onNewline) {
|
||||||
cutOffIndex = nextNewlineIndex + 1;
|
int nextNewlineIndex = text.indexOf('\n', cutOffIndex);
|
||||||
|
if (nextNewlineIndex != -1 && nextNewlineIndex != text.length() - 1) {
|
||||||
|
cutOffIndex = nextNewlineIndex + 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
text = text.substring(cutOffIndex).trim();
|
text = text.substring(cutOffIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(addPrefix)
|
||||||
|
text = prefix + text;
|
||||||
|
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user