Screen dynamic submenu

Hi all!

I’ve already announced this at IRC, and now I’m posting here :smiley:

I’ve written little perl script that makes dynamic menu with attached and/or deattached screen sessions. So, here we go. First, code you need to enter into your configs:

[code]AddToMenu MyMenu

  • Popup SubMenuScreen

DestroyMenu recreate SubMenuScreen
AddToMenu SubMenuScreen

  • DynamicPopupAction Function BuildScreenMenu

DestroyFunc BuildScreenMenu
AddToFunc BuildScreenMenu

  • I DestroyMenu recreate SubMenuScreen
  • I AddToMenu SubMenuScreen
  • I Piperead ‘echo “$HOME/.fvwm/bin/fvwm-screen-menu.pl”’ [/code]

And now the script:

[code]#!/usr/bin/perl -w
#################################

fvwm-screen-menu.pl

by: jsosic

#################################

use Shell qw(screen grep hostname);

Get list of detached/attached screen sessions and hostname

$s_detached_sessions = screen(’-list | grep Detached’);
$s_attached_sessions = screen(’-list | grep Attached’);
$hostname = hostname(’’);

regexps rule :wink:

$hostname=~s/\s//g;
$s_detached_sessions=~s/()|(|Detached|$hostname|^\s+)//g;
$s_attached_sessions=~s/()|(|Attached|$hostname|^\s+)//g;
$s_detached_sessions=~s/…\n//g;
$s_attached_sessions=~s/…\n//g;

Finally, here’s what we came for.

@ls_detached_sessions = split /\s+/, $s_detached_sessions;
foreach my $session (@ls_detached_sessions){
print " + “$session” Exec exec urxvtc -n screen -e screen -r $session.$hostname \n";
}

put separator if there are detached screens…

if ($s_detached_sessions ne “”){
print " + “” NoP\n";
}

Add some general stuff…

print " + “New Session” Exec exec urxvtc -n screen -e screen \n";
print " + “Reattach first” Exec exec urxvtc -n screen -e screen -d -RR \n";

put separator if there are attached screens…

if ($s_attached_sessions ne “”){
print " + “” NoP\n";
}

Little last minute addition: Attached sessions

@ls_attached_sessions = split /\s+/, $s_attached_sessions;
foreach my $session (@ls_attached_sessions){
print " + “$session” Exec exec urxvtc -n screen -e screen -d $session.$hostname \n";
}
[/code]

Also, don’t forget to chmod +x the script, and pinpoint it correctly in your config.

Happy screen-ing!

And a screenshot:

My dearest sir, you are simply awesome!
I have been trying for some time to do this but failed miserably all the times, thank you very much.

I’m glad that I’ve been able to help :smiley:

I know many of fvwm zealots will appretiate this one, and I wondered how nobody tought of this one earlier, and so I just code it.

Many of Fvwm zealots bow to sacred three: Fvwm, zsh, screen, and I’m one of them :smiley: If I had to drop one of the three, I wouldn’t know which one is less important to me :slight_smile:

They had, of course; just that they hadn’t made it explicitly known.

– Thomas Adam

Too bad I can’t demand patent rights on some ideas I never publicly announced… I would have been millionaire.

Here’s upgraded script - it now fixex bug with named screen sessions, thanx to Llukaz for pointing it out:

[code]#!/usr/bin/perl -w
#################################

fvwm-screen-menu.pl

by: jsosic

#################################

use Shell qw(screen grep hostname);

Get list of detached/attached screen sessions and hostname

$s_detached_sessions = screen(’-list | grep Detached’);
$s_attached_sessions = screen(’-list | grep Attached’);
$hostname = hostname(’’);

regexps rule :wink:

$hostname=~s/\s//g;
$s_detached_sessions=~s/()|(|Detached|$hostname|^\s+)//g;
$s_attached_sessions=~s/()|(|Attached|$hostname|^\s+)//g;
$s_detached_sessions=~s/^[0-9]{2,6}.//g;
$s_attached_sessions=~s/^[0-9]{2,6}.//g;
$s_detached_sessions=~s/…\n//g;
$s_attached_sessions=~s/…\n//g;

Finally, here’s what we came for.

@ls_detached_sessions = split /\s+/, $s_detached_sessions;
foreach my $session (@ls_detached_sessions){
print " + “$session” Exec exec urxvtc -n screen -e screen -r $session \n";
}

put separator if there are detached screens…

if ($s_detached_sessions ne “”){
print " + “” NoP\n";
}

