Blinking string in curses application - ruby

In the meagre documentation for ruby curses I found this method
A_BLINK
Blinking
See ::attrset
However, I don't know how to utilize it.
win1 = Window.new
win1.addstr.a_blink "Blinking" #=> error
Please don't blame me, there is literally no help on google regarding curses. Honestly, at least not for ruby.

You can set attributes with Curses::Window#attrset. Here's an example:
require "curses"
include Curses
init_screen
begin
attrs = {
A_NORMAL => 'Normal display (no highlight)',
A_STANDOUT => 'Best highlighting mode of the terminal.',
A_UNDERLINE => 'Underlining',
A_REVERSE => 'Reverse video',
A_BLINK => 'Blinking',
A_DIM => 'Half bright',
A_BOLD => 'Extra bright or bold',
A_PROTECT => 'Protected mode',
A_INVIS => 'Invisible or blank mode',
A_ALTCHARSET => 'Alternate character set',
}
attrs.each { |a, s|
attrset(a)
addstr("#{s}\n")
}
refresh
getch
ensure
close_screen
end

Related

Ruby's Mechanize balks when selecting a radio button (maybe because its name is capitalized) but Perl's WWW::Mechanize works fine

I'm trying to submit a form with Ruby's Mechanize gem. This form has a set of radio buttons named "KeywordType". The individual buttons are named something like rdoAny, rdoAll and rdoPhrase. With Perl's WWW:Mechanize it works just fine:
my $result = $agent->submit_form(
form_number => 1,
fields => {
txtKeywords => 'foo bar baz',
lstLocationCode => '2100',
lstONETMajorGroup => '0',
KeywordType => 'rdoAny'
},
button => 'btnSearch'
);
but Ruby balks when I do this:
result = page.form_with(:id => 'frmSearch') do |field|
field.txtKeywords = 'foo bar baz'
field.lstLocationCode = '2100'
field.lstONETMajorGroup = '0'
field.KeywordType = 'rdoAny'
end.submit
This throws the error
"undefined method `KeywordType=' for #<Mechanize::Form:0x00000001c896e0> (NoMethodError)".
I've tried leaving out the KeywordType field, but then I just get sent back to the same page with no obvious error message. I've also tried doing things like field.radiobuttons.second.check and field.radiobuttons_with(:name => "KeywordType") to no avail.
And on a side note, is whatever's going on because Ruby sees a capitalized radiobutton name and thinks it's a constant?
Thanks.
Does this work?
field['KeywordType'] = 'rdoAny'
Edit: Oh, and I think you missed a part here:
result = page.form_with(:id => 'frmSearch') do |field|
should be (I think):
result = page.form_with(:id => 'frmSearch').fields.each do |field|
Gaah. The outdated version gremlin strikes again. "gem update mechanize" is now at least showing me the values for the two dropdowns.

Understanding Tk Listbox in ruby

I'm trying to make a small program to mung some data into usable form. One thing I'd like it to do is to be able to select some files and perform actions on them, so I thought i'd use the listbox object in Tk to do that. I want to be able to open a file and see its filename displayed in the listbox. As far as I've read this is precisely what using listvariable in the listbox is for. Yet when I run my code the listbox is never updated (although items already in the listvariable variable are displayed fine).
So here's a close to MWE for this. What am I doing wrong, and what fundamental idea have I misunderstood?
require 'tk'
require 'tkextlib/tile'
$path_list = []
$populate_list = TkVariable.new( $path_list )
def get_file
file = Tk.getOpenFile
file = open(file) unless file.empty?
path = File.basename(file, ".out")
if $path_list.include?(path)
Tk.messageBox(
'type' => "ok",
'icon' => "warning",
'title' => " - Minimum Working Example - ",
'message' => "This file has already been added! Nothing was added to the list"
)
else
$path_list.push(path)
end
end
root = TkRoot.new {title "- Minimum Working Example -"}
frame = Tk::Tile::Frame.new(root) {padding "3 3 12 12"}.grid( :sticky => 'nsew') # 'north south east west'
TkGrid.columnconfigure root, 0, :weight => 1; TkGrid.rowconfigure root, 0, :weight => 1
$file_listbox = Tk::Listbox.new(frame) {
listvariable $populate_list}.grid( :column => 1, :row => 0, :rowspan => 6)
Tk::Tile::Button.new(frame) {
width 15; text 'Open file...'; command {get_file}}.grid( :column => 0, :row => 1)
Tk.mainloop
Do I maybe have to write it in some other order?
Just add one line of code:
$populate_list.value = $path_list
under this one:
$path_list.push(path)
It works for me, although looks weird.
TkVariable create a proxy for you ruby variable, thus bridge your ruby var references with Tk widgets. But i don't know why changes in proxy var don't affect the var it points to. I'm not sure whether it should do that automatically.

