Skip to content

Commit e28be01

Browse files
committed
Create new terminal sessions with directory of active session
This mimics the behaviour of most tabbed terminal emulators. Fixes #1009.
1 parent 7f7c1ef commit e28be01

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,9 @@ void addNewSession(boolean failSafe, String sessionName) {
603603
new AlertDialog.Builder(this).setTitle(R.string.max_terminals_reached_title).setMessage(R.string.max_terminals_reached_message)
604604
.setPositiveButton(android.R.string.ok, null).show();
605605
} else {
606-
TerminalSession newSession = mTermService.createTermSession(null, null, null, failSafe);
606+
TerminalSession currentSession = getCurrentTermSession();
607+
String workingDirectory = (currentSession == null) ? null : currentSession.getCwd();
608+
TerminalSession newSession = mTermService.createTermSession(null, null, workingDirectory, failSafe);
607609
if (sessionName != null) {
608610
newSession.mSessionName = sessionName;
609611
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import android.system.OsConstants;
99
import android.util.Log;
1010

11+
import java.io.File;
1112
import java.io.FileDescriptor;
1213
import java.io.FileInputStream;
1314
import java.io.FileOutputStream;
@@ -339,4 +340,25 @@ public int getPid() {
339340
return mShellPid;
340341
}
341342

343+
/** Returns the shell's working directory or null if it was unavailable. */
344+
public String getCwd() {
345+
if (mShellPid < 1) {
346+
return null;
347+
}
348+
try {
349+
final String cwdSymlink = String.format("/proc/%s/cwd/", mShellPid);
350+
String outputPath = new File(cwdSymlink).getCanonicalPath();
351+
if (!outputPath.endsWith("/")) {
352+
outputPath += '/';
353+
}
354+
if (!cwdSymlink.equals(outputPath)) {
355+
return outputPath;
356+
}
357+
} catch (IOException | SecurityException e) {
358+
Log.e(EmulatorDebug.LOG_TAG, "Error getting current directory", e);
359+
}
360+
return null;
361+
}
362+
363+
342364
}

0 commit comments

Comments
 (0)