Is there a way to uniquely identify the current Windows Terminal viewport within a WSL2 Ubuntu bash environment?

24 views Asked by At

Or, a way to obtain the current window/tab/pane index numbers for a Windows Terminal "viewport" from within a WSL2 Ubuntu bash shell?

Background and Use Case:

I would like to use Windows Terminal as my primary terminal multiplexer and use Gnu Screen only for session persistence in my WSL2 + Ubuntu dev environment. That is, I don't want open up multiple tabs and panes within Gnu Screen itself, but instead want to use Windows Terminal to open multiple bash sessions in different windows, tabs, and panes to my WSL2 Ubuntu instance, arranged in whatever dynamic layout I desire.

This works, but the problem is when I manually close Windows Terminal or I have to reboot my computer, upon restart Windows Terminal will faithfully recreate its layout and reopen fresh bash shells, but I lose the state of each previous bash session.

I've used Gnu Screen for a long time and would like to use it to reconnect my detached screen sessions. But I don't want to just reattach the 'next available' detached screen session randomly to each recreated viewport. Rather, each screen session should be reconnected to the same corresponding bash shell associated with the original window/tab/pane viewport in the recreated Windows Terminal layout after it restarts. This way, no matter what order I create my windows, tabs, and panes in Windows Terminal, or close/reopen or move them around, my bash screen sessions will be preserved in the same original viewport after a restart.

In theory this should be easy. As Windows Terminal recreates its layout, the .bashrc script of each reopened bash shell in each viewport could be instructed to reattach to its former screen session, thus restoring all bash viewports to their matching bash screen sessions exactly as before.

To do this, I would need to name each screen session as it is created with a unique identifier associated with the specific Windows Terminal viewport ID that it is tied to. Or worst case, construct a unique ID from the window/tab/panel index numbers associated with the bash viewport in Windows Terminal. And if needed, I could even run a background bash process to monitor these WT values for each bash shell and rename its associated screen session automatically if they change.

I did notice the "WT_SESSION" and WSL_INTEROP bash shell environment variables in my WSL2 Ubuntu bash environment. Unfortunately those values change after restarting Windows Terminal, even for the same recreated viewport and bash shell. And I have not been able to find a way to obtain the current Windows Terminal window/tab/pane index values from within the bash shell to construct my own unique viewport identifier to name my screen sessions with.

Does anyone know how I could get the window/tab/pane index numbers of the current Windows Terminal viewport from within the Ubuntu bash environment? Or obtain a unique layout "viewport" identifier that persists between restarts of Windows Terminal?

0

There are 0 answers