• Login
sketchucation logo sketchucation
  • Login
⚠️ Libfredo 15.4b | Minor release with bugfixes and improvements Update

[Code] YARC – yet another ruby console

Scheduled Pinned Locked Moved Developers' Forum
22 Posts 4 Posters 3.7k Views 4 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    Aerilius
    last edited by Aerilius 18 Jun 2012, 22:25

    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?


    ae_Console.zip

    1 Reply Last reply Reply Quote 0
    • D Offline
      Dan Rathbun
      last edited by 18 Jun 2012, 23:39

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

      
        def open
          dlg = @console_dlg
          if dlg.visible?
            dlg.bring_to_front
          else
            OSX ? dlg.show_modal() ; dlg.show()
          end
        end
      
      

      I'm not here much anymore.

      1 Reply Last reply Reply Quote 0
      • D Offline
        Dan Rathbun
        last edited by 19 Jun 2012, 00:09

        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

        I'm not here much anymore.

        1 Reply Last reply Reply Quote 0
        • D Offline
          Dan Rathbun
          last edited by 19 Jun 2012, 01:22

          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:

                @console_dlg.set_on_close{
                  save_defaults
                  @@open = false
                }
          
          

          to:

                @console_dlg.add_action_callback("do_on_close") { |dlg, param|
                  save_defaults
                  @@open = false
                }
          
          

          Nit-Pickin': 😛

          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.)

          💭

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • D Offline
            Dan Rathbun
            last edited by 19 Jun 2012, 02:22

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

            I'm not here much anymore.

            1 Reply Last reply Reply Quote 0
            • A Offline
              Anton_S
              last edited by 19 Jun 2012, 08:02

              😄 😛 Wow, wow, wow, yahooooooooooooooooo1!!!!!! ☀ 😄

              Jst awesome, that ruby console could save a lot of time 😍

              1 Reply Last reply Reply Quote 0
              • thomthomT Offline
                thomthom
                last edited by 19 Jun 2012, 08:23

                Screenshot for the OP please? ☀

                Thomas Thomassen — SketchUp Monkey & Coding addict
                List of my plugins and link to the CookieWare fund

                1 Reply Last reply Reply Quote 0
                • A Offline
                  Aerilius
                  last edited by 19 Jun 2012, 08:43

                  @dan rathbun said:

                  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 said:

                  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 said:

                  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 said:

                  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.

                  1 Reply Last reply Reply Quote 0
                  • D Offline
                    Dan Rathbun
                    last edited by 19 Jun 2012, 16:57

                    @dan rathbun said:

                    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.

                    I'm not here much anymore.

                    1 Reply Last reply Reply Quote 0
                    • A Offline
                      Aerilius
                      last edited by 19 Jun 2012, 19:05

                      I added a screenshot.

                      @dan rathbun said:

                      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.

                      1 Reply Last reply Reply Quote 0
                      • thomthomT Offline
                        thomthom
                        last edited by 19 Jun 2012, 19:58

                        @dan rathbun said:

                        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. 😞

                        Thomas Thomassen — SketchUp Monkey & Coding addict
                        List of my plugins and link to the CookieWare fund

                        1 Reply Last reply Reply Quote 0
                        • D Offline
                          Dan Rathbun
                          last edited by 19 Jun 2012, 19:59

                          @dan rathbun said:

                          @dan rathbun said:

                          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!

                          @unknownuser said:

                          (http://forums.sketchucation.com/viewtopic.php?f )":2mfzj5er]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.)

                          I'm not here much anymore.

                          1 Reply Last reply Reply Quote 0
                          • D Offline
                            Dan Rathbun
                            last edited by 19 Jun 2012, 20:02

                            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.

                            I'm not here much anymore.

                            1 Reply Last reply Reply Quote 0
                            • thomthomT Offline
                              thomthom
                              last edited by 19 Jun 2012, 21:11

                              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. 😕

                              Thomas Thomassen — SketchUp Monkey & Coding addict
                              List of my plugins and link to the CookieWare fund

                              1 Reply Last reply Reply Quote 0
                              • D Offline
                                Dan Rathbun
                                last edited by 19 Jun 2012, 21:55

                                @thomthom said:

                                ... 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 ??

                                I'm not here much anymore.

                                1 Reply Last reply Reply Quote 0
                                • D Offline
                                  Dan Rathbun
                                  last edited by 19 Jun 2012, 22:54

                                  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:

                                  @@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:

                                  
                                      (OSX)? @console_dlg.show_modal ; @console_dlg.show
                                      return @console_dlg
                                    end
                                  
                                  

                                  Then the instance open method should be:

                                  
                                    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()

                                  
                                    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?)

                                  
                                  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:

                                  
                                    # Provide a handle to the command, in case
                                    # some other script wants to add it to a toolbar.
                                    #
                                    def self.command
                                      @@command
                                    end
                                  
                                  

                                  I'm not here much anymore.

                                  1 Reply Last reply Reply Quote 0
                                  • D Offline
                                    Dan Rathbun
                                    last edited by 19 Jun 2012, 22:57

                                    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

                                    I'm not here much anymore.

                                    1 Reply Last reply Reply Quote 0
                                    • A Offline
                                      Anton_S
                                      last edited by 20 Jun 2012, 20:55

                                      @dan rathbun said:

                                      
                                      > 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`

                                      1 Reply Last reply Reply Quote 0
                                      • thomthomT Offline
                                        thomthom
                                        last edited by 20 Jun 2012, 22:17

                                        @anton_s said:

                                        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.

                                        Thomas Thomassen — SketchUp Monkey & Coding addict
                                        List of my plugins and link to the CookieWare fund

                                        1 Reply Last reply Reply Quote 0
                                        • D Offline
                                          Dan Rathbun
                                          last edited by 20 Jun 2012, 23:18

                                          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.

                                          I'm not here much anymore.

                                          1 Reply Last reply Reply Quote 0
                                          • 1
                                          • 2
                                          • 1 / 2
                                          1 / 2
                                          • First post
                                            1/22
                                            Last post
                                          Buy SketchPlus
                                          Buy SUbD
                                          Buy WrapR
                                          Buy eBook
                                          Buy Modelur
                                          Buy Vertex Tools
                                          Buy SketchCuisine
                                          Buy FormFonts

                                          Advertisement