Add some general stuff…

print " + “New Session” Exec exec urxvtc -n screen -e screen \n";
print " + “Reattach first” Exec exec urxvtc -n screen -e screen -d -RR \n";

put separator if there are attached screens…

if ($s_attached_sessions ne “”){
print " + “” NoP\n";
}

Little last minute addition: Attached sessions

@ls_attached_sessions = split /\s+/, $s_attached_sessions;
foreach my $session (@ls_attached_sessions){
print " + “$session” Exec exec urxvtc -n screen -e screen -d $session \n";
}
[/code]

Sorry to resurrect an old thread, but I came across this script and decided to use it myself.

I’ve made changes to add support for Tmux as well. I’ve separated out the Screen and Tmux stuff so you can just delete the entire screen or tmux section if you don’t want to support either of those two options. I also made some changes to make it work, as the version from 2006 didn’t run the shell commands.

I’ve reworked the regex filtering of the names as well, so this can handle session names with spaces in them.

#!/usr/bin/perl -w
#################################

#fvwm-screen-menu.pl
#Original by: jsosic
#
#Modified by Dennis Katsonis to add Tmux support.
#################################

use Shell qw(screen tmux grep hostname);

$hostname = hostname("-s");
#regexps rule :wink:
$hostname=~s/\s//g;

#Get list of detached/attached screen sessions and hostname


# SCREEN SECTION - Delete this section if you don't want screen support
$s_detached_sessions = `screen -list | grep Detached`;
$s_attached_sessions = `screen -list | grep Attached`;

$s_detached_sessions=~s/\(.*\)|(|Detached|\.$hostname|^\s+)//g;
$s_attached_sessions=~s/\(.*\)|(|Attached|\.$hostname|^\s+)//g;

print " + \"SCREEN\" Title\n";

#Add some general stuff…
print " + \"New Session\" Exec exec urxvtc -n screen -e screen \n";
print " + \"Reattach first\" Exec exec urxvtc -n screen -e screen -d -RR \n";


#put separator if there are detached screens…
if ($s_detached_sessions ne ""){
	print " + \"Screen Detached\" Title\n";
}


#Finally, here’s what we came for.
@ls_detached_sessions = split /\n+/, $s_detached_sessions;
foreach my $session (@ls_detached_sessions){
	$session=~s/^\s+|\s+$//g;
	print " + \"$session\" Exec exec urxvtc -n screen -e screen -r \"$session\"\n";
}


#put separator if there are attached screens…
if ($s_attached_sessions ne ""){
	print " + \"Screen Attached\" Title\n";
}

#Little last minute addition: Attached sessions
@ls_attached_sessions = split /\n+/, $s_attached_sessions;
foreach my $session (@ls_attached_sessions){
	$session=~s/^\s+|\s+$//g;
	print " + \"$session\" Exec exec urxvtc -n screen -e screen -d -t \"$session\"\n";
}


# TMUX SECTION - Delete this section if you don't want tmux support


$t_detached_sessions = `tmux list-sessions -F '#{session_name} #{?session_attached,Attached,Detached}' | grep Detached`;
$t_attached_sessions = `tmux list-sessions -F '#{session_name} #{?session_attached,Attached,Detached}' | grep Attached`;
$t_detached_sessions=~s/()|(|Detached|^\s+)//g;
$t_attached_sessions=~s/()|(|Attached|^\s+)//g;

print " + \"TMUX\" Title\n";

print " + \"New Session\" Exec exec urxvtc -n tmux -e tmux \n";
print " + \"Reattach first\" Exec exec urxvtc -n tmux -e tmux attach-session \n";

#put separator if there are detached screens…
if ($t_detached_sessions ne ""){
	print " + \"Tmux Detached\" Title\n";
}


@ls_t_detached_sessions = split /\n+/, $t_detached_sessions;
foreach my $session (@ls_t_detached_sessions){
	$session=~s/^\s+|\s+$//g;
	print " + \"$session\" Exec exec urxvtc -n tmux -e tmux attach-session -t \"$session\"\n";
}


#put separator if there are attached screens…
if ($t_attached_sessions ne ""){
	print " + \"Tmux Attached\" Title\n";
}

#Little last minute addition: Attached sessions
@ls_t_attached_sessions = split /\n+/, $t_attached_sessions;
foreach my $session (@ls_t_attached_sessions){
	$session=~s/^\s+|\s+$//g;
	print " + \"$session\" Exec exec urxvtc -n tmux -e tmux attach-session -t \"$session\"\n";
}