#!/usr/bin/perl
use strict;
use warnings;

sub sess_names {
    map { m{^ \s* \d+ \. ([^.]+?) (?:\.\w+)? \s+}smx } @_;
}

# Get list of detached/attached screen sessions and hostname
my @screen_out=qx(screen -list);

my @detached_sessions=sess_names grep {m{\(Detached\)}} @screen_out;
my @attached_sessions=sess_names grep {m{\(Attached\)}} @screen_out;

print <<'EOL' if @detached_sessions;
  + "Reattach first" A screen -d -RR
EOL

print <<'EOL';
  + "New Session" A screen
EOL

print <<'EOL' if @detached_sessions;
  + "" NoP
EOL

print <<"EOL" for @detached_sessions;
  + "$_ (a)" A screen -r "$_"
EOL

print <<'EOL' if @attached_sessions;
  + "" NoP
EOL

print <<"EOL" for @attached_sessions;
  + "$_ (d)" A screen -d "$_"
EOL
