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