/usr/bin/byobu-select-session is in byobu 5.16-1.1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | #!/usr/bin/env python
#
# byobu-select-session
# Copyright (C) 2010 Canonical Ltd.
# Copyright (C) 2012 Dustin Kirkland <dustin.kirkland@gmail.com>
#
# Authors: Dustin Kirkland <dustin.kirkland@gmail.com>
# Ryan C. Thompson <rct@thompsonclan.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import commands, os, re, sys, subprocess
PKG = "byobu"
SHELL = os.getenv("SHELL", "/bin/bash")
HOME=os.getenv("HOME")
BYOBU_CONFIG_DIR=os.getenv("BYOBU_CONFIG_DIR", HOME+"/.byobu")
BYOBU_BACKEND=os.getenv("BYOBU_BACKEND", "tmux")
choice = ""
sessions = []
text = []
BYOBU_UPDATE_ENVVARS=["DISPLAY", "DBUS_SESSION_BUS_ADDRESS", "SESSION_MANAGER", "GPG_AGENT_INFO",
"XDG_SESSION_COOKIE", "XDG_SESSION_PATH", "GNOME_KEYRING_CONTROL",
"GNOME_KEYRING_PID", "GPG_AGENT_INFO", "SSH_AUTH_SOCK", "SSH_AGENT_PID"]
def get_sessions():
sessions = []
i = 0
if BYOBU_BACKEND == "screen":
output = commands.getstatusoutput("screen -ls")
if output[0] >= 0:
for s in output[1].split("\n"):
s = re.sub(r'\s+', ' ', s)
# Ignore hidden sessions (named sessions that start with a ".")
if (s.find(" ") == 0 and len(s) > 1 and s.count("..") == 0):
text.append("screen: %s" % s.strip())
items = s.split(" ")
sessions.append("screen____%s" % items[1])
i += 1
if BYOBU_BACKEND == "tmux":
output = commands.getstatusoutput("tmux list-sessions")
if output[0] == 0:
for s in output[1].split("\n"):
text.append("tmux: %s" % s.strip())
items = s.split(":")
sessions.append("tmux____%s" % items[0])
i += 1
return sessions
def update_environment(session):
backend, session_name = session.split("____", 2)
for var in BYOBU_UPDATE_ENVVARS:
value = os.getenv(var)
if value:
if backend == "tmux":
cmd = ["tmux", "setenv", "-t", session_name, var, value]
else:
cmd = ["screen", "-S", session_name, "-X", "setenv", var, value]
print "Sending variable: %s" % (cmd, )
subprocess.call(cmd, stdout=open(os.devnull, "w"))
def attach_session(session):
print("Attaching: [%s]\n" % session)
update_environment(session)
backend, session_name = session.split("____", 2)
# must use the binary, not the wrapper!
if backend == "tmux":
os.execvp("tmux", ["", "-2", "attach", "-t", session_name])
else:
os.execvp("screen", ["", "-AOxRR", session_name])
sessions = get_sessions()
show_shell = os.path.exists("%s/.always-select" % (BYOBU_CONFIG_DIR))
if len(sessions) > 1 or show_shell:
sessions.append("NEW")
text.append("Create a new Byobu session (%s)" % BYOBU_BACKEND)
sessions.append("SHELL")
text.append("Run a shell without Byobu (%s)" % SHELL)
if len(sessions) > 1:
sys.stdout.write("\nByobu sessions...\n\n")
tries = 0
while tries < 3:
i = 1
for s in text:
sys.stdout.write(" %d. %s\n" % (i, s))
i += 1
try:
choice = int(raw_input("\nChoose 1-%d [1]: " % (i-1)))
if choice >= 1 and choice < i:
break
else:
tries += 1
choice = ""
sys.stderr.write("\nERROR: Invalid input\n");
except KeyboardInterrupt:
print
sys.exit(0)
except:
if choice == "":
choice = 1
break
tries += 1
choice = ""
sys.stderr.write("\nERROR: Invalid input\n");
elif len(sessions) == 1:
# Auto-select the only session
choice = 1
if BYOBU_BACKEND == "tmux" and os.getenv("TMUX"):
sys.stderr.write("ERROR: Sessions should be nested with care. Unset $TMUX to force.\n")
sys.exit(1)
elif BYOBU_BACKEND == "screen" and re.match("screen", os.getenv("TERM")):
sys.stderr.write("ERROR: Sessions should be nested with care. Remove 'screen' from $TERM to force.\n")
sys.exit(1)
if choice:
if sessions[choice-1] == "NEW":
# Create a new session
if BYOBU_BACKEND == "tmux":
os.execvp("byobu", ["", "new-session", SHELL])
else:
os.execvp("byobu", ["", SHELL])
elif sessions[choice-1] == "SHELL":
os.execvp(SHELL, [SHELL])
else:
# Attach to the chosen session; must use the binary, not the wrapper!
attach_session(sessions[choice-1])
# No valid selection, default to the youngest session, create if necessary
if BYOBU_BACKEND == "tmux":
args = ""
else:
args = "-AOxRR"
os.execvp("byobu", ["", args])
|