Created
October 19, 2019 13:20
-
-
Save Ujang360/ff2ac5f9836e03c6b161b207eeee09ad to your computer and use it in GitHub Desktop.
Start TMUX Prompt
This file contains 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/python3 | |
import libtmux | |
import subprocess | |
import os | |
def create_new_session(tmux_server: libtmux.Server): | |
user_answer = input("Input session name: ") | |
tmux_server.new_session(session_name=user_answer) | |
tmux_server.attach_session(user_answer) | |
try: | |
tmux_server = libtmux.Server() | |
sessions = tmux_server.list_sessions() | |
session_count = len(sessions) | |
print("Available TMUX Session:") | |
if len(sessions) == 0: | |
user_answer = input("No session available, create new session? (Y/N): ").lower() | |
if user_answer == "y": | |
create_new_session(tmux_server) | |
exit(0) | |
else: | |
input_success = False | |
last_session_id = 0 | |
session_names = dict() | |
print("[0] -> create-new-session") | |
for session in sessions: | |
last_session_id += 1 | |
session_name = dict(session._info)["session_name"] | |
session_names[last_session_id] = session_name | |
print(f"[{last_session_id}] -> {session_name}") | |
while not input_success: | |
user_answer = input(f"Choose from 0 to {last_session_id}: ") | |
try: | |
selected_session_id = int(user_answer) | |
if selected_session_id > last_session_id: | |
input_success = False | |
continue | |
elif selected_session_id == 0: | |
create_new_session(tmux_server) | |
else: | |
tmux_server.attach_session(session_names[selected_session_id]) | |
input_success = True | |
exit(0) | |
except ValueError: | |
input_success = False | |
continue | |
except Exception as e: | |
print(e) | |
exit(1) | |
except KeyboardInterrupt: | |
print("\nCancelled...") | |
home_dir = os.getenv("HOME", "/") | |
bash = subprocess.Popen("bash", cwd=home_dir) | |
bash.wait() | |
exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment