Last active
March 21, 2020 01:05
-
-
Save ibejohn818/a0a9e2c13c6dd26313bdc9b247030114 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| import sys | |
| import os | |
| import shlex | |
| import pprint | |
| import re | |
| from subprocess import * | |
| def cmd(inp): | |
| command = shlex.split(inp) | |
| com = Popen(command, shell=False, stdout=PIPE, stderr=PIPE) | |
| out = ''.join(com.communicate()) | |
| return out, com.returncode | |
| def tmuxSessions(kill = False): | |
| tm = cmd('tmux list-sessions'); | |
| sess = []; | |
| sess.append("Create New Session"); | |
| s = tm[0].split("\n") | |
| for i, v in enumerate(s): | |
| if not re.search(':',v): | |
| continue; | |
| index = i+1 | |
| sess.append(v) | |
| if not kill and len(sess)==1: | |
| return createNewSession() | |
| for i, v in enumerate(sess): | |
| if kill and i==0: | |
| continue; | |
| out = "{0}) {1}".format(i,v) | |
| print out | |
| ask = "Select a Session #: \n" | |
| if kill: | |
| ask = "Select a Session # To Kill: \n" | |
| inp = raw_input(ask); | |
| return inp,sess; | |
| def getTmuxSessions(): | |
| sess = []; | |
| command = "tmux list-sessions"; | |
| res = cmd(command); | |
| def restoreSession(sess): | |
| s = sess.split(":") | |
| command = "tmux attach -t '{0}'".format(s[0]) | |
| cmd(command) | |
| exit(0) | |
| def createNewSession(): | |
| # ask user for new session name | |
| name = raw_input("Name you new session: \n") | |
| command = "tmux new-session -s '{0}'".format(name) | |
| res = cmd(command) | |
| exit(0) | |
| def killSession(sess): | |
| s = sess.split(":") | |
| command = "tmux kill-session -t '{0}'".format(s[0]); | |
| res = cmd(command); | |
| exit(0); | |
| def askCreate(): | |
| inp = tmuxSessions(); | |
| while inp[0].isdigit() == False or int(inp[0])<0 or int(inp[0])>(len(inp[1])-1): | |
| inp = tmuxSessions() | |
| index = int(inp[0]); | |
| if index ==0: | |
| return createNewSession() | |
| # session = inp[1][int(inp[0])] | |
| return restoreSession(inp[1][index]) | |
| def askKill(): | |
| inp = tmuxSessions(True); | |
| while inp[0].isdigit() == False or int(inp[0])<=0 or int(inp[0])>len(inp[1]): | |
| inp = tmuxSession(True); | |
| sess = inp[1][int(inp[0])] | |
| killSession(sess); | |
| exit(0); | |
| print '###########################' | |
| print '## TMUX SESSIONS ##########' | |
| print '###########################' | |
| try: | |
| if len(sys.argv)>1 and sys.argv[1] == '-k': | |
| askKill() | |
| else: | |
| askCreate() | |
| except KeyboardInterrupt: | |
| print "\n Canceled......" | |
| exit(0) | |
| except NameError: | |
| print "Exiting...." | |
| exit(0); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment