]> gitweb.fluxo.info Git - termplex.git/commitdiff
Fix: wtmux: session isolation through distinct sockets
authorSilvio Rhatto <rhatto@riseup.net>
Fri, 9 Aug 2024 01:57:01 +0000 (22:57 -0300)
committerSilvio Rhatto <rhatto@riseup.net>
Fri, 9 Aug 2024 01:57:01 +0000 (22:57 -0300)
wtmux

diff --git a/wtmux b/wtmux
index b577fd89775feb010671d207716effea20f77945..38532826603420ec85977ec872d8ee59a5748e3f 100755 (executable)
--- a/wtmux
+++ b/wtmux
 opts=""
 name="$1"
 state="$2"
+socket="-L${name}"
 
 # Session selection.
 if [ ! -z "${name}" ]; then
-  if ! tmux list-sessions 2> /dev/null | grep -q "^${name}:"; then
+  if [ "${name}" = "root" ]; then
+    sudo tmux attach
+    exit
+  fi
+
+  if ! tmux ${socket} list-sessions 2> /dev/null | grep -q "^${name}:"; then
     session="new-session -d -s ${name}"
 
     # Use a unique, per-session name
-    #session"${session} -t ${name}"
+    #session="${session} -t ${name}"
+
+    # Ensure there's a separate server running for each session.
+    #
+    # This achieves session isolation by using a unique socket per session.
+    #
+    # This solves tmux creating too many environment variables and triggering
+    # the following error when using programs such as "less" and "ranger":
+    #
+    #   Error: too many environment variables: >= MAX_ENVS (256)
+    #
+    # For a list of tmux environment variables available within a session,
+    # type "env | grep TMUX".
+    #
+    # Reference:
+    # https://github.com/tmux/tmux/wiki/Advanced-Use
+    session="${socket} ${session}"
 
-    if [ "${name}" = "root" ]; then
-      sudo tmux attach
-      exit
-    elif [ -f "$HOME/.tmux/${name}" ]; then
+    if [ -f "$HOME/.tmux/${name}" ]; then
       opts="$HOME/.tmux/${name}"
     elif [ -f "$HOME/.tmux/base" ]; then
       opts="$HOME/.tmux/base"
@@ -36,15 +55,15 @@ if [ ! -z "${name}" ]; then
     fi
 
     # Start session.
-    if [ ! -z "$opts" ]; then
-      tmux $session \; source-file $opts
+    if [ ! -z "${opts}" ]; then
+      tmux ${session} \; source-file ${opts}
     else
-      tmux $session
+      tmux ${session}
     fi
   fi
 
   if [ "$state" != "detached" ]; then
-    tmux attach -t ${name}
+    tmux ${socket} attach -t ${name}
   fi
 else
   tmux