Skip to content

Commit 0b4bbaf

Browse files
Allow users to adjust terminal transcript rows with termux.properties
This `terminal-transcript-rows` key can be used to adjust the terminal transcript rows. The user can set an integer value between `100` and `50000`. The default value is still `2000`. So adding an entry like `terminal-transcript-rows=10000` to `termux.properties` file will allow users to scroll back ~10000 lines of command output. After updating the value, termux must be restarted. You can also run `termux-reload-settings` command so that termux loads the updated value, but only new sessions will use the updated value, existing sessions will not be affected. You can test this with the following, where `70` is number of `x` characters per line and `10001` is the number of lines to print. `x="$(printf 'x%.0s' {1..70})"; for i in {1..10001}; do echo "$i:$x"; done` Be advised that using large values may have a performance impact depending on your device capabilities, so use at your own risk. Closes #2071
1 parent e7dd0ee commit 0b4bbaf

File tree

9 files changed

+128
-21
lines changed

9 files changed

+128
-21
lines changed

app/src/main/java/com/termux/app/TermuxActivity.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,9 @@ private void reloadActivityStyling() {
846846
if (mTermuxTerminalViewClient != null)
847847
mTermuxTerminalViewClient.onReload();
848848

849+
if (mTermuxService != null)
850+
mTermuxService.setTerminalTranscriptRows();
851+
849852
// To change the activity and drawer theme, activity needs to be recreated.
850853
// But this will destroy the activity, and will call the onCreate() again.
851854
// We need to investigate if enabling this is wise, since all stored variables and

app/src/main/java/com/termux/app/TermuxService.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import android.widget.ArrayAdapter;
2121

2222
import com.termux.R;
23+
import com.termux.app.settings.properties.TermuxAppSharedProperties;
2324
import com.termux.app.terminal.TermuxTerminalSessionClient;
2425
import com.termux.app.utils.PluginUtils;
2526
import com.termux.shared.termux.TermuxConstants;
@@ -106,6 +107,8 @@ class LocalBinder extends Binder {
106107
/** If the user has executed the {@link TERMUX_SERVICE#ACTION_STOP_SERVICE} intent. */
107108
boolean mWantsToStop = false;
108109

110+
public Integer mTerminalTranscriptRows;
111+
109112
private static final String LOG_TAG = "TermuxService";
110113

111114
@Override
@@ -503,6 +506,7 @@ public synchronized TermuxSession createTermuxSession(ExecutionCommand execution
503506
// If the execution command was started for a plugin, only then will the stdout be set
504507
// Otherwise if command was manually started by the user like by adding a new terminal session,
505508
// then no need to set stdout
509+
executionCommand.terminalTranscriptRows = getTerminalTranscriptRows();
506510
TermuxSession newTermuxSession = TermuxSession.execute(this, executionCommand, getTermuxTerminalSessionClient(), this, sessionName, executionCommand.isPluginExecutionCommand);
507511
if (newTermuxSession == null) {
508512
Logger.logError(LOG_TAG, "Failed to execute new TermuxSession command for:\n" + executionCommand.getCommandIdAndLabelLogString());
@@ -560,6 +564,19 @@ public void onTermuxSessionExited(final TermuxSession termuxSession) {
560564
updateNotification();
561565
}
562566

567+
/** Get the terminal transcript rows to be used for new {@link TermuxSession}. */
568+
public Integer getTerminalTranscriptRows() {
569+
if (mTerminalTranscriptRows == null)
570+
setTerminalTranscriptRows();
571+
return mTerminalTranscriptRows;
572+
}
573+
574+
public void setTerminalTranscriptRows() {
575+
// TermuxService only uses this termux property currently, so no need to load them all into
576+
// an internal values map like TermuxActivity does
577+
mTerminalTranscriptRows = TermuxAppSharedProperties.getTerminalTranscriptRows(this);
578+
}
579+
563580

564581

565582

app/src/main/java/com/termux/app/settings/properties/TermuxAppSharedProperties.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.termux.app.terminal.io.KeyboardShortcut;
66
import com.termux.app.terminal.io.extrakeys.ExtraKeysInfo;
77
import com.termux.shared.logger.Logger;
8-
import com.termux.shared.settings.properties.SharedPropertiesParser;
98
import com.termux.shared.settings.properties.TermuxPropertyConstants;
109
import com.termux.shared.settings.properties.TermuxSharedProperties;
1110

@@ -17,7 +16,7 @@
1716

1817
import javax.annotation.Nonnull;
1918

20-
public class TermuxAppSharedProperties extends TermuxSharedProperties implements SharedPropertiesParser {
19+
public class TermuxAppSharedProperties extends TermuxSharedProperties {
2120

2221
private ExtraKeysInfo mExtraKeysInfo;
2322
private List<KeyboardShortcut> mSessionShortcuts = new ArrayList<>();
@@ -96,4 +95,13 @@ public ExtraKeysInfo getExtraKeysInfo() {
9695
return mExtraKeysInfo;
9796
}
9897

98+
99+
100+
/**
101+
* Load the {@link TermuxPropertyConstants#KEY_TERMINAL_TRANSCRIPT_ROWS} value from termux properties file on disk.
102+
*/
103+
public static int getTerminalTranscriptRows(Context context) {
104+
return (int) TermuxSharedProperties.getInternalPropertyValue(context, TermuxPropertyConstants.KEY_TERMINAL_TRANSCRIPT_ROWS);
105+
}
106+
99107
}

terminal-emulator/src/main/java/com/termux/terminal/TerminalEmulator.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ public final class TerminalEmulator {
137137
/** The number of character rows and columns in the terminal screen. */
138138
public int mRows, mColumns;
139139

140+
/** The number of terminal transcript rows that can be scrolled back to. */
141+
public static final int TERMINAL_TRANSCRIPT_ROWS_MIN = 100;
142+
public static final int TERMINAL_TRANSCRIPT_ROWS_MAX = 50000;
143+
public static final int DEFAULT_TERMINAL_TRANSCRIPT_ROWS = 2000;
144+
140145
/** The normal screen buffer. Stores the characters that appear on the screen of the emulated terminal. */
141146
private final TerminalBuffer mMainBuffer;
142147
/**
@@ -294,9 +299,9 @@ static int mapDecSetBitToInternalBit(int decsetBit) {
294299
}
295300
}
296301

297-
public TerminalEmulator(TerminalOutput session, int columns, int rows, int transcriptRows, TerminalSessionClient client) {
302+
public TerminalEmulator(TerminalOutput session, int columns, int rows, Integer transcriptRows, TerminalSessionClient client) {
298303
mSession = session;
299-
mScreen = mMainBuffer = new TerminalBuffer(columns, transcriptRows, rows);
304+
mScreen = mMainBuffer = new TerminalBuffer(columns, getTerminalTranscriptRows(transcriptRows), rows);
300305
mAltBuffer = new TerminalBuffer(columns, rows, rows);
301306
mClient = client;
302307
mRows = rows;
@@ -317,6 +322,13 @@ public boolean isAlternateBufferActive() {
317322
return mScreen == mAltBuffer;
318323
}
319324

325+
private int getTerminalTranscriptRows(Integer transcriptRows) {
326+
if (transcriptRows == null || transcriptRows < TERMINAL_TRANSCRIPT_ROWS_MIN || transcriptRows > TERMINAL_TRANSCRIPT_ROWS_MAX)
327+
return DEFAULT_TERMINAL_TRANSCRIPT_ROWS;
328+
else
329+
return transcriptRows;
330+
}
331+
320332
/**
321333
* @param mouseButton one of the MOUSE_* constants of this class.
322334
*/

terminal-emulator/src/main/java/com/termux/terminal/TerminalSession.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,17 @@ public final class TerminalSession extends TerminalOutput {
7474
private final String mCwd;
7575
private final String[] mArgs;
7676
private final String[] mEnv;
77+
private final Integer mTranscriptRows;
78+
7779

7880
private static final String LOG_TAG = "TerminalSession";
7981

80-
public TerminalSession(String shellPath, String cwd, String[] args, String[] env, TerminalSessionClient client) {
82+
public TerminalSession(String shellPath, String cwd, String[] args, String[] env, Integer transcriptRows, TerminalSessionClient client) {
8183
this.mShellPath = shellPath;
8284
this.mCwd = cwd;
8385
this.mArgs = args;
8486
this.mEnv = env;
87+
this.mTranscriptRows = transcriptRows;
8588
this.mClient = client;
8689
}
8790

@@ -118,7 +121,7 @@ public String getTitle() {
118121
* @param rows The number of rows in the terminal window.
119122
*/
120123
public void initializeEmulator(int columns, int rows) {
121-
mEmulator = new TerminalEmulator(this, columns, rows, /* transcript= */2000, mClient);
124+
mEmulator = new TerminalEmulator(this, columns, rows, mTranscriptRows, mClient);
122125

123126
int[] processId = new int[1];
124127
mTerminalFileDescriptor = JNI.createSubprocess(mShellPath, mCwd, mArgs, mEnv, processId, rows, columns);

termux-shared/src/main/java/com/termux/shared/models/ExecutionCommand.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ public int getValue() {
8181
public String workingDirectory;
8282

8383

84+
/** The terminal transcript rows for the {@link ExecutionCommand}. */
85+
public Integer terminalTranscriptRows;
86+
87+
8488
/** If the {@link ExecutionCommand} is a background or a foreground terminal session command. */
8589
public boolean inBackground;
8690
/** If the {@link ExecutionCommand} is meant to start a failsafe terminal session. */

termux-shared/src/main/java/com/termux/shared/settings/properties/TermuxPropertyConstants.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.google.common.collect.ImmutableBiMap;
44
import com.termux.shared.termux.TermuxConstants;
55
import com.termux.shared.logger.Logger;
6+
import com.termux.terminal.TerminalEmulator;
67
import com.termux.view.TerminalView;
78

89
import java.io.File;
@@ -11,7 +12,7 @@
1112
import java.util.Set;
1213

1314
/*
14-
* Version: v0.10.0
15+
* Version: v0.11.0
1516
*
1617
* Changelog
1718
*
@@ -49,6 +50,9 @@
4950
* - 0.10.0 (2021-05-15)
5051
* - Add `MAP_BACK_KEY_BEHAVIOUR`, `MAP_SOFT_KEYBOARD_TOGGLE_BEHAVIOUR`, `MAP_VOLUME_KEYS_BEHAVIOUR`.
5152
*
53+
* - 0.11.0 (2021-06-10)
54+
* - Add `*KEY_TERMINAL_TRANSCRIPT_ROWS*`.
55+
*
5256
*/
5357

5458
/**
@@ -131,6 +135,14 @@ public final class TermuxPropertyConstants {
131135

132136

133137

138+
/** Defines the key for the terminal transcript rows */
139+
public static final String KEY_TERMINAL_TRANSCRIPT_ROWS = "terminal-transcript-rows"; // Default: "terminal-transcript-rows"
140+
public static final int IVALUE_TERMINAL_TRANSCRIPT_ROWS_MIN = TerminalEmulator.TERMINAL_TRANSCRIPT_ROWS_MIN;
141+
public static final int IVALUE_TERMINAL_TRANSCRIPT_ROWS_MAX = TerminalEmulator.TERMINAL_TRANSCRIPT_ROWS_MAX;
142+
public static final int DEFAULT_IVALUE_TERMINAL_TRANSCRIPT_ROWS = TerminalEmulator.DEFAULT_TERMINAL_TRANSCRIPT_ROWS;
143+
144+
145+
134146

135147

136148
/* float */
@@ -259,6 +271,7 @@ public final class TermuxPropertyConstants {
259271
/* int */
260272
KEY_BELL_BEHAVIOUR,
261273
KEY_TERMINAL_CURSOR_BLINK_RATE,
274+
KEY_TERMINAL_TRANSCRIPT_ROWS,
262275

263276
/* float */
264277
KEY_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR,

termux-shared/src/main/java/com/termux/shared/settings/properties/TermuxSharedProperties.java

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
import javax.annotation.Nonnull;
1515

16-
public class TermuxSharedProperties implements SharedPropertiesParser {
16+
public class TermuxSharedProperties {
1717

1818
protected final Context mContext;
1919
protected final SharedProperties mSharedProperties;
@@ -24,7 +24,7 @@ public class TermuxSharedProperties implements SharedPropertiesParser {
2424
public TermuxSharedProperties(@Nonnull Context context) {
2525
mContext = context;
2626
mPropertiesFile = TermuxPropertyConstants.getTermuxPropertiesFile();
27-
mSharedProperties = new SharedProperties(context, mPropertiesFile, TermuxPropertyConstants.TERMUX_PROPERTIES_LIST, this);
27+
mSharedProperties = new SharedProperties(context, mPropertiesFile, TermuxPropertyConstants.TERMUX_PROPERTIES_LIST, new SharedPropertiesParserClient());
2828
loadTermuxPropertiesFromDisk();
2929
}
3030

@@ -146,24 +146,47 @@ public Object getInternalPropertyValue(String key, boolean cached) {
146146
// {@link #loadTermuxPropertiesFromDisk()} call
147147
// A null value can still be returned by
148148
// {@link #getInternalPropertyValueFromValue(Context,String,String)} for some keys
149-
value = getInternalPropertyValueFromValue(mContext, key, null);
149+
value = getInternalTermuxPropertyValueFromValue(mContext, key, null);
150150
Logger.logWarn(LOG_TAG, "The value for \"" + key + "\" not found in SharedProperties cache, force returning default value: `" + value + "`");
151151
return value;
152152
}
153153
} else {
154154
// We get the property value directly from file and return its internal value
155-
return getInternalPropertyValueFromValue(mContext, key, mSharedProperties.getProperty(key, false));
155+
return getInternalTermuxPropertyValueFromValue(mContext, key, mSharedProperties.getProperty(key, false));
156156
}
157157
}
158158

159+
160+
161+
162+
159163
/**
160-
* Override the
161-
* {@link SharedPropertiesParser#getInternalPropertyValueFromValue(Context,String,String)}
162-
* interface function.
164+
* Get the internal {@link Object} value for the key passed from the file returned by
165+
* {@link TermuxPropertyConstants#getTermuxPropertiesFile()}. The {@link Properties} object is
166+
* read directly from the file and internal value is returned for the property value against the key.
167+
*
168+
* @param context The context for operations.
169+
* @param key The key for which the internal object is required.
170+
* @return Returns the {@link Object} object. This will be {@code null} if key is not found or
171+
* the object stored against the key is {@code null}.
163172
*/
164-
@Override
165-
public Object getInternalPropertyValueFromValue(Context context, String key, String value) {
166-
return getInternalTermuxPropertyValueFromValue(context, key, value);
173+
public static Object getInternalPropertyValue(Context context, String key) {
174+
return SharedProperties.getInternalProperty(context, TermuxPropertyConstants.getTermuxPropertiesFile(), key, new SharedPropertiesParserClient());
175+
}
176+
177+
/**
178+
* The class that implements the {@link SharedPropertiesParser} interface.
179+
*/
180+
public static class SharedPropertiesParserClient implements SharedPropertiesParser {
181+
/**
182+
* Override the
183+
* {@link SharedPropertiesParser#getInternalPropertyValueFromValue(Context,String,String)}
184+
* interface function.
185+
*/
186+
@Override
187+
public Object getInternalPropertyValueFromValue(Context context, String key, String value) {
188+
return getInternalTermuxPropertyValueFromValue(context, key, value);
189+
}
167190
}
168191

169192
/**
@@ -194,6 +217,8 @@ public static Object getInternalTermuxPropertyValueFromValue(Context context, St
194217
return (int) getBellBehaviourInternalPropertyValueFromValue(value);
195218
case TermuxPropertyConstants.KEY_TERMINAL_CURSOR_BLINK_RATE:
196219
return (int) getTerminalCursorBlinkRateInternalPropertyValueFromValue(value);
220+
case TermuxPropertyConstants.KEY_TERMINAL_TRANSCRIPT_ROWS:
221+
return (int) getTerminalTranscriptRowsInternalPropertyValueFromValue(value);
197222

198223
/* float */
199224
case TermuxPropertyConstants.KEY_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR:
@@ -263,9 +288,9 @@ public static int getBellBehaviourInternalPropertyValueFromValue(String value) {
263288

264289
/**
265290
* Returns the int for the value if its not null and is between
266-
* {@code TermuxPropertyConstants#IVALUE_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR_MIN} and
267-
* {@code TermuxPropertyConstants#IVALUE_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR_MAX},
268-
* otherwise returns {@code TermuxPropertyConstants#DEFAULT_IVALUE_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR}.
291+
* {@code TermuxPropertyConstants#IVALUE_TERMINAL_CURSOR_BLINK_RATE_MIN} and
292+
* {@code TermuxPropertyConstants#IVALUE_TERMINAL_CURSOR_BLINK_RATE_MAX},
293+
* otherwise returns {@code TermuxPropertyConstants#DEFAULT_IVALUE_TERMINAL_CURSOR_BLINK_RATE}.
269294
*
270295
* @param value The {@link String} value to convert.
271296
* @return Returns the internal value for value.
@@ -279,6 +304,24 @@ public static float getTerminalCursorBlinkRateInternalPropertyValueFromValue(Str
279304
true, true, LOG_TAG);
280305
}
281306

307+
/**
308+
* Returns the int for the value if its not null and is between
309+
* {@code TermuxPropertyConstants#IVALUE_TERMINAL_TRANSCRIPT_ROWS_MIN} and
310+
* {@code TermuxPropertyConstants#IVALUE_TERMINAL_TRANSCRIPT_ROWS_MAX},
311+
* otherwise returns {@code TermuxPropertyConstants#DEFAULT_IVALUE_TERMINAL_TRANSCRIPT_ROWS}.
312+
*
313+
* @param value The {@link String} value to convert.
314+
* @return Returns the internal value for value.
315+
*/
316+
public static float getTerminalTranscriptRowsInternalPropertyValueFromValue(String value) {
317+
return SharedProperties.getDefaultIfNotInRange(TermuxPropertyConstants.KEY_TERMINAL_TRANSCRIPT_ROWS,
318+
DataUtils.getIntFromString(value, TermuxPropertyConstants.DEFAULT_IVALUE_TERMINAL_TRANSCRIPT_ROWS),
319+
TermuxPropertyConstants.DEFAULT_IVALUE_TERMINAL_TRANSCRIPT_ROWS,
320+
TermuxPropertyConstants.IVALUE_TERMINAL_TRANSCRIPT_ROWS_MIN,
321+
TermuxPropertyConstants.IVALUE_TERMINAL_TRANSCRIPT_ROWS_MAX,
322+
true, true, LOG_TAG);
323+
}
324+
282325
/**
283326
* Returns the int for the value if its not null and is between
284327
* {@code TermuxPropertyConstants#IVALUE_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR_MIN} and
@@ -435,6 +478,10 @@ public int getTerminalCursorBlinkRate() {
435478
return (int) getInternalPropertyValue(TermuxPropertyConstants.KEY_TERMINAL_CURSOR_BLINK_RATE, true);
436479
}
437480

481+
public int getTerminalTranscriptRows() {
482+
return (int) getInternalPropertyValue(TermuxPropertyConstants.KEY_TERMINAL_TRANSCRIPT_ROWS, true);
483+
}
484+
438485
public float getTerminalToolbarHeightScaleFactor() {
439486
return (float) getInternalPropertyValue(TermuxPropertyConstants.KEY_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR, true);
440487
}

termux-shared/src/main/java/com/termux/shared/shell/TermuxSession.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public static TermuxSession execute(@NonNull final Context context, @NonNull Exe
109109
Logger.logDebug(LOG_TAG, executionCommand.toString());
110110

111111
Logger.logDebug(LOG_TAG, "Running \"" + executionCommand.getCommandIdAndLabelLogString() + "\" TermuxSession");
112-
TerminalSession terminalSession = new TerminalSession(executionCommand.executable, executionCommand.workingDirectory, executionCommand.arguments, environment, terminalSessionClient);
112+
TerminalSession terminalSession = new TerminalSession(executionCommand.executable, executionCommand.workingDirectory, executionCommand.arguments, environment, executionCommand.terminalTranscriptRows, terminalSessionClient);
113113

114114
if (sessionName != null) {
115115
terminalSession.mSessionName = sessionName;

0 commit comments

Comments
 (0)