-
-
Save ibejohn818/4808367933faa640217202c0d197d99e 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
Another File to test |
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 php | |
<?php | |
############################################# | |
# List TMUX session and chose an existing one | |
# or create a new one | |
# To Kill Sessions: Call the script with | |
# the -k option and you will be prompted | |
# to kill sessions | |
# ########################################## | |
$kill = false; | |
if($argv[1] == "-k") { | |
$kill = true; | |
} | |
//get the tmux sessions | |
$tmuxls = `tmux ls`; | |
$tmuxArray = explode("\n",$tmuxls); | |
$sessions = []; | |
foreach($tmuxArray as $k=>$v) { | |
if(!empty($v)) { | |
//get the session index | |
$pieces = explode(":",$v); | |
$sessionIndex = $pieces[0]; | |
$sessionName = $pieces[1]; | |
$sessions[] = [ | |
'index'=>$sessionIndex, | |
'name'=>$v | |
]; | |
} | |
} | |
hr(); | |
out("TMUX Sessions (".count($sessions).")",1); | |
hr(); | |
selectSession($sessions,$kill); | |
function selectSession($sessions,$kill) { | |
$indexes = []; | |
if(!$kill) { | |
$indexes[] = 0; | |
out("0) Create a new session"); | |
} | |
foreach($sessions as $k=>$v) { | |
$ind = $k+1; | |
$indexes[] = $ind; | |
out("{$ind}) {$v['name']}"); | |
} | |
$indexesStr = implode("/",$indexes); | |
if($kill) { | |
$msg = "Select a session to kill"; | |
} else { | |
$msg = "Select a session"; | |
} | |
$selection = in("{$msg}: ({$indexesStr})"); | |
if(!$kill && $selection == "0") { | |
return newSession(); | |
} | |
if(!is_numeric($selection)) { | |
return selectSession($sessions,$kill); | |
} | |
$selection -= 1; | |
if(!array_key_exists($selection,$sessions)) { | |
return selectSession($sessions,$kill); | |
} | |
if($kill) { | |
return killSession($sessions[$selection]['index']); | |
} | |
return resumeSession($sessions[$selection]['index']); | |
} | |
function killSession($index) { | |
passthru("tmux kill-session -t \"{$index}\""); | |
exit(0); | |
} | |
function resumeSession($index) { | |
passthru("tmux attach -t \"{$index}\""); | |
exit(0); | |
} | |
function newSession() { | |
hr(); | |
$sessionName = in("Name your new session"); | |
$sn = str_replace('"',"",$sessionName); | |
passthru("tmux new-session -s \"{$sn}\""); | |
exit(0); | |
} | |
############################### | |
### Utility Functions | |
############################## | |
function out($s,$l = 1) { | |
echo $s; | |
for($i=1;$i<=$l;$i++) { | |
echo "\n"; | |
} | |
} | |
function hr() { | |
out("============================="); | |
} | |
function in( $prompt = '' ) { | |
out($prompt.": "); | |
return rtrim( fgets( STDIN ), "\n" ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment