-
-
Save nathyong/c18c91c1f16785e08293 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/python2.7 | |
| from subprocess import check_output, STDOUT, CalledProcessError, call | |
| from sys import argv, stdout, stdin | |
| def run(func): | |
| """Run a function and capture the output.""" | |
| if type(func) != type([]) and type(func) == type(""): | |
| func = func.split(' ') | |
| try: | |
| out = check_output(func, stderr=STDOUT)[:-1] | |
| except CalledProcessError as o: | |
| out = o.output[:-1] | |
| return out | |
| ls = run('tmux ls') | |
| if len(argv) == 1 and 'windows' in ls: | |
| print ls | |
| exit() | |
| names = [i.split(":")[0]for i in ls.split("\n")] | |
| if len(argv) == 1: | |
| argv = ['tt', '0'] | |
| if argv[1] in names: | |
| call(['tmux', 'attach', '-t', argv[1]]) | |
| exit() | |
| response = '' | |
| while response != 'y': | |
| if not 'windows' in ls: | |
| out = 'no sessions, create one called "%s"? (y/n) ' | |
| else: | |
| out = 'session "%s" does not exist, create it? (y/n) ' | |
| stdout.write(out % argv[1]) | |
| try: | |
| response = stdin.readline()[:-1].lower() | |
| except KeyboardInterrupt: | |
| exit() | |
| if response == 'y': | |
| call(['tmux', 'new-session', '-s', argv[1]]) | |
| elif response == 'n': | |
| exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment