Posted to tcl by kostix at Wed Nov 01 00:23:51 GMT 2006view raw

  1. #! /usr/bin/perl
  2. # Minimalistic start-up frontend for SCREEN.
  3. # At first, asks the screen whether there're some lingering screen sessions.
  4. # If there is none, just starts screen, otherwise asks the user
  5. # to select desired session (or opt for creating a new one).
  6. # Given the user choice, arranges to make the user connected to what he/she chose.
  7.  
  8. # v1.0, 06-Aug-2005, kostix. Initial version.
  9.  
  10. # TODO extract PTY and host from the screen's output, show them to the user.
  11. # TODO (?) add options processing.
  12.  
  13. use strict;
  14. use warnings;
  15.  
  16. open SCREENS, 'screen -ls 2>&1 |';
  17.  
  18. my (@screens, $complaint);
  19. push @screens, [ '<make new session>', 'new' ];
  20.  
  21. while(<SCREENS>) {
  22. # $1 -> screen id, $2 -> mode:
  23. if (/^\s+(\d+).*\s+\((\w+)\)$/) {
  24. push @screens, [ $1, lc $2 ];
  25. } else {
  26. $complaint .= $_;
  27. }
  28. }
  29.  
  30. my $ok = close SCREENS;
  31. $ok >>= 8, die "screen -ls returned $ok and said: $complaint" unless $ok != 1;
  32.  
  33. exec 'screen' unless @screens; # just spawn screen making new session.
  34.  
  35. my $no = 0;
  36. foreach my $ref (@screens) {
  37. my $id = $ref->[0]; my $state = $ref->[1];
  38. print "$no: $id -> $state\n";
  39. ++$no;
  40. }
  41.  
  42. require Term::ReadLine;
  43. my $rl = Term::ReadLine->new('Screen Selector');
  44.  
  45. my $resp = undef;
  46. while ('forever') {
  47. $resp = $rl->readline('Pick one or give EOF: ');
  48.  
  49. exit 1 unless defined $resp; # bail out on EOF
  50. last if $resp =~ qr/^\d+$/;
  51. print "Never learned how to type a number?\n";
  52. }
  53.  
  54. exec 'screen' if $resp == 0; # user has opted for new session
  55.  
  56. # Reattach to a running session:
  57. my @spell = ('screen');
  58. push @spell, '-d' if $screens[$resp][1] eq 'attached';
  59. push @spell, '-r', $screens[$resp][0];
  60. exec @spell;