]> git.vpit.fr Git - perl/modules/rgit.git/blob - lib/App/Rgit/Policy/Interactive.pm
Remove trailing whitespace
[perl/modules/rgit.git] / lib / App / Rgit / Policy / Interactive.pm
1 package App::Rgit::Policy::Interactive;
2
3 use strict;
4 use warnings;
5
6 use Cwd ();
7
8 use App::Rgit::Utils qw/:codes/;
9
10 use base qw/App::Rgit::Policy/;
11
12 =head1 NAME
13
14 App::Rgit::Policy::Interactive - A policy that asks what to do on error.
15
16 =head1 VERSION
17
18 Version 0.06
19
20 =cut
21
22 our $VERSION = '0.06';
23
24 my ($int_code, $shell);
25
26 sub new {
27  my $class = shift;
28  $class = ref $class || $class;
29
30  eval "require Term::ReadKey"
31       or die "You have to install Term::ReadKey to use the interactive mode.\n";
32
33  unless (defined $int_code) {
34   $int_code = { Term::ReadKey::GetControlChars() }->{INTERRUPT};
35  }
36
37  unless (defined $shell) {
38   for (grep defined, $ENV{SHELL}, '/bin/sh') {
39    if (-x $_) {
40     $shell = $_;
41     last;
42    }
43   }
44  }
45
46  $class->SUPER::new(@_);
47 }
48
49 my %codes = (
50  'a' => [ LAST,        'aborting' ],
51  'i' => [ NEXT,        'ignoring' ],
52  'I' => [ NEXT | SAVE, 'ignoring all' ],
53  'r' => [ REDO,        'retrying' ],
54 );
55
56 sub report {
57  my ($policy, $cmd, $conf, $repo, $status, $signal) = @_;
58
59  return NEXT unless $status;
60
61  while (1) {
62   $conf->warn("[a]bort, [i]gnore, [I]gnore all, [r]etry, open [s]hell ?");
63
64   Term::ReadKey::ReadMode(4);
65   my $key = Term::ReadKey::ReadKey(0);
66   Term::ReadKey::ReadMode(1);
67
68   $conf->warn("\n");
69
70   next unless defined $key;
71
72   if ($key eq $int_code) {
73    $conf->warn("Interrupted, aborting\n");
74    return LAST;
75   } elsif ($key eq 's') {
76    if (defined $shell) {
77     $conf->info('Opening shell in ', $repo->work, "\n");
78     my $cwd = Cwd::cwd;
79     $repo->chdir;
80     system { $shell } $shell;
81     chdir $cwd;
82    } else {
83     $conf->err("Couldn't find any shell\n");
84    }
85   } elsif (exists $codes{$key}) {
86    my $code = $codes{$key};
87    $conf->info('Okay, ', $code->[1], "\n");
88    return $code->[0];
89   }
90  }
91 }
92
93 =head1 SEE ALSO
94
95 L<rgit>.
96
97 =head1 AUTHOR
98
99 Vincent Pit, C<< <perl at profvince.com> >>, L<http://profvince.com>.
100
101 You can contact me by mail or on C<irc.perl.org> (vincent).
102
103 =head1 BUGS
104
105 Please report any bugs or feature requests to C<bug-rgit at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=rgit>.
106 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
107
108 =head1 SUPPORT
109
110 You can find documentation for this module with the perldoc command.
111
112     perldoc App::Rgit::Policy::Interactive
113
114 =head1 COPYRIGHT & LICENSE
115
116 Copyright 2008,2009,2010 Vincent Pit, all rights reserved.
117
118 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
119
120 =cut
121
122 1; # End of App::Rgit::Policy::Interactive