How can I display the output of an array in Ruby Shoes? - ruby

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

Related

Ruby colorize a section of text

I am writing a command line script in ruby and I am trying to color a section of lines. Currently, I am using the 'colorize' gem, but from the documentation it only lets you color one line of text at a time
puts "test".colorize(:green)
puts "test".colorize(:green)
puts "test".colorize(:green)
But, that seems a bit redundant to me and I would like to color all the lines of text, but only call 'colorize(:green)' once and not 3 times.
How can this be done in Ruby?
Define a method for this:
def putsg(text)
puts text.colorize(:green)
end
And than call that method:
putsg "test"
putsg "test"
putsg "test"
puts ["test", "test", "test"].join($/).colorize(:green)
or
puts ["test", "test", "test"].map{|s| s.colorize(:green)}
Regarding your statement "...but from the documentation it only lets you color one line of text at a time"
You can colorize different parts of the same line with different colors.
s = "Hello"
ss = "world"
puts "#{s.red} #{"there".white} #{s.blue}"
You can also accomplish your aim like this:
s = "test"
puts "#{s}\n#{s}\n#{s}".green
Or:
s1 = "check"
s2 = "this"
s3 = "out"
puts "#{s1}\n#{s2}\n#{s3}".green
You can also define a method like this:
The input is the color you want as a string and the text you want outputed. This will work in the command prompt.
def colorized_text(color,text)
#Find colors here: https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
color_code_hash = {
'red' => 196,
'green' => 40,
'yellow' => 226,
'blue' => 27,
}
puts"\e[38;5;#{color_code_hash[color]}m#{text}\e[0m"
end
I found this to be much better than using the colorize gem since that only gives you 8 colors. You can find all the colors on the ansi escape wiki page under the 8 bit section.

Ruby shoes, para.cursor method

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.

Appending a line of text in a Text_Box in ruby shoes

How do you append a line of text in a Text_Box in ruby shoes? I can see no way of doing this. Currently I am writing to a text file then opening that text file to get newly appended content.
here two ways, one with the initialisation and one after
Shoes.app :width => 300, :height => 450 do
#text = edit_box :width => 1.0, :height => 400, :text =>'test'
#text.text = "test2"
end
You have 7 forms of Text_Box:
banner, a 48 pixel font.
title, a 34 pixel font.
subtitle, a 26 pixel font.
tagline, an 18 pixel font.
caption, a 14 pixel font.
para, a 12 pixel font.
inscription, a 10 pixel font.
To create a Text_Box of 12 pixel font you need to do that:
Shoes.app do
#text_box_example = para "Some text \n"
#To append line:
#text_box_example.replace #text_box_example + "New line of text\n"
end
This is a little late but you can do it like this:
require 'green_shoes'
Shoes.app do
background "#EFC"
flow :width=>'100%', :margin=>10 do
stack do
title "Green shoes append example"
end
#j=edit_box("Data")
stack :width=>150 do
b=button "Click me"
b.click{
#j.text= "#{#j.text} New line of text\n"
}
end
end
end
j is the name of the edit_box.

How can I create a hidden button in Shoes?

In Shoes, I'd like to create a button that is initially hidden. I've tried passing :hidden => true as part of the button style, as well as calling #button.hide() after creating it, but it remains obstinately visible.
I've been able to work around this by not creating the button until I want it shown, but that requires checking to see if it already exists, rather than just using it.
Not at present. Buttons are still fairly unreliable in Shoes, especially on Windows. You can work around the issue by putting the button in a slot and hiding or showing the slot, but you may discover that the button won't hide again once it has been clicked once:
Shoes.app do
para 'This is some text.'
#flow = flow :width => 50, :hidden => true do
button 'a' do |btn|
alert 'Hello, World!'
end
end
button 'toggle' do
#flow.toggle
end
para 'Blah blah blah'
end
Luckily, there is a way out: slots. Slots can be given a click event, which makes them behave much as a button does. In fact, you can make fairly decent buttons out of slots.
Here's something I cobbled together. It lets you use the pesto_button method to generate buttons built on flows. You can modify it to fit your needs, including such things as using an image as the background, modifiable text (with auto-expanding width?), ability to change styles on the fly, etc:
class PestoButton < Widget
def initialize (text, opts = {})
#border_color = opts[:border_color] || gray
#border_width = opts[:border_width] || 3
#color = opts[:up_color] || gainsboro
#click_color = opts[:down_color] || #border_color
#width = opts[:width] || 80
#click = block_given? ? Proc.new { yield } : nil
#text = text
#visible = true
#flow = flow :width => #width do
background #color
border #border_color, :strokewidth => #border_width
para #text, :align => 'center'
end
#flow.click do
#flow.clear
#flow.append do
background #click_color
border #border_color, :strokewidth => #border_width
para #text, :align => 'center'
end
end
#flow.release do
#flow.clear
#flow.append do
background #color
border #border_color, :strokewidth => #border_width
para #text, :align => 'center'
#click.call if #click
end
end
end
def click
#click = block_given? ? Proc.new { yield } : nil
end
def show
#flow.show
end
def toggle
#flow.toggle
end
def hide
#flow.hide
end
end
Shoes.app do
para 'This is some text.'
#btn = pesto_button 'Click me!' do
alert 'Hello, World!'
end
button 'toggle' do
#btn.toggle
end
button 'new block' do
#btn.click do
alert 'Goodbye, World!'
end
end
button 'no block' do
#btn.click #Clears the click method
end
para 'Blah blah blah'
end

Downloading the body of sites using user input

I want to use shoes to be able to download the body of a website that a user enters in an edit_line. How can I make this work? Can anyone help explain why the below code does not work? It just pops up a new window, and does not download the site from the text entered....
Here's my code thus far:
Shoes.app do
stack (:left => 175, :top => 200) do
para "Enter a url:"
flow do
#url = edit_line
button "OK" do
window do
stack do
title "Searching site", :size => 16
#status = para "One moment..."
# Search site for query and print body
download #url.text do |site|
#status.text = "Body: " + site.response.body.inspect
end
end
end
end
end
end
end
Nevermind - I figured it out. I just didn't pop to a new window and it downloads and prints the body fine. : )

Resources