[Code] YARC – yet another ruby console

[Code] YARC – yet another ruby console

Postby Aerilius » Mon Jun 18, 2012 11:25 pm

I just couldn't stand SketchUp's Ruby Console anymore because its slowness was hindering me a lot. So this is my humble try at an alternative. It's basically a rebuilt of the original and I want to keep the interface as simple as possible.

Ruby Console.gif


  • multi-line input (shift+enter)
  • clear function
  • doesn't slow down
    (when I paste big rubies, it's a significant difference to the native console)
  • remembers command history from one session to another
  • code-highlighting
  • save/insert code snippets

Version: 0.5

Date: 18.06.2012

What do you think?
0
Last edited by Aerilius on Tue Jun 19, 2012 7:53 pm, edited 4 times in total.

Aerilius 
PluginStore Author
PluginStore Author
 

Re: [Code] YARC – yet another ruby console

Postby Dan Rathbun » Tue Jun 19, 2012 12:35 am

    Here's ver 0.6.1 with all the mods below added in for the "ae_Console/Console.rb" and "ae_Console/Console.htm" files.

    Updated again, to ver 0.6.2, fixed the location and sizing of the WebDialog.

    Updated again to ver 0.6.3 (It REALLY now remembers where it the user puts it and it's size. We let Sketchup do the work here.)
    !! Closing the dialog will clear it contents, and wipe out command snippets !!

    ae_Console_0_6_3.rbz

    ae_Console_0_6_3.zip





    Add class variables to hold the various singleton instance objects:
    Code: Select all
    @@console_dlg = nil
    @@command  = nil
    @@topmenu  = nil
    @@menuitem = nil
    (see bottom of post...)

    The open instance method should be named create and return a reference to the newly created UI::WebDialog instance:
    Code: Select all
        (OSX)? @console_dlg.show_modal : @console_dlg.show
        return @console_dlg
      end


    Then the instance open method should be:
    Code: Select all
      def open
        dlg = @console_dlg
        if OSX
          dlg.visible? ? dlg.bring_to_front() : dlg.show_modal()
        else # WIN
          dlg.visible? ? dlg.bring_to_front() : dlg.show()
        end
      end


    And the class method AE::Console.open()
    Code: Select all
      def self.open(instance)
        if @@console_dlg.nil? # nil if not yet instanced.
          @@console_dlg = instance.method(:create).call # also opens it.
        else
          # The WebDialog instance exists, just open it.
          instance.open()
        end
      end


    Lastly, I always suggest putting the "Run Once" block inside your namespace.
    (at the bottom of the file...) and the file_loaded() inside that block to prevent multiple entries in the $loaded_files array.
    (Actually, file_loaded() will not insert duplicates, but .. why call a method if you don't have to?)
    Code: Select all
    unless file_loaded?(File.basename(__FILE__))
      $old_stdout = $stdout if !$old_stdout # in case you want to turn off traces
      $stdout = AE::Console.new
      $stderr = $stdout # trap error displays as well
      @@command = UI::Command.new("Ruby Console (WebDialog)"){
        self.open($stdout)
      }
      # Menu
      @@topmenu = UI.menu("Window")
      @@menuitem = @@topmenu.add_item(@@command)

      file_loaded(File.basename(__FILE__))
    end

    end # class Console

    end # module AE


    Then you can (your choice,) provide class getter methods for singleton objects, that make sense. Ie, references that some other organizer script, might need a handle to:
    Code: Select all
      # Provide a handle to the command, in case
      # some other script wants to add it to a toolbar.
      #
      def self.command
        @@command
      end
    0
    Last edited by Dan Rathbun on Tue Jun 19, 2012 11:54 pm, edited 7 times in total.
      I'm not here much anymore. But a PM will fire email notifications.
      User avatar
      Dan Rathbun 
      PluginStore Author
      PluginStore Author
       

      Re: [Code] YARC – yet another ruby console

      Postby Dan Rathbun » Tue Jun 19, 2012 12:39 am

      And here's another edition of the open instance method:

      Code: Select all
        def open
          dlg = @console_dlg
          if dlg.visible?
            dlg.bring_to_front
          else
            OSX ? dlg.show_modal() : dlg.show()
          end
        end
      0
        I'm not here much anymore. But a PM will fire email notifications.
        User avatar
        Dan Rathbun 
        PluginStore Author
        PluginStore Author
         

        Re: [Code] YARC – yet another ruby console

        Postby Dan Rathbun » Tue Jun 19, 2012 1:09 am

        And another booboo:

        Line 24 should be:
        module AE; end

        and then remove the extraneous line:
        end # module AE
        at the bottom of the file.

        Otherwise you are trying to create these namespaces:
        AE::AE::Console
        0
          I'm not here much anymore. But a PM will fire email notifications.
          User avatar
          Dan Rathbun 
          PluginStore Author
          PluginStore Author
           

          Re: [Code] YARC – yet another ruby console

          Postby Dan Rathbun » Tue Jun 19, 2012 2:22 am

          OK:

          Thoughts:

          It seems to work OK for me on XP.

          It works independent of the native Sketchup Ruby Console (and let us keep it that way. I still need it running with my personal modified toolbar.) Neither one one affects the other, each has it's own input and output.

          Issues:

          The left-side of the "code highlight" button is clipped by about 1 pixel.

          The default dialog background color is not working for me, inside the "init" callback.
          I cut & pasted it outside, just before the callback definition, and now it works.

          The text size is quite small, I prefer larger.

          I believe that the block form of the methods show, show_modal and set_on_close, are all still bugged on Safari Mac. The blocks never run.
          For set_on_close, you will need to attach a onunload="window.location=skp:do_on_close@" to the body or html element.
          And change:
          Code: Select all
                @console_dlg.set_on_close{
                  save_defaults
                  @@open = false
                }
          to:
          Code: Select all
                @console_dlg.add_action_callback("do_on_close") { |dlg, param|
                  save_defaults
                  @@open = false
                }


          Nit-Pickin': :P

          And, in all the block parameter lists, you are using the instance reference @console_dlg, which is weird. Those parameter (var) names are local to each block, and passed in by Sketchup's skp: protocol handler.
          The instance reference @console_dlg, is not local to each block, it's shared by the instance context.
          I am concerned that the callback will be actually making re-assignments each time a callback is called. Basically when the block gets called, the first parameter makes the assignment @console_dlg = @console_dlg.
          Maybe it does not matter, as long as things go well, and the reference never changes.
          The reason for the parameter, is so you can use a local nickname reference,.. d if you wanted ( I usually use dlg, myself.)

          :idea:
          0
            I'm not here much anymore. But a PM will fire email notifications.
            User avatar
            Dan Rathbun 
            PluginStore Author
            PluginStore Author
             

            Re: [Code] YARC – yet another ruby console

            Postby Dan Rathbun » Tue Jun 19, 2012 3:22 am

              Still an issue: I do not see the line numbers, when I turn that feature on.
              0
                I'm not here much anymore. But a PM will fire email notifications.
                User avatar
                Dan Rathbun 
                PluginStore Author
                PluginStore Author
                 

                Re: [Code] YARC – yet another ruby console

                Postby Aerilius » Tue Jun 19, 2012 8:21 am

                Dan Rathbun wrote:And, in all the block parameter lists, you are using the instance reference @console_dlg, which is weird.

                Thanks! I haven't yet thought about why it references the dialog but shouldn't be the same instance variable. And thanks for all the other improvements!

                Dan Rathbun wrote:The default dialog background color is not working for me, inside the "init" callback.
                I had done that with javascript (and thus in the callback) but then I found the API method that only works before the dialog is displayed.

                Dan Rathbun wrote:The left-side of the "code highlight" button is clipped by about 1 pixel.
                It didn't occur to me, I will test this later with Windows and with classic/themed.

                Dan Rathbun wrote:Still an issue: I do not see the line numbers, when I turn that feature on.

                The numbering starts again for each item that you write on the console so I thought it makes only sense for multi-line items (otherwise there is always a 1). I could enable it for single-line items if it causes less confusion.

                I was thinking about an autocomplete function. Instead of maintaining a dictionary of keywords, I could do this:
                If the user types a module name + something into the console, I send the module name (or nothing) to Ruby, get an array of methods and filter the best matching method name.
                0
                Last edited by Aerilius on Tue Jun 19, 2012 9:43 am, edited 1 time in total.

                Aerilius 
                PluginStore Author
                PluginStore Author
                 

                Re: [Code] YARC – yet another ruby console

                Postby Anton_S » Tue Jun 19, 2012 9:02 am

                :) :P Wow, wow, wow, yahooooooooooooooooo1!!!!!! :berserk: :sketchstatic: :family: :enlight: :D

                Jst awesome, that ruby console could save a lot of time :ecstatic:
                0

                Anton_S 
                PluginStore Author
                PluginStore Author
                 

                Re: [Code] YARC – yet another ruby console

                Postby thomthom » Tue Jun 19, 2012 9:23 am

                Screenshot for the OP please? :enlight:
                0
                Thomas Thomassen — SketchUp Monkey & Coding addict
                List of my plugins and link to the CookieWare fund
                User avatar
                thomthom 
                PluginStore Author
                PluginStore Author
                 

                Re: [Code] YARC – yet another ruby console

                Postby Dan Rathbun » Tue Jun 19, 2012 5:57 pm

                Dan Rathbun wrote:Updated again, to ver 0.6.2, fixed the location and sizing of the WebDialog. (It now remembers where it the user puts it and it's size.)


                Actually it seems.. there are still a few issues with positioning:

                The width is remembered OK, both during a session, and on restart.

                But there is still a problem with new height being remembered, both during the session and on restart. (It's like the height is hard-coded.)

                A new position, is not remembered during the session, but only on the next restart.

                (I do know how to solve this on Windows using WinAPI calls, but was hoping to keep this all cross-platform.)

                Maybe ThomThom could have a look see?

                ADD: Usually for WebDialogs, I just let Sketchup handle remembering the position and size. I am not sure why You (Andreas,) why you were sending the height and width, and calculating bordersize and titlebar size in Ruby, and saving them in instance variables. If you keep all this on the Javascript side, and we can do away with that, then the built-in save and restore in the WebDialog class can solve this issue.
                0
                  I'm not here much anymore. But a PM will fire email notifications.
                  User avatar
                  Dan Rathbun 
                  PluginStore Author
                  PluginStore Author
                   

                  Re: [Code] YARC – yet another ruby console

                  Postby Aerilius » Tue Jun 19, 2012 8:05 pm

                  I added a screenshot.

                  Dan Rathbun wrote:I am not sure why you were sending the height and width, and calculating bordersize and titlebar size in Ruby, and saving them in instance variables.

                  That was some code that I used for determining the webdialog size from the content (so that I don't have to hardcode it and it can't conflict with different rendering/fonts/zoom settings). It makes sense for static dialogs that a user shouldn't be able to resize, but I see it doesn't make sense here (other than makeing things complicated). I'll see how it works without.
                  0

                  Aerilius 
                  PluginStore Author
                  PluginStore Author
                   

                  Re: [Code] YARC – yet another ruby console

                  Postby thomthom » Tue Jun 19, 2012 8:58 pm

                  Dan Rathbun wrote:But there is still a problem with new height being remembered, both during the session and on restart. (It's like the height is hard-coded.)

                  A new position, is not remembered during the session, but only on the next restart.


                  Yes, it's an annoying WebDialog thing. If you close and reopen the webdialog within a session it doesn't remember the new position or size until the next session. :(
                  0
                  Thomas Thomassen — SketchUp Monkey & Coding addict
                  List of my plugins and link to the CookieWare fund
                  User avatar
                  thomthom 
                  PluginStore Author
                  PluginStore Author
                   

                  Re: [Code] YARC – yet another ruby console

                  Postby Dan Rathbun » Tue Jun 19, 2012 8:59 pm

                  Dan Rathbun wrote:
                  Dan Rathbun wrote:Updated again, to ver 0.6.2, fixed the location and sizing of the WebDialog. (It now remembers where it the user puts it and it's size.)


                  Actually it seems.. there are still a few issues with positioning:

                  ...


                  Well, because of the state of the API Dictionary.. I can no longer remember what is bugged and what is not!

                  thomthom in this post wrote:Haven't checked it out, but on PC the position is stored between sessions, but not within sessions. If you make a webdialog, close it, then open it again it doesn't remember the last position - only the last position from last session.


                  I haven't noticed because I am using Win32API calls to control where my Console is (.. I guess I gave up on Sketchup doing it. We can only wait so long for the vendor to fix bugs.)
                  0
                    I'm not here much anymore. But a PM will fire email notifications.
                    User avatar
                    Dan Rathbun 
                    PluginStore Author
                    PluginStore Author
                     

                    Re: [Code] YARC – yet another ruby console

                    Postby Dan Rathbun » Tue Jun 19, 2012 9:02 pm

                    There is a way around this issue.. but I hate it. It's not best practice.

                    You create a NEW instance each time the WD is opened, and abandon the old instance when it's closed.
                    You'll lose any info and snippet links I think.
                    0
                      I'm not here much anymore. But a PM will fire email notifications.
                      User avatar
                      Dan Rathbun 
                      PluginStore Author
                      PluginStore Author
                       

                      Re: [Code] YARC – yet another ruby console

                      Postby thomthom » Tue Jun 19, 2012 10:11 pm

                      So instances remember it... I see. They read the last position when the WebDialog initializes.
                      Good point about the Win32API, for my WebDialog wrapper in TT_Lib I can add that. ..though OSX only... creating new instance isn't ideal in many of my cases because I want to preserve the content throughout the session. :/
                      0
                      Thomas Thomassen — SketchUp Monkey & Coding addict
                      List of my plugins and link to the CookieWare fund
                      User avatar
                      thomthom 
                      PluginStore Author
                      PluginStore Author
                       

                      Re: [Code] YARC – yet another ruby console

                      Postby Dan Rathbun » Tue Jun 19, 2012 10:55 pm

                      thomthom wrote:... creating new instance isn't ideal in many of my cases because I want to preserve the content throughout the session. :/

                      Yepper... you (and Aerilius,) would have to write to a log, and restore it on open.

                      Actually, Aerilius is already saving the commands... he could just iterate them, and re-eval them to restore the output.
                      And perhaps the snippets are already saved in a "snippet" directory ??
                      0
                        I'm not here much anymore. But a PM will fire email notifications.
                        User avatar
                        Dan Rathbun 
                        PluginStore Author
                        PluginStore Author
                         

                        Re: [Code] YARC – yet another ruby console

                        Postby Dan Rathbun » Tue Jun 19, 2012 11:57 pm

                          Updated again to ver 0.6.3

                          (It REALLY now remembers where it the user puts it and it's size. We let Sketchup do the work here.)

                          !! Closing the dialog will clear it contents, and wipe out command snippets !!
                          So use minimize, instead of close, if you wish to keep these things.

                          See second post for d/l
                          0
                            I'm not here much anymore. But a PM will fire email notifications.
                            User avatar
                            Dan Rathbun 
                            PluginStore Author
                            PluginStore Author
                             

                            Re: [Code] YARC – yet another ruby console

                            Postby Anton_S » Wed Jun 20, 2012 9:55 pm

                            Dan Rathbun wrote:
                            Code: Select all
                            unless file_loaded?(File.basename(__FILE__))
                            ...
                            file_loaded(File.basename(__FILE__))



                            I recommend putting the whole __FILE__ instead of a File.basename(__FILE__). That way you would eliminate same filenames that are in different folders.

                            EX:
                            Plugins/Anton_Lib/core.rb
                              that statement would be true
                              unless file_loaded?(File.basename(__FILE__))
                                ...
                                file_loaded(File.basename(__FILE__))
                              end
                            Plugins/TT_Lib/core.rb
                              that statement would be false, preventing vital parts to be called
                              unless file_loaded?(File.basename(__FILE__))
                                ...
                                file_loaded(File.basename(__FILE__))
                              end
                            0

                            Anton_S 
                            PluginStore Author
                            PluginStore Author
                             

                            Re: [Code] YARC – yet another ruby console

                            Postby thomthom » Wed Jun 20, 2012 11:17 pm

                            Anton_S wrote:I recommend putting the whole __FILE__ instead of a File.basename(__FILE__). That way you would eliminate same filenames that are in different folders.

                            +1

                            I have adopted the same pattern myself. Just using the filename is not a unique ID - full path is. It doesn't even have to be a filename you feed file_loaded - just any unique string.
                            0
                            Thomas Thomassen — SketchUp Monkey & Coding addict
                            List of my plugins and link to the CookieWare fund
                            User avatar
                            thomthom 
                            PluginStore Author
                            PluginStore Author
                             

                            Re: [Code] YARC – yet another ruby console

                            Postby Dan Rathbun » Thu Jun 21, 2012 12:18 am

                            Yup we've talked a bit about this before. (I didn't change what he had in his code. I thot it might be too far off topic.)

                            I've posted a new topic on this subject:
                            [ Code ] custom file_loaded() and file_loaded?()

                            @Anton_S: I sent you a personal edition via PM, check your inbox.
                            0
                              I'm not here much anymore. But a PM will fire email notifications.
                              User avatar
                              Dan Rathbun 
                              PluginStore Author
                              PluginStore Author
                               

                              Re: [Code] YARC – yet another ruby console

                              Postby Anton_S » Thu Jun 21, 2012 6:57 am

                              yep, sure
                              0

                              Anton_S 
                              PluginStore Author
                              PluginStore Author
                               

                              Re: [Code] YARC – yet another ruby console

                              Postby Aerilius » Thu Jun 21, 2012 7:41 am

                              Anton_S wrote:I recommend putting the whole __FILE__

                              That's very reasonable. I'll fix it.

                              I just wanted to let you all know that I'm working on improvements (esp. the snippets should have been saved over sessions) but I'm a bit busy at the moment. I'll publish a next version when I'm ready.
                              0

                              Aerilius 
                              PluginStore Author
                              PluginStore Author
                               

                              SketchUcation One-Liner Adverts

                              by Ad Machine » 5 minutes ago



                              Ad Machine 
                              Robot
                               



                               

                              Return to Developers' Forum

                              Who is online

                              Users browsing this forum: No registered users and 13 guests