File Coverage

lib/UR/Namespace/Command/Test/Window.pm
Criterion Covered Total %
statement 15 109 13.7
branch 0 18 0.0
condition 0 15 0.0
subroutine 5 21 23.8
pod n/a
total 20 163 12.2


line stmt bran cond sub pod time code
1              
2 1     1   22 use strict;
  1         2  
  1         142  
3 1     1   4 use warnings;
  1         1  
  1         54  
4              
5 1     1   3 use above 'UR';
  1         1  
  1         8  
6              
7             package UR::Namespace::Command::Test::Window;
8             require UR;
9             our $VERSION = "0.46"; # UR $VERSION;
10              
11             UR::Object::Type->define(
12             class_name => __PACKAGE__,
13             is => 'Command',
14             has => {
15             src => {
16             is_optional => 1,
17             is_many => 1,
18             shell_args_position => 1
19             }
20             },
21             doc => 'repl tk window'
22             );
23              
24             sub execute {
25 0     0     my $self = shift;
26 0           require Tk;
27            
28 0           my $src = [$self->src];
29 0 0         if (@$src > 0) {
30 0           for my $code (@$src) {
31 1     1   4 no strict;
  1         1  
  1         23  
32 1     1   4 no warnings;
  1         1  
  1         1057  
33 0           eval $code;
34 0           print $@;
35             }
36             }
37             else {
38 0           UR::Namespace::Command::Test::Window::Tk->activate_with_gtk_support;
39             }
40              
41             }
42              
43             package UR::Namespace::Command::Test::Window::Tk;;
44              
45             our $tmp;
46             our $workspace;
47              
48             sub new {
49 0 0 0 0     if ($^O eq 'MSWin32' || $^O eq 'cygwin') {
50             #$tmp = $ENV{TEMP};
51 0   0       $tmp ||= 'C:/temp';
52             }
53             else {
54 0           $tmp = $ENV{TMPDIR};
55 0   0       $tmp ||= '/tmp';
56             }
57              
58             # Make a window
59 0           my $debug_window = new MainWindow(-title=>"Debug");
60              
61             # The top pane is for inputing Perl
62 0           my $in = $debug_window->Scrolled("Text", -scrollbars=>'os',-exportselection => 1)->pack(-expand => 0, -fill => 'x');
63              
64             # The middle is a frame with an eval button
65 0           my $frame = $debug_window->Frame()->pack(-expand=>0,-fill=>'both',-anchor=>'nw');
66 0     0     my $go = $frame->Button(-text => 'eval()', -command => sub { &exec_debug($debug_window) } )->pack(-expand => 0, -fill => 'none', -anchor=>'nw', -side=>'left');
  0            
67            
68             # The bottom is a pane for output
69 0           my $out = $debug_window->Scrolled("Text", -scrollbars=>'osoe',-wrap => 'none')->pack(-fill => 'both', -expand => 1);
70              
71             # See if there is a workspace file for an app with this name
72 0           my $user = $ENV{USER};
73 0   0       $user ||= 'anonymous';
74            
75 0           $0 =~ /([^\/\s]+)$/;
76 0           my $core_name = $1;
77            
78 0   0       $workspace ||= "$user\@$core_name";
79 0           print STDOUT "Workspace is $workspace\n";
80            
81 0 0         if (open(LAST_WORKSPACE,"${tmp}/$workspace"))
82             {
83 0           while ()
84             {
85 0           $in->insert("end",$_);
86             }
87 0           close LAST_WORKSPACE;
88             }
89              
90 0           $debug_window->{in} = $in;
91 0           $debug_window->{out} = $out;
92              
93 0           return $debug_window;
94             }
95              
96             sub new_gtk {
97 0     0     require Gtk;
98 0           my $debug_window = DebugWindow::new();
99 0           my $frame = $debug_window->Frame()->pack(-expand=>0,-fill=>'both',-anchor=>'nw');
100 0           my $continuous_refresh = 0;
101              
102             $frame->Button(-text => 'One Gtk', -command => sub
103             {
104 0     0     Gtk->main_iteration
105 0           })->pack(-expand => 0, -fill => 'none', -anchor=>'nw', -side=>'left');
106            
107            
108             $frame->Button(-text => 'All Gtk', -command => sub
109             {
110 0     0     while (Gtk->events_pending)
111             {
112 0           Gtk->main_iteration;
113             }
114 0           })->pack(-expand => 0, -fill => 'none', -anchor=>'nw', -side=>'left');
115            
116 0           my $handleGtk;
117             $handleGtk = sub
118             {
119 0 0 0 0     return unless (Exists($debug_window) and $continuous_refresh);
120 0           Gtk->main_iteration;
121            
122 0 0         my $delay = (Gtk->events_pending ? 5 : 500);
123 0           Tk->after($delay, $handleGtk);
124 0           };
125            
126             $frame->Button
127             (
128             -text => 'Gtk Cont',
129             -command => sub
130             {
131 0     0     $continuous_refresh = (not $continuous_refresh);
132 0           &$handleGtk;
133             }
134 0           )->pack(-expand => 0, -fill => 'none', -anchor=>'nw', -side=>'left');
135             }
136              
137             sub activate {
138 0     0     my $window=&new;
139 0           $window->waitWindow();
140 0           Tk->MainLoop;
141              
142             }
143              
144             sub activate_with_gtk_support {
145 0     0     &new_gtk;
146 0           Tk->MainLoop;
147             }
148              
149       0     sub hook_button {
150            
151             }
152              
153             sub hook_gtk_button {
154 0     0     my $gtk_button = shift;
155             $gtk_button->signal_connect('button_press_event', sub
156             {
157 0     0     my ($self,$event) = @_;
158            
159             # Test the Gtk widget event to see which button was clicked.
160            
161 0 0         if ($event->{button} == 3)
162             {
163             # Instantiate the debug window with the special Gtk buttons on it.
164 0           my $debug_window = DebugWindow::new_gtk();
165 0           Tk->MainLoop();
166             }
167 0           return(1);
168 0           });
169             }
170              
171             sub show_new {
172             # Legacy function
173 0     0     &new(@_);
174             }
175              
176             sub exec_debug {
177 0     0     my $self = $_[0];
178 0           my $in = $self->{in};
179 0           my $out = $self->{out};
180              
181             # Clear the results window.
182 0           $out->delete("1.0","end");
183              
184             # Get all of the text in the workspace window.
185 0           my $perl = $in->get("1.0","end");
186            
187             # If there is a valid selection override the above with just the selected text.
188 0           eval { $perl = $in->get("sel.first","sel.last"); };
  0            
189            
190             # Open a temporary output file to catch the STDOUT
191 0           my $filename = "${tmp}/${workspace}_output";
192            
193 0 0         open (DEBUG_FH,">$filename") or die "Failed to open temp file '$filename': $!\n";
194            
195             # Redirect STDOUT temporarily
196 0           *ORIG_STDOUT = *STDOUT;
197 0           *STDOUT = *DEBUG_FH;
198            
199             # Run the perl.
200 0           eval ("package main;\n" . $perl);
201              
202             # Print any errors
203 0           print $@;
204              
205             # Restore STDOUT
206 0           *STDOUT = *ORIG_STDOUT;
207 0           close DEBUG_FH;
208            
209             # Get the script output
210 0           my $fh = IO::File->new("${tmp}/${workspace}_output");
211 0           my $text;
212 0 0         if ($fh)
213             {
214 0           my @text = $fh->getlines;
215 0           $fh->close;
216 0           $text = join("",@text);
217             }
218            
219             # Print to the console as well as the result widget.
220 0           print $text;
221            
222             # For some reason embedded \n causese every other row to disappear.
223             # Split on line boundaries and feed the output to the widget in pieces.
224 0           foreach my $row (split /$/, $text) {
225 0           $out->insert('end',$row);
226             }
227            
228             # Save the whole workspace like we do when the app closes
229 0           save_workspace($self);
230             }
231              
232             sub save_workspace {
233 0     0     my $self = $_[0];
234 0           my $in = $self->{in};
235            
236             # Save the workspace
237 0 0         if (open (SCRIPT_FH, ">${tmp}/$workspace"))
238             {
239 0           print SCRIPT_FH $in->get("1.0","end");
240 0           close SCRIPT_FH;
241 0           print "Saved to ${tmp}/$workspace\n";
242             }
243             else
244             {
245 0           print STDOUT "Failed to save the current workspace (${tmp}/$workspace)!";
246             }
247             }
248              
249             1;
250