#! /usr/bin/env python # # byobu-select-session # Copyright (C) 2010 Canonical Ltd. # # Authors: Dustin Kirkland # # 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 . import commands, os, re, sys 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", "screen") choice = "" sessions = [] text = [] def get_sessions(): sessions = [] i = 0 if commands.getstatusoutput("command -v screen")[0] == 0: 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 commands.getstatusoutput("command -v tmux")[0] == 0: 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 attach_session(session): print("Attaching: [%s]\n" % session) items = session.split("____", 2) if items[0] == "tmux": os.execvp("tmux", ["", "attach", "-t", items[1]]) else: os.execvp("screen", ["", "-AOxRR", items[1]]) 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 = 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: # Attach to the only session; must use the binary, not the wrapper! if BYOBU_BACKEND == "tmux": os.execvp("tmux", ["", "attach"]) else: os.execvp("screen", ["", "-AOxRR"]) 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])