My question is concerning the "para" object. Where can I look up all the methods para has? I tried the shoesrb.com manual but all it says is that para is used to render text. I also tried #shoes at Freenode, but no one answered. Seems that no one is online.
I ask because I don't understand what the pounded (###) line does.
str, t = "", nil
Shoes.app :height => 500, :width => 450 do
background rgb(77, 77, 77)
stack :margin => 10 do
para span("TEXT EDITOR", :stroke => red, :fill => white), " * USE ALT-Q TO QUIT", :stroke => white
end
stack :margin => 10 do
t = para "", :font => "Monospace 12px", :stroke => white
t.cursor = -1 ####### I don't understand this line
end
keypress do |k|
case k
when String
str += k
when :backspace
str.slice!(-1)
when :tab
str += " "
when :left ### This is the part I'm interested in
#### Can you suggest a method to put in here. It moves the cursor to the left.
when :alt_q
quit
when :alt_c
self.clipboard = str
when :alt_v
str += self.clipboard
end
t.replace str
end
end
Does the para class have a cursor method? The official documentation has no answer.
I am trying to extend this into a simple text editor but I can't understand how to move the cursor.
I am a novice programer and new to Ruby as well.
Furthermore, where do the Shoes programmers hang out? I tried the mailing list, apparently it's out of service. Is there are specific forum or a different mailing list?
thanks for trying out Shoes.
Yes, Red Shoes a.k.a. Shoes 3 apparently has a cursor method that lets you set the position of the cursor. It's undocumented though, I had to look up in the sources. Your milage may vary using it.
The Shoes mailing list is definitely alive and active. Just send a message to shoes#librelist.com and you should automatically be signed up. That would be the best channel for help on Shoes, for the rest communication happens mostly through Github issues.
Related
In short i have a custom step definition to scroll down a page of search results until a text string is found.
It's scrolling down and finding the text string
But the while loop is not ending, despite it correctly scrolling down and stopping when the text string is found.
This is obviously causing me no end of but hurt.
Then /^I scroll until I see the "This is a test" text$/ do
q = query("android.widget.TextView text:'This is a test'")
while q.empty?
scroll("android.view.View id:'search_listView'", :down)
q = query("android.widget.TextView text:'This is a test'")
end
end
is the ruby that I've been using.
Which version of Calabash do you use?
I have written similiar step definition and it works perfectly for me (Calabash 0.5.1, Ruby 2.1.2).
Here is the code (I think it's more universal and simple):
Then /^I scroll down until I see '(.*?)' text$/ do |text|
while element_does_not_exist("TextView marked:'#{text}'")
scroll_down
end
end
Both element_does_not_exist() and scroll_down are Calabash Ruby API's functions.
Instead scroll_down you can try to use your function to scroll specified ScrollView.
(edit: Sorry, I didn't look at comments ;))
Try This...
def scroll_to_text(text)
element = "android.widget.TextView text:'#{text}'"
if !element_exists(element)
wait_poll(:until_exists => element, :timeout => 60, :screenshot_on_error => true) do
scroll("android.view.View id:'search_listView'", :down)
end
end
end
This method will give you a screenshot and raise an error if it didn't find the text scrolling after 60 seconds. you can modify to use as you wanted. (I just get the code from your post so if something is wrong at the first time try modifying this).
Let's say we want to display a title on the first page that takes up the top half of the page. The bottom half of the page should then fill up with our article text, and the text should continue to flow over into the subsequent pages until it runs out:
This is a pretty basic layout scenario but I don't understand how one would implement it in Prawn.
Here's some example code derived from their online documentation:
pdf = Prawn::Document.new do
text "The Prince", :align => :center, :size => 48
text "Niccolò Machiavelli", :align => :center, :size => 20
move_down 42
column_box([0, cursor], :columns => 3, :width => bounds.width) do
text((<<-END.gsub(/\s+/, ' ') + "\n\n") * 20)
All the States and Governments by which men are or ever have been ruled,
have been and are either Republics or Princedoms. Princedoms are either
hereditary, in which the bla bla bla bla .....
END
end
end.render
but that will just continue to show the title space for every page:
What's the right way to do this?
I have been fighting with this same problem. I ended up subclassing ColumnBox and adding a helper to invoke it like so:
module Prawn
class Document
def reflow_column_box(*args, &block)
init_column_box(block) do |parent_box|
map_to_absolute!(args[0])
#bounding_box = ReflowColumnBox.new(self, parent_box, *args)
end
end
private
class ReflowColumnBox < ColumnBox
def move_past_bottom
#current_column = (#current_column + 1) % #columns
#document.y = #y
if 0 == #current_column
#y = #parent.absolute_top
#document.start_new_page
end
end
end
end
end
Then it is invoked exactly like a normal column box, but on the next page break will reflow to the parents bounding box. Change your line:
column_box([0, cursor], :columns => 3, :width => bounds.width) do
to
reflow_column_box([0, cursor], :columns => 3, :width => bounds.width) do
Hope it helps you. Prawn is pretty low level, which is a two-edged sword, it sometimes fails to do what you need, but the tools are there to extend and build more complicated structures.
I know this is old, but I thought I'd share that a new option has been added to fix this in v0.14.0.
:reflow_margins is an option that sets column boxes to fill their parent boxes on new page creation.
column_box(reflow_margins: true, columns: 3)
So, the column_box method creates a bounding box. The documented behavior of the bounding box is that it starts at the same position as on the previous page if it changes to the next page. So the behavior you are seeing is basically correct, also not what you want. The suggested workaround I have found by googling is to use a span instead, because spans do not have this behavior.
The problem now is, how to build text columns with spans? They don't seem to support spans natively. I tried to build a small script that mimicks columns with spans. It creates one span for each column and aligns them accordingly. Then, the text is written with text_box, which has the overflow: :truncate option. This makes the method return the text that did not fit in the text box, so that this text can then be rendered in the next column. The code probably needs some tweaking, but it should be enough to demonstrate how to do this.
require 'prawn'
text_to_write = ((<<-END.gsub(/\s+/, ' ') + "\n\n") * 20)
All the States and Governments by which men are or ever have been ruled,
have been and are either Republics or Princedoms. Princedoms are either
hereditary, in which the bla bla bla bla .....
END
pdf = Prawn::Document.generate("test.pdf") do
text "The Prince", :align => :center, :size => 48
text "Niccolò Machiavelli", :align => :center, :size => 20
move_down 42
starting_y = cursor
starting_page = page_number
span(bounds.width / 3, position: :left) do
text_to_write = text_box text_to_write, at: [bounds.left, 0], overflow: :truncate
end
go_to_page(starting_page)
move_cursor_to(starting_y)
span(bounds.width / 3, position: :center) do
text_to_write = text_box text_to_write, at: [bounds.left, 0], overflow: :truncate
end
go_to_page(starting_page)
move_cursor_to(starting_y)
span(bounds.width / 3, position: :right) do
text_box text_to_write, at: [bounds.left, 0]
end
end
I know this is not an ideal solution. However, this was the best I could come up with.
Use floats.
float do
span((bounds.width / 3) - 20, :position => :left) do
# Row Table Code
end
end
float do
span((bounds.width / 3) - 20, :position => :center) do
# Row Table Code
end
end
float do
span((bounds.width / 3) - 20, :position => :right) do
# Row Table Code
end
end
Use Prawns grid layout instead. It is very well documented...and easier to control your layout.
I have used VBScript in the past for QTP and I could use the input box function to display a pop up window.
I am wondering if there is a way to do this with Ruby? I need a popup that will allow the user to input some information before the WATIR script executes.
I looked around StackOverflow but didn't see anything.
Here an example of how to get the messagebox from vbscript in Ruby, i'll try to get the inputbox in the same way
require "Win32API"
message = "This is a sample Windows message box generated using Win32API"
title = "Win32API from Ruby"
api = Win32API.new('user32','MessageBox',['L', 'P', 'P', 'L'],'I')
api.call(0,message,title,0)
Maybe this example code helps you (win32 only):
require 'win32ole'
def inputbox( message, title="Message from #{__FILE__}" )
vb_msg = %Q| "#{message.gsub("\n",'"& vbcrlf &"')}"|
vb_msg.gsub!( "\t", '"& vbtab &"' )
vb_msg.gsub!( '&""&','&' )
vb_title = %Q|"#{title}"|
# go!
sc = WIN32OLE.new( "ScriptControl" )
sc.language = "VBScript"
sc.eval(%Q|Inputbox(#{vb_msg}, #{vb_title})|)
#~ sc.eval(%Q|Inputbox(#{vb_msg}, #{vb_title}, aa,hide)|)
end
def popup(message)
wsh = WIN32OLE.new('WScript.Shell')
wsh.popup(message, 0, __FILE__)
end
str = "a | does not break it...\n\nOne\n\tTwo tabbed\nThree..."
res = inputbox( str, "demonstration | title")
popup %Q|When asked\n\n"#{str}"\n\nyou answered:\n#{res}|
This results in:
It follows a popup box with.
See also http://rubyonwindows.blogspot.com/2007/04/ruby-excel-inputbox-hack.html
Since i can't find an inputbox api for windows here is what i do most of the time if i need some simple dialog. Unlike red shoes it is just a gem so easy to install.
require 'green_shoes'
Shoes.app{
e = edit_line
button("Click me!"){alert("You entered." + e.text)}
}
Ruby is a programing/scripting language, so on it's own it doesn't do any kind of GUI or Graphics related stuff. That being said there are quite a few projects and frameworks that use Ruby to accomplish the type of thing you are looking for. The big ones are Ruby on Rails that uses Ruby to create web applications, and Shoes which is for creating computer applications.
There is also this question
What's the best/easiest GUI Library for Ruby?
I would suggest shoes, it's a cross-platform toolkit for writing graphical apps.
I used it and it's pretty cool, you can do something like:
Shoes.app :width => 300, :height => 200 do
stack do
edit_line :width => 400
end
end
I've got this as my code
openAll = File.open('N:\Josh\Blondie\db.txt')
allNumbers = Array.new
allNumbers=[]
openAll.each_line {|line|
allNumbers.push line
}
puts allNumbers
and I'd like to be able to display the output of this code in a new window with Ruby Shoes, I can't seem to get it to display anything though. The contents of the file are names and phone numbers.
Any ideas?
Here's an example of outputting text to a shoes window. Using a puts statement just outputs to the shell, not to the Shoes app.
Shoes.app :title => "GUI RAW file converter, for the CLI challenged",
:resizable => true do
background white
stack do
flow {
background gray, :height => 30
caption "Caption", :margin => 8, :stroke => white
stack {
para 'This is a fancy line I just printed to the window'
####### Here's an example line you could use to put out the array...
allNumbers.each do |number|
para "#{number}"
end
}
}
end
end
I guess you should use the method Kernel#alert instead of Kernel#puts.
http://shoesrb.com/manual/Built-in.html
I don't know anything in Ruby, but i'm pretty interested in DSLs. And DSL seems to be a buzz word for you community.
Do you actually implement DSLs in Ruby for your own purposes ? If so, how complex and how dedicated are they ?
i've seen this question here, but i'm more interested in your everyday experience.
thanks.
Here's another example of a Ruby DSL, it's called Mail, and it's a DSL for sending emails:
mail = Mail.new do
to 'nicolas#test.lindsaar.net.au'
from 'Mikel Lindsaar <mikel#test.lindsaar.net.au>'
subject 'First multipart email sent with Mail'
end
see here: http://github.com/mikel/mail
My own experience writing DSLs in Ruby is quite limited but i've done the following:
(1) An L-System DSL:
Dragon = TexPlay::LSystem.new {
rule "F" => "F"
rule "X" => "X+YF+"
rule "Y" => "-FX-Y"
angle 90
atom "FX"
}
(2) An image manipulation tool:
image.paint {
circle 20, 20, 15, :color => :red
rect 10, 20, 100, 100, :color => :green
pixel 40, 40, :color => :white
}
Its Ruby's speciality, to get everything working very quickly, But it could become tough to manage. I would say, for small or medium DSL projects ruby is awesome. As i haven't created any big DSL project in Ruby, i cannot recommend it(for bigger projects).