Codit/Plugins/Diff.pm untested

filter: Statementstmt 0.0% 0 / 119 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 158 CC 7 SCAR 16.7
CC
7
Cov
0%
new
12.0
ReConfigure
12.0
Diff
6.0
CRAP 5.3
1 package App::Codit::Plugins::Diff;
2  
3 =head1 NAME
4  
5 App::Codit::Plugins::Diff - plugin for App::Codit
6  
7 =cut
8  
9 use strict;
10 use warnings;
11 use vars qw( $VERSION );
12 $VERSION = '0.22';
13  
14 use base qw( Tk::AppWindow::BaseClasses::Plugin );
15  
16 require Tk::CodeText;
17 use Text::Diff;
18 use Tk;
19  
20 =head1 DESCRIPTION
21  
22 Generate a diff between your document and a file.
23  
24 =head1 DETAILS
25  
26 This plugin shows you the differences between your document and a file in diff format.
27 You can select a diff from your document to the file or form the file to your document.
28 You can also select several display styles.
29 The diff can be exported to a diff file.
30  
31 =cut
32  
33 sub new {
34 my $class = shift;
35 my $self = $class->SUPER::new(@_);
36 return undef unless defined $self;
37  
38 my $page = $self->ToolRightPageAdd('Diff', 'diff', undef, 'Perform a difff on your document against a file');
39  
40  
41 my @padding = (-padx => 2, -pady => 2);
42  
43 my $sm = $page->Frame(
44 -relief => 'groove',
45 -borderwidth => 2,
46 )->pack(-padx => 2, -fill => 'x');
47  
48 my $ff = $sm->Frame->pack(@padding, -fill => 'x');
49 my $fe = $ff->Entry->pack(-side => 'left', -expand => 1, -fill => 'x');
50 $self->{FILEENTRY} = $fe;
51 $ff->Button(
52 -relief => 'flat',
53 -command => sub {
54 my ($file) = $self->pickFileOpen;
55 if (defined $file) {
56 $fe->delete(0, 'end');
57 $fe->insert(0, $file);
58 }
59 },
60 -image => $self->getArt('document-open', 16),
61 )->pack(-side => 'right');
62  
63  
64 #mode selection
65 my $mf = $sm->Frame->pack(-fill =>'x');
66 $mf->Label(
67 -anchor =>'e',
68 -width => 7,
69 -text => 'Mode:',
70 )->pack(-side => 'left');
71 my $mode = 'File to document';
72 $self->{MODE} = \$mode;
73 my $mb = $mf->Menubutton(
74 -anchor => 'w',
75 -textvariable => \$mode,
76 )->pack(@padding, -side => 'left', -expand => 1, -fill => 'x');
77 my @menu = ();
78 for ('File to document', 'Document to file') {
79 my $operation = $_;
80 push @menu, [command => $operation,
81 -command => sub { $mode = $operation },
82 ];
83 }
84 $mb->configure(-menu => $mb->Menu(
85 -menuitems => \@menu,
86 ));
87  
88 #style selection
89 my $sf = $sm->Frame->pack(-fill =>'x');
90 $sf->Label(
91 -anchor =>'e',
92 -width => 7,
93 -text => 'Style:',
94 )->pack(-side => 'left');
95 my $style = 'Unified';
96 $self->{STYLE} = \$style;
97 my $sb = $sf->Menubutton(
98 -anchor => 'w',
99 -textvariable => \$style,
100 )->pack(@padding, -side => 'left', -expand => 1, -fill => 'x');
101 my @smenu = ();
102 for ('Unified', 'Context', 'OldStyle') {
103 my $operation = $_;
104 push @smenu, [command => $operation,
105 -command => sub { $style = $operation },
106 ];
107 }
108 $sb->configure(-menu => $sb->Menu(
109 -menuitems => \@smenu,
110 ));
111  
112  
113  
114 my $sa = $page->Frame(
115 -relief => 'groove',
116 -borderwidth => 2,
117 )->pack(@padding, -expand => 1, -fill => 'both');
118 my $bf1 = $sa->Frame->pack(-expand => 1, -fill =>'x');
119 $bf1->Button(
120 -text => 'Diff',
121 -command => ['Diff', $self],
122 )->pack(@padding, -expand => 1, -side => 'left', -fill => 'x');
123 $bf1->Button(
124 -text => 'Clear',
125 -command => ['Clear', $self],
126 )->pack(@padding, -expand => 1, -side => 'left', -fill => 'x');
127 my $bf2 = $sa->Frame->pack(-expand => 1, -fill =>'x');
128 $bf2->Button(
129 -text => 'Patch',
130 -command => ['Patch', $self],
131 )->pack(@padding, -expand => 1, -side => 'left', -fill => 'x');
132 $bf2->Button(
133 -text => 'Export',
134 -command => ['Export', $self],
135 )->pack(@padding, -expand => 1, -side => 'left', -fill => 'x');
136  
137 my $txt = $sa->CodeText(
138 -readonly => 1,
139 -showfolds => 0,
140 -shownumbers => 0,
141 -showstatus => 0,
142 -syntax => 'Diff',
143 -width => 40,
144 -wrap => 'none',
145 )->pack(@padding, -fill => 'both', -expand => 1);
146 $self->{TXT} = $txt;
147  
148 $self->ReConfigure;
149 return $self;
150 }
151  
152 sub Clear {
153 my $self = shift;
154 my $txt = $self->{TXT};
155 $txt->delete('0.0', 'end');
156 }
157  
158 sub Diff {
159 my $self = shift;
160 my $txt = $self->{TXT};
161 $txt->delete('0.0', 'end');
162 my $widg = $self->mdi->docWidget;
163 my $source = $widg->get('0.0', 'end - 1 char');
164 my $file = $self->GetFile;
165 my $mode = $self->GetMode;
166 my $style = $self->GetStyle;
167 my $diff = '';
168 if ($mode eq 'File to document') {
169 $diff = diff($file, \$source, { STYLE => $style });
170 } else {
171 $diff = diff(\$source, $file, { STYLE => $style });
172 }
173 $txt->insert('end', $diff);
174 $self->after(10, sub { $txt->highlightPurge });
175 }
176  
177 sub Export {
178 my $self = shift;
179 my ($file) = $self->pickFileSave;
180 my $txt = $self->{TXT};
181 $txt->saveExport($file) if defined $file;
182 }
183  
184 sub GetFile {
185 my $self = shift;
186 my $m = $self->{FILEENTRY};
187 return $m->get;
188 }
189  
190 sub GetMode {
191 my $self = shift;
192 my $m = $self->{MODE};
193 return $$m;
194 }
195  
196 sub GetStyle {
197 my $self = shift;
198 my $m = $self->{STYLE};
199 return $$m;
200 }
201  
202 sub Patch {
203 my $self = shift;
204 my $patch = $self->{TXT}->get('0.0', 'end - 1c');
205 my $widg = $self->mdi->docWidget;
206 $widg->patchdiff(\$patch);
207 }
208  
209 sub ReConfigure {
210 my $self = shift;
211 my $txt = $self->{TXT};
212 my $fam = $self->configGet('-contentfontfamily');
213 $fam = 'Courier' unless defined $fam;
214 my $siz = $self->configGet('-contentfontsize');
215 $siz = 10 unless defined $siz;
216 $txt->configure(-font => "{$fam} $siz");
217 $txt->configure(-themefile => $self->GetThemeFile);
218 }
219  
220 sub Unload {
221 my $self = shift;
222 $self->ToolRightPageRemove('Diff');
223 return $self->SUPER::Unload
224 }
225  
226 =head1 LICENSE
227  
228 Same as Perl.
229  
230 =head1 AUTHOR
231  
232 Hans Jeuken (hanje at cpan dot org)
233  
234 =head1 TODO
235  
236 =over 4
237  
238 =back
239  
240 =head1 BUGS AND CAVEATS
241  
242 If you find any bugs, please report them here L<https://github.com/haje61/App-Codit/issues>.
243  
244 =head1 SEE ALSO
245  
246 =over 4
247  
248 =item L<Text::Diff>
249  
250 =back
251  
252 =cut
253  
254  
255 1;
256  
257  
258  
259  
260  
261  
262  
263  
264