Line not rendering - ruby

I am developing some prawn reports and running into an issue where any line I draw with a code like the following will render only in the last page.
horizontal_line(0, 200, :at => y)
It is called once per page.
My code is relatively complex now so I tried to isolate the problem to post here, the isolated code follows
require 'prawn'
a = Prawn::Document.new(:page_size => 'A4', :margin => [20,20,20,20])
a.font('Times-Roman')
a.horizontal_line(10, 400, :at => 140)
a.text_box('Test Text', :size => 50, :at => [2, 100], :width => 400)
puts a.render
For my surprise, it didnĀ“t work even with a single page document. Only the "Test Text" is being rendered. It makes me think I am doing something wrong in the page setup or something like that.

Fond out the problem.
The correct use would be:
require 'prawn'
a = Prawn::Document.new(:page_size => 'A4', :margin => [20,20,20,20])
a.font('Times-Roman')
a.stroke do
a.horizontal_line(10, 400, :at => 140)
end
a.text_box('Test Text', :size => 50, :at => [2, 100], :width => 400)
puts a.render

Related

Ruby Tkinter scrollbar doesn't appear in its frame and allows scrolling past canvas bounds. What am I doing wrong?

I've been fighting with Tkinter in ruby to try and get a proper scrollable frame working.
I've had the most success with this tutorial: https://www.youtube.com/watch?v=VmlgrrXAqb4
I have translated what he does in the video into ruby and it "works", but only kinda. When I run the application I get a window similar to his with all of the buttons, but the scrollbar handle doesn't appear in it's frame and I can scroll past the content of the frame in both directions.
I'm trying to build an application that has a heavy use of scrolling, but I can't get past this. I am posting as a last resort. I've tried most of the examples I can find online, but I keep getting unexpected behavior. It is worth noting that I am on MacOS in case someone tries the code on Windows or Linux and it works for some reason.
require 'tk'
require 'tkextlib/tkimg'
win = TkRoot.new
win.geometry "500x500"
win.resizable false,false
wrapper1 = Tk::Tile::Frame.new win
wrapper2 = Tk::Tile::Frame.new win
mycanvas = TkCanvas.new wrapper1
mycanvas.pack :side => "left", :fill => "both", :expand => "yes"
yscrollbar = TkScrollbar.new :parent => wrapper1, :command => Proc.new {|*args| mycanvas.yview *args}
yscrollbar.pack :side => "left", :fill => "y"
mycanvas.configure(:yscrollcommand => Proc.new {|*args| yscrollbar.set *args })
mycanvas.bind "<Configure>", Proc.new {mycanvas.scrollregion = mycanvas.bbox("all")}
myframe = Tk::Tile::Frame.new mycanvas
TkcWindow.new(mycanvas, 0, 0, :anchor => "nw", :window => myframe)
wrapper1.pack :fill => "both", :expand => "yes", :padx => 10, :pady => 10
wrapper2.pack :fill => "both", :expand => "yes", :padx => 10, :pady => 10
for i in 0..50
button = TkButton.new(myframe)
button.text = "Button " + i.to_s
button.pack
end
Tk.mainloop
I figured out what was wrong.
#The line of code below where the Configure event is bound needs to be:
mycanvas.bind "Configure" #...
#instead of:
mycanvas.bind "<Configure>" #...

Retrieve first column value from TK::Tile::Treeview in Ruby

I try to retrieve the value of the first column from an item retrieved from a treeview using "item.get("#0")" but I got this error "RuntimeError: Display column #0 cannot be set". This method works for the other columns.
Can anyone help to figure out a solution?
Regards,
Marc
Here is a standalone example code:
require 'tk'
require 'tkextlib/tile'
$root = TkRoot.new
$frame = Tk::Tile::Frame.new($root)
$tree = Tk::Tile::Treeview.new($frame)
$tree['columns'] = ['action_text','action_description']
$tree.column_configure("#0", :width => 100)
$tree.heading_configure("#0", :text => 'la 1er colonne')
$tree.column_configure('action_text', :width => 100, :anchor => 'center')
$tree.heading_configure('action_text', :text => 'Text')
$tree.column_configure('action_description', :width => 100, :anchor => 'w')
$tree.heading_configure('action_description', :text => 'Description')
# Inserted at the root, program chooses id:
$tree.insert('', 'end', :id => 'widgets', :text => 'Widget Tour')
# Same thing, but inserted as first child:
$tree.insert('', 0, :id => 'gallery', :text => 'Applications')
# Treeview chooses the id:
item = $tree.insert('', 'end', :text => 'Tutorial')
# Inserted underneath an existing node:
$tree.insert( 'widgets', 'end', :text => 'Canvas')
$tree.insert( item, 'end', :text => 'Tree')
$tree.insert('', 2, :text => 'tata')
$tree.insert('', 'end', :text => 'envolee', :values => ['le centre', 'la description'])
$frame.grid(:column => 0, :row => 0, :sticky => 'nsew') {padding "3 3 12 12"}
$tree.grid(:column => 0, :row => 0, :sticky => 'nsew')
TkGrid.columnconfigure($root, 0, :weight => 1)
TkGrid.rowconfigure($root, 0, :weight => 1)
TkGrid.columnconfigure($frame, 0, :weight => 1)
TkGrid.rowconfigure($frame, 0, :weight => 1)
def create_action
item = $tree.selection_get[0]
puts item
puts "ID:"
puts item.id
puts "Index:"
puts item.index
puts "action_text:"
puts item.get('action_text')
puts "1:"
puts item.get('#1')
puts $tree.get(item.id, '#0')
puts "0:"
puts item.get("#0")
end
$tree.bind("Double-1") { create_action }
Tk.mainloop
I do not know if I understand exactly what you want to do,
item.text
might be a solution.