checking if a html element is true with ruby and watir?

at the moment I have this:
def burnChart()
#browser.div(:id => "container").div(:id => "header").img(:class => "cogMenuHover").click
#browser.div(:id => "container").div(:id => "header").div(:class => "sbTopMenu").li(:class => "taskMenuOp", :index =>1).click
if
assert(#browser.send(type.to_sym, :class, "highcharts-grid").exists?)
puts 'Chart has been found!'
else
puts 'No chart was generated'
end
end
originally I thought I had to use to_css? but from what I've seen of others using it, that's incorrect I'm unsure.
Can anyone help me out, I just want to check if a class exists and return a true or fasle to log an error or a confirmation
I believe you want:
def burnChart()
#browser.div(:id => "container").div(:id => "header").img(:class => "cogMenuHover").click
#browser.div(:id => "container").div(:id => "header").div(:class => "sbTopMenu").li(:class => "taskMenuOp", :index =>1).click
#Due to using IE, it looks like .exists? has to be used.
if #browser.element(:class, "highcharts-grid").exists?
puts 'Chart has been found!'
else
puts 'No chart was generated'
end
end
Notice that:
The check for the element should be the condition for the if statement.
Changed the check to be .present?. .exists? returns true as long as the element is on the page, but usually you also want to check that it is visible.
The switch to #browser.element instead of #browser.send. You can do what you were doing, but you were not defining a type anywhere, so seemed unnecessary.

Error generating pdf using prawn

I am trying to use tags to give some styling for a pdf being generated using prawn. But, there seems to be an error.
require 'rubygems'
require 'prawn'
require 'prawn/layout'
require 'prawn/format'
Prawn::Document.generate "example.pdf" do
tags:h1=>{ :font_size => "16pt", :font_weight => :bold }
text"<h1>Student Details</h1>"
end
I get the following error -
/usr/lib/ruby/gems/1.8/gems/prawn-format-0.2.3/lib/prawn/format/text_object.rb:91:in `%': can't convert nil into Float (TypeError)
Any help is greatly appreciated.
Cheers!!
Shouldn't it be:
tags[:h1] = { :font_size => "16pt", :font_weight => :bold }
?
Also please note that:
As of Prawn 0.7, prawn-format is completely unsupported, and will not
work with versions of Prawn 0.7+. Feel free to fork and fix, of
course.
Consider using methods from Prawn::Text
http://rubydoc.info/gems/prawn/0.12.0/Prawn/Text
EDIT
For example:
require 'rubygems'
require 'prawn'
Prawn::Document.generate('font_calculations.pdf') do
font "Courier", :size => 16, :style => :bold
text "Student details"
font "Courier", :size => 12, :style => :normal
text "normal text"
text "this is normal, <b>but this is bold</b>", :inline_format => true
text "normal <font size='18'>bigger</font> normal", :inline_format => true
end
That's just one of many ways of doing this.

Alert box in Waitr Webdriver

I had a look here: http://wiki.openqa.org/display/WTR/JavaScript+Pop+Ups
Every solution is for IE on Windows. I am using Firefox on Mac. Is there a way to click on OK of a JavaScript alert box?
Proper handling of alerts and prompts is still being worked on in WebDriver, but a common workaround is to overwrite the window functions using execute_script(), i.e.
browser.execute_script("window.alert = function(msg) { window.lastAlert = msg; }")
browser.button(:id => "trigger-alert").click
browser.execute_script("return window.lastAlert") #=> "the message"
Since I'd like to avoid a bunch of monkey patches floating around (a common problem in the Watir community), I've added some helper methods as an optional require - after the next release you should be able to do:
require "watir-webdriver/extensions/alerts"
browser.alert do
browser.button(:id => "alert").click
end #=> "the alert message"
browser.confirm(true) do
browser.button(:id => "confirm").click
end #=> "the confirm message"
browser.prompt("returned value") do
browser.button(:id => "prompt").click
end #=> { :message => "foo", :default => "bar" }
Note that this is temporary and the API may be removed in the future when the issue is resolved in WebDriver.
UPDATE:
Proper alert handling is now implemented. The above example would now be done like this:
browser.button(:id => "alert").click
browser.alert.ok
browser.button(:id => "confirm").click
browser.alert.ok # or browser.alert.close
browser.button(:id => "prompt").click
alert = browser.alert
alert.text #=> "foo"
alert.ok

Resources