Skip to content

Commit a1713d0

Browse files
committed
[Terminal] Display commands in bold with $ prepended
Signed-off-by: Muntashir Al-Islam <muntashirakon@riseup.net>
1 parent 3d502a9 commit a1713d0

1 file changed

Lines changed: 37 additions & 17 deletions

File tree

app/src/main/java/io/github/muntashirakon/AppManager/runner/TermActivity.java

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,24 @@
22

33
package io.github.muntashirakon.AppManager.runner;
44

5+
import android.graphics.Typeface;
56
import android.os.Bundle;
67
import android.os.PowerManager;
8+
import android.text.Editable;
9+
import android.text.Spanned;
10+
import android.text.style.StyleSpan;
711
import android.view.MenuItem;
812
import android.view.inputmethod.EditorInfo;
13+
import android.widget.TextView;
914

10-
import androidx.annotation.AnyThread;
15+
import androidx.annotation.MainThread;
1116
import androidx.annotation.NonNull;
1217
import androidx.annotation.Nullable;
1318
import androidx.appcompat.widget.AppCompatEditText;
1419
import androidx.appcompat.widget.AppCompatTextView;
1520

1621
import java.io.BufferedOutputStream;
17-
import java.io.BufferedReader;
18-
import java.io.InputStreamReader;
22+
import java.io.InputStream;
1923
import java.io.OutputStream;
2024
import java.nio.charset.StandardCharsets;
2125
import java.util.Objects;
@@ -35,6 +39,7 @@ public class TermActivity extends BaseActivity {
3539
private Process mProc;
3640
private OutputStream mProcessOutputStream;
3741
private PowerManager.WakeLock mWakeLock;
42+
private boolean mCommandInProgress;
3843
private final ExecutorService mExecutor = Executors.newFixedThreadPool(3);
3944

4045
@Override
@@ -43,16 +48,21 @@ protected void onAuthenticated(@Nullable Bundle savedInstanceState) {
4348
setSupportActionBar(findViewById(R.id.toolbar));
4449
mCommandInput = findViewById(R.id.command_input);
4550
mCommandOutput = findViewById(R.id.command_output);
51+
mCommandOutput.setText("", TextView.BufferType.EDITABLE);
4652
mCommandInput.setOnEditorActionListener((v, actionId, event) -> {
4753
if (actionId == EditorInfo.IME_ACTION_DONE) {
48-
String command = Objects.requireNonNull(mCommandInput.getText()) + "\n";
54+
String command = Objects.requireNonNull(mCommandInput.getText()).toString();
55+
appendBoldOutput((mCommandInProgress ? " " : "$ ") + command);
56+
mCommandInProgress = command.endsWith("\\");
57+
appendOutput("\n");
4958
if (mProcessOutputStream != null) {
5059
if (!ProcessCompat.isAlive(mProc)) {
5160
// Process is dead
5261
return false;
5362
}
5463
try {
5564
mProcessOutputStream.write(command.getBytes(StandardCharsets.UTF_8));
65+
mProcessOutputStream.write("\n".getBytes(StandardCharsets.UTF_8));
5666
mProcessOutputStream.flush();
5767
} catch (Throwable e) {
5868
e.printStackTrace();
@@ -89,25 +99,27 @@ private void initShell() {
8999
mProc = ProcessCompat.exec(new String[]{"sh"}/*, new String[]{"TERM=xterm-256color"}*/);
90100
mProcessOutputStream = new BufferedOutputStream(mProc.getOutputStream());
91101
mExecutor.submit(() -> {
92-
try (BufferedReader reader = new BufferedReader(new InputStreamReader(mProc.getInputStream()))) {
93-
String line;
94-
while ((line = reader.readLine()) != null) {
102+
try (InputStream in = mProc.getInputStream()) {
103+
byte[] buffer = new byte[1024];
104+
int len;
105+
while ((len = in.read(buffer)) != -1) {
95106
synchronized (mLock) {
96-
String finalLine = line;
97-
runOnUiThread(() -> mCommandOutput.append(finalLine + "\n"));
107+
String chunk = new String(buffer, 0, len, StandardCharsets.UTF_8);
108+
runOnUiThread(() -> appendOutput(chunk));
98109
}
99110
}
100111
} catch (Throwable e) {
101112
e.printStackTrace();
102113
}
103114
});
104115
mExecutor.submit(() -> {
105-
try (BufferedReader reader = new BufferedReader(new InputStreamReader(mProc.getErrorStream()))) {
106-
String line;
107-
while ((line = reader.readLine()) != null) {
116+
try (InputStream in = mProc.getErrorStream()) {
117+
byte[] buffer = new byte[1024];
118+
int len;
119+
while ((len = in.read(buffer)) != -1) {
108120
synchronized (mLock) {
109-
String finalLine = line;
110-
runOnUiThread(() -> mCommandOutput.append(finalLine + "\n"));
121+
String chunk = new String(buffer, 0, len, StandardCharsets.UTF_8);
122+
runOnUiThread(() -> appendOutput(chunk));
111123
}
112124
}
113125
} catch (Throwable e) {
@@ -123,8 +135,16 @@ private void initShell() {
123135
});
124136
}
125137

126-
@AnyThread
127-
private void appendOutput(String line) {
128-
mCommandOutput.append(line + "\n");
138+
@MainThread
139+
private void appendOutput(String text) {
140+
mCommandOutput.append(text);
141+
}
142+
143+
@MainThread
144+
private void appendBoldOutput(String boldText) {
145+
Editable editable = mCommandOutput.getEditableText();
146+
int start = editable.length();
147+
editable.append(boldText);
148+
editable.setSpan(new StyleSpan(Typeface.BOLD), start, editable.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
129149
}
130150
}

0 commit comments

Comments
 (0)