Ruby Shoes Hover element

Hover element dosen't work. Where is problem? How fix it?
Shoes.app :width => 635, :height => 410 do
background image "http://PATH_TO_IMAGE"
a = stack :width => 360, :height => 200, :margin_left => 250, :margin_top => 200 do
hover do
a.clear { para "Some Text qwe qwe qwerty", :stroke => black }
end
leave do
a.clear { background image "http://PATH_TO_IMAGE" }
end
end
end
I think in principle you cannot use image as the argument for background.
I also am not sure if that is effect you want to achieve but it shows what you did wrong:
Shoes.app :width => 635, :height => 410 do
a = stack :width => 360, :height => 200 do
image "http://PATH_TO_IMAGE"
hover do
a.clear {para "Some Text qwe qwe qwerty", :stroke => black }
end
leave do
a.clear { image "http://PATH_TO_IMAGE" }
end
end
end

ANSI escape code with html tags in Ruby?

Interestingly there are built-in ansi escape code in Ruby.
There is also a more powerful version from a gem.
Unfortunately, these logs output to the console. My text is shown in the page so I need HTML tags to wrap around my text.
Would you guys have any idea how to go about it?
I guess what you want is to transform from escape characters to HTML.
I did it once by assuming the following code/colour hash for escape characters:
{ :reset => 0,
:bright => 1,
:dark => 2,
:underline => 4,
:blink => 5,
:negative => 7,
:black => 30,
:red => 31,
:green => 32,
:yellow => 33,
:blue => 34,
:magenta => 35,
:cyan => 36,
:white => 37,
:back_black => 40,
:back_red => 41,
:back_green => 42,
:back_yellow => 43,
:back_blue => 44,
:back_magenta => 45,
:back_cyan => 46,
:back_white => 47}
What I did was the following conversion (far away from being anyhow optimized):
def escape_to_html(data)
{ 1 => :nothing,
2 => :nothing,
4 => :nothing,
5 => :nothing,
7 => :nothing,
30 => :black,
31 => :red,
32 => :green,
33 => :yellow,
34 => :blue,
35 => :magenta,
36 => :cyan,
37 => :white,
40 => :nothing,
41 => :nothing,
43 => :nothing,
44 => :nothing,
45 => :nothing,
46 => :nothing,
47 => :nothing,
}.each do |key, value|
if value != :nothing
data.gsub!(/\e\[#{key}m/,"<span style=\"color:#{value}\">")
else
data.gsub!(/\e\[#{key}m/,"<span>")
end
end
data.gsub!(/\e\[0m/,'</span>')
return data
end
Well, you will need fill the gaps of the colours I am not considering or backgrounds. But I guess you can get the idea.
Hope it helps
Thank you for the link to a cool gem I had not seen. I think what you are looking for, however, is termed Cascading Style Sheets (CSS). Because that google search will bring up about every other page cached on the internet, here are a few links for you that should get you started:
http://www.w3schools.com/css/default.asp - start with the most basic stuff.
http://guides.rubyonrails.org/layouts_and_rendering.html
http://rorrocket.com/ - generalized tutorials
http://nubyonrails.com/articles/dynamic-css - this may or may not be useful but a brief glance might provide some more rails like info on css, including SASS*.
*SASS is a ruby-ized abstraction to CSS used very frequently with ruby/rails

Setting cell/column widths on a Prawn table

I'm making a little script with ruby which produces a week schedule PDF file, using Prawn as a PDF library and I'm struggling with styling the table. I'd like to set a static width for all the columns in the table so that the widths wouldn't depend on the contents of the cells.
I've read the documentation (lot of room for improvement there) from the Prawn project site and googled for a few hours, but I'm lost at how to set width for columns or cells in a table, or how to style the columns/cells in any way whatsoever. I do get a PDF file which has a grid layout though, the cells just vary in size a lot, which doesn't look that neat.
This didn't work:
Prawn::Document.generate(#filename, :page_size => 'A4', :page_layout => :landscape) do
table(course_matrix, :headers => HEADERS, :border_style => :grid, :row_colors => ['dddddd', 'eeeeee'], :column_widths => 50)
end
Here's the current version of my method to generate PDF, but it doesn't stylize the cells either:
def produce_pdf
course_matrix = DataParser.new.parse_for_pdf
Prawn::Document.generate(#filename, :page_size => 'A4', :page_layout => :landscape) do
table(course_matrix, :headers => HEADERS, :border_style => :grid, :row_colors => ['dddddd', 'eeeeee']) do |table|
table.cells.style { |cell| cell.width = 50 }
end
end
end
I do something like this:
pdf = Prawn::Document.new(
:page_size => 'A4',
:page_layout => :landscape,
:margin => [5.mm])
....
....
pdf.table(tbl_data) do
row(0).style(:background_color => 'dddddd', :size => 9, :align => :center, :font_style => :bold)
column(0).style(:background_color => 'dddddd', :size => 9, :padding_top => 20.mm, :font_style => :bold)
row(1).column(1..7).style(:size => 8, :padding => 3)
cells[0,0].background_color = 'ffffff'
row(0).height = 8.mm
row(1..3).height = 45.mm
column(0).width = 28.mm
column(1..7).width = 35.mm
row(1..3).column(6..7).borders = [:left, :right]
row(3).column(6..7).borders = [:left, :right, :bottom]
....
pdf.render()
More info here.
To set a static width for all the columns I do something like this:
REPORT_FIELDS = %w[DESCRIPTION PRICE DATE NOTE].freeze
A4_SIZE = 200.freeze
data = []
data << REPORT_FIELDS
... things happen ...
table(data, column_widths: (A4_SIZE/REPORT_FIELDS.size).mm))
In this case I wanted to set the table to fit the full page and with the cells with the same width.

Resources