Specific events in sublime text 3 - sublimetext

I am writing a plugin for Sublime Text 3 such that I need to do either of two things:
1) Run a function every 300 milliseconds
2) Run a function whenever the cursor changes position
Although I have been looking over the documentation and examining the examples in the Default package, I yet am having trouble achieving my goal. What would help me is small example that prints "hello, world" to a buffer every time one of the above 2 conditions are met. Thank you.

As an answer I gave to this question it has something similar, you also add set_timeout for running every 300ms:
annoying_helloworld.py
class someclass():
def run(x):
print("Hello world!" + str(x))
def run_with_timeout(x):
someclass.run(x)
sublime.set_timeout(lambda: someclass.run_with_timeout(123), 300)
class utfcodeCommand(sublime_plugin.EventListener):
def on_selection_modified(self, view):
someclass.run(666)
sublime.set_timeout(lambda: someclass.run_with_timeout(123), 300)

Related

Simplify Move and Maximise screen using Selenium (Ruby)

I am running my selenium tests using a ruby Framework having recently joined a new company. As I am working on 3 screens I have added in a browser step to move the browser window to my left most screen before maximising using
#driver.manage.window.move_to(-1800, 1500)
#driver.manage.window.maximize
My question very simply is, having never used Ruby before, is there a way to refactor these 2 lines into 1 I have tried various ways but always get an error.
It's not possible in this place because both are returning string object so you can't cascade the calling like
#driver.manage.window.move_to(-1800, 1500).maximize
But If it's very important for you, then I will tell you a way to do this, open the window.rb file where you can return the self from move_to function as shown below
def move_to(x, y)
#bridge.reposition_window Integer(x), Integer(y)
self
end
And then you can make a call like this
#driver.manage.window.move_to(-1800, 1500).maximize

How to do seek with stdout?

I am trying to implement the Sudoku game in ruby. In that I had a one problem. The problem is I am not able to handle with multiple lines at a time. For example, I have a 5 * 5 boxes. In each boxes the user need to enter the number. For example the user needs to put the number in the second box, I don't know how to go to the second box. Because, the second box is available in the first line. But I was written more than 10 lines.
I was searched in internet. In that they said one way is to use the seek
method. But the seek method is not working for the stdout.
I did not implemented the Sudoku code. I just tried how to seek the stdout with simple example.
Here is the example code,
file = $stdout.dup
file.puts "######### ##########"
file.puts "####################"
file.puts "####################"
file.puts "####################"
file.puts "####################"
file.pos=10
file.puts "#"
In this example, The 10th position contains the space, I need to change the space to the #\n. For that I was tried the above code. But it will not work.
Can anyone please explain me why it is not working and how to do this in only ruby not using rails?
Not every filehandle is seekable. If stdout is a disk file, then you can probably seek it, but if stdout is a terminal, then you won't be able to.

Connecting Shoes GUI to existing Ruby code

I built a command line game in Ruby and now I'm trying to build a GUI for the game using Shoes. I spent the morning reading about Shoes, looking at some code samples, and writing a bit of code myself. It appears (I'm new to Shoes, so this could be totally wrong) that the Shoes code (for example, a shoes.rb file that you open with the Shoes application) has to contain all the Ruby code you want to run. All of the Ruby code will exist within that file.
The way I'm firing up this game is at the command line by passing command line arguments to play.rb (a Ruby script). Either run play.rb "command line game" or play.rb "shoes game" at the command line. If you run the latter, it will make a system call to open shoes.rb with the Shoes application. Within play.rb it will pass ShoesInterface to the players, rather than CommandLineInterface. Below is an example of how a player might be asked for a move:
In the human_player.rb file:
def make_move
#interface.print("It's your turn to make a move.")
end
#interface will either be CommandLineInterface or ShoesInterface. The print method in CommandLineInterface is simply a puts statement. The print method in ShoesInterface should tell the Shoes GUI to display that text. I'm trying to re-use as much of my code as possible. Theoretically, regardless of how someone is playing the game (on the web, at the command line, through a GUI, etc.) it should use most of the same logic. The interfaces simply display messages and receive user input. They just do it in different ways.
The problem is that I don't know how to connect my Shoes GUI to my existing Ruby code. Is anyone here proficient in Shoes? This might not be possible in Shoes...maybe I'll have to use a different Ruby GUI, but I thought I'd ask before moving on to another one.
What you can do, is to write a Shoes Widget that will respond to method print. Below is a very crude example of such a widget which will append a message from the edit line to its arbitrary display slot. Of course you can easily modify the Widget so that you can initialize it with a target slot for display etc but the idea stays the same.
class Prompt < Shoes::Widget
def initialize opts={}
#top=opts[:top]
#left=opts[:left]
#width=opts[:width]
#prom=flow :top=>#top, :left=>#left, :width=>#width do
background red
end
end
def print(msg)
#prom.append do
para msg
end
end
end
Shoes.app :title => "Test" do
#el=edit_line
button "print"do
#interface.print(#el.text)
end
#interface=prompt({:top=>50, :left=>20, :width=>100})
end
I hope that is what yo wanted.

Nokogiri builder performance on huge XML?

I need to build a huge XML file, about 1-50 MB. I thought that using builder would be effective enough and, well it is, somewhat. The problem is, after the program reaches its last line it doesn't end immediately, but Ruby is still doing something for several seconds, maybe garbage collection? After that the program finally ends.
To give a real example, I am measured the time of building an XML file. It outputs 55 seconds (there is a database behind so it takes long) when the XML was built, but Ruby still processes for about 15 more seconds and the processor is going crazy.
The pseudo/real code is as follows:
...
builder = Nokogiri::XML::Builder.with(doc) do |xml|
build_node(xml)
end
...
def build_node(xml)
...
xml["#{namespace}"] if namespace
xml.send("#{elem_name}", attrs_hash) do |elem_xml|
...
if has_children
if type
case type
when XML::TextContent::PLAIN
elem_xml.text text_content
when XML::TextContent::COMMENT
elem_xml.comment text_content
when XML::TextContent::CDATA
elem_xml.cdata text_content
end
else
build_node(elem_xml)
end
end
end
end
Note that I was using a different approach using my own structure of classes, and the speed of the build was the same, but at the last line the program normally ended, but now I am forced to use Nokogiri so I have to find a solution.
What I can do to avoid that X seconds long overhead after the XML is built? Is it even possible?
UPDATE:
Thanks to a suggestion from Adiel Mittmann, during the creation of my minimal working example I was able to locate the problem. I now have a small (well not that small) example demonstrating the problem.
The following code is causing the problem:
xml.send("#{elem_name}_") do |elem_xml|
...
elem_xml.text text_content #This line is the problem
...
end
So the line executes the following code based on Nokogiri's documentation:
def create_text_node string, &block
Nokogiri::XML::Text.new string.to_s, self, &block
end
Text node creation code gets executed then. So, what exactly is happening here?
UPDATE 2:
After some other tries, the problem can be easily reproduced by:
builder = Nokogiri::XML::Builder.new do |xml|
0.upto(81900) do
xml.text "test"
end
end
puts "End"
So is it really Nokogiri itself? Is there any option for me?
Your example also takes a long time to execute here. And you were right: it's the garbage collector that's taking so long to execute. Try this:
require 'nokogiri'
class A
def a
builder = Nokogiri::XML::Builder.new do |xml|
0.upto(81900) do
xml.text "test"
end
end
end
end
A.new.a
puts "End1"
GC.start
puts "End2"
Here, the delay happens between "End1" and "End2". After "End2" is printed, the program closes immediately.
Notice that I created an object to demonstrate it. Otherwise, the data generated by the builder can only be garbage collected when the program finishes.
As for the best way to do what you're trying to accomplish, I suggest you ask another question giving details of what exactly you're trying to do with the XML files.
Try using the Ruby built-in (sic) Builder. I use it to generate large XML files as well, and it has such an small footprint.

Need to get information from Qt4ruby Form's textedit(textbox) and pass back to string for console

I think this problem is best described in code. I'm sure the solution is close, I just haven't been able to find it. I've been looking over the Qt4 api as well as doing tutorials. Here is my code so far:
require 'Qt4'
class PictureCommentForm < Qt::Widget
def initialize(parent = nil)
super()
#setFixedSize(300, 100)
#comment_text = nil
picture = Qt::Label.new()
image = Qt::Image.new('image.jpeg')
picture.pixmap = image
comment = Qt::LineEdit.new()
layout = Qt::VBoxLayout.new()
layout.addWidget(picture)
layout.addWidget(comment)
setLayout(layout)
connect(comment, SIGNAL('returnPressed()'), self, setCommentText(comment.text) )
end
def setCommentText(text)
#comment_text = text
$qApp.quit()
end
end
app = Qt::Application.new(ARGV)
comment_form = PictureCommentForm.new()
comment_form.show()
app.exec
comment_text = comment_form.comment_text
puts "Comment was:\n #{comment_text}"
EDIT: Thanks for that answer integer. All I want done is a dialog box showing a picture and comment so I can get that data. I do plan on making a full GUI version with qt4, but that's for later.
I don't know Ruby, so bear with me, but I use Qt extensively in Python.
First point is that Qt really, really doesn't want to be used the way you're trying to use it. If you're making some sort of script, then Qt wants you to give it to Qt so it can run your code when it feels like:
We recommend that you connect clean-up
code to the aboutToQuit() signal,
instead of putting it in your
application's main() function because
on some platforms the
QCoreApplication::exec() call may not
return.
Working with Qt you pretty much have to do event-driven programming and give it control of your program flow / main loop.
If you really just want some "utility" that shows some GUI input box and prints whatever the user inputs to console, consider putting the puts directly in whatever function you connected to the text box. Then you can use that program's output in other console scripts.

Resources