librelist archives

« back to archive

duplicate hot-key verifier

duplicate hot-key verifier

From:
Sebastjan Hribar
Date:
2013-05-07 @ 07:30
Hi,

I am attending a forum next week where I have a chance to present some 
tools I've made and this is a great opportunity to promote ruby and 
shoes. A while back I've written a small app to help me verify the 
hot-key assignments in computer programs. I've done some refactoring for 
the purpose of the demonstration and I've come across a few issues. I am 
pressed for time and would be very grateful if someone could take a look.

Code:

-----------------------------------------------------------------------------------------------------
require 'nkf'
require 'green_shoes'
Shoes.app :title => "Duplicate hotkey verifier", margin: 10, width: 1000 do
flow do
background yellow
   @main_frame = flow margin: 10, width: 1000 do
       caption "Duplicate hotkey verifier", margin: 10, :align => "center"
       background yellow

       @info = flow do
         para "Instructions"
         para "To get the list of available letters to be used as 
hotkeys pres the icon to add a file exported from the Catalyst. The 
available letters will be written at the end of the exported file and 
also printed on the screen."
       end #@info end

       image "file-add-icon.png", margin: 10, width: 50, height: 50, 
:click => proc {
         @file = ask_open_file
         @strings = []
         @duplicates = []
         @used_chars = []

         all = ["&a", "&b", "&c", "&d", "&e", "&f", "&g", "&h", "&i", 
"&j", "&k",
               "&l", "&m", "&n", "&o", "&p", "&r", "&s", "&t", "&u", "&v",
               "&w", "&x", "&y", "&z"]

         data=nil
         open(@file, "rb:UTF-16LE") {|fin| data=fin.read() }
         open(@file, "wb:UTF-8"){ |fout| fout.write(data) } if data

         input_file = File.open(@file, "a+")
         input_file.each_line {|line| @strings << line}

         @strings.map!{|c| c.downcase.strip}
         @strings.each {|str| str.gsub!(/.+\t/, "")}

         @strings.each do |str|
           char = str[/&[a-z]/]
           @duplicates << char if @used_chars.include? char
           @used_chars << char
         end

         @unused = [(all)-(@used_chars)]

         @unused.first.map{|c| c.gsub!("&", "")}

         input_file.write("\nHere are the unused characters:\n")
         @unused.each {|char2| input_file.write(char2)}
         input_file.close()

         @results.append do
           flow do
           para "\nHere are the unused characters for the file #{@file}:\n"
               @unused.each_with_index do |c, i|
                 box = []
                 box[i] = stack margin: 3, height: 10, width: 10 do
                 button "#{c}" do
                   box[i].clear
                 end
                 end
               end
           end
         end
       }
   end # @main_frame end

   @results = flow margin: 10, width: 1000 do
     background pink
     caption "Results:"
   end # @results end
end
end # App end
----------------------------------------------------------------------

Issues:

1. I can't get rid of the white space in the bottom, which is odd since 
I don't have heights specified and I have a high level flow with 
background defined which usually solves this issues when using green shoes.

2. If I press cancel when adding the file the app crashes. How might I 
fix this?

3. When I try to implement a single button per unused character I get a 
button which holds all characters??? There should be a button in the 
results slot for every single character with executable block for 
clearing the button and hence updating the available letters list.

It seems to me as if the @unused array is considered to only have 1 
element. Is this because of the "first.map"?

Otherwise the letters are written to the file with no errors.

Please find attached a file to test this on.

love shoes!
seba

Re: [shoes] duplicate hot-key verifier

From:
ashbb
Date:
2013-05-07 @ 12:31
Hi Seba,

> I am attending a forum next week where I have a chance to present some
tools
> I've made and this is a great opportunity to promote ruby and shoes.
Wow, cool!

#1 You are not specifying height size of window.
   Hence Green Shoes uses default size: 500.

#2 Add `if @file ... end` like this:

      image "cy.png", margin: 10, width: 50, height: 50, :click => proc {
        @file = ask_open_file
        if @file
          # bla  bla bla
        end
      }

#3 You are not replacing the value of @unused. Try out the following.

      @unused = @unused.first.map{|c| c.gsub!("&", "")}

Hope this helps,
ashbb

Re: [shoes] duplicate hot-key verifier

From:
Sebastjan Hribar
Date:
2013-05-07 @ 19:26
Dne 07. 05. 2013 14:31, piše ashbb:
> Hi Seba,
>
> > I am attending a forum next week where I have a chance to present 
> some tools
> > I've made and this is a great opportunity to promote ruby and shoes.
> Wow, cool!
>
> #1 You are not specifying height size of window.
>    Hence Green Shoes uses default size: 500.
Thank you. number 2 and 3 work great:)

The white space is still there though even if I specify window height 
and if I wrap everything in a high level flow.
White space is removed only if I fix the results height, but that limits 
the display so that's not an option.

Anyway, I've made it a bit prettier and it works fine now with buttons 
getting generated for each unused letter:


------------------------------------------------------------------------------------------------
require 'nkf'
require 'green_shoes'
Shoes.app :title => "Duplicate hotkey verifier", margin: 10, width: 800, 
height: 800 do
   @main_frame = flow margin: 10, width: 800, height: 300 do

       caption "Duplicate hotkey verifier", margin: 10, :align => "center"
       background cornflowerblue

         caption "Instructions", :underline => "double"
         para "To get the list of available letters to be used as 
hotkeys press the icon to add a file exported from the Catalyst. The 
available letters will be written at the end of the exported file and 
also printed on the screen."

       image "file-add-icon.png", margin: 10, width: 60, height: 60, 
:click => proc {
         @file = ask_open_file
         if @file
             @strings = []
             @duplicates = []
             @used_chars = []

             all = ["&a", "&b", "&c", "&d", "&e", "&f", "&g", "&h", 
"&i", "&j", "&k",
                   "&l", "&m", "&n", "&o", "&p", "&r", "&s", "&t", "&u", 
"&v",
                   "&w", "&x", "&y", "&z"]

             data=nil
             open(@file, "rb:UTF-16LE") {|fin| data=fin.read() }
             open(@file, "wb:UTF-8"){ |fout| fout.write(data) } if data

             input_file = File.open(@file, "a+")
             input_file.each_line {|line| @strings << line}

             @strings.map!{|c| c.downcase.strip}
             @strings.each {|str| str.gsub!(/.+\t/, "")}

             @strings.each do |str|
               char = str[/&[a-z]/]
               @duplicates << char if @used_chars.include? char
               @used_chars << char
             end

             @unused = [(all)-(@used_chars)]
             @unused = @unused.first.map{|c| c.gsub!("&", "")}

             input_file.write("\nHere are the unused characters:\n")
             @unused.each {|char2| input_file.write(char2)}
             input_file.close()

             @results.append do
             flow do
               para "\nHere are the unused characters for the file 
#{@file}:\n"
                   @unused.each_with_index do |c, i|
                     box = []
                     box[i] = stack height: 40, width: 40 do
                       button "#{c}", height: 40, width: 40 do
                         box[i].clear
                       end
                     end
                   end
             end
             end
         end
       }
   end # @main_frame end

   @results = flow margin: 10, width: 800 do
     background aliceblue
     caption "Results:", :underline => "double"
   end # @results end
end # App end

------------------------------------------------------------------------------------------------

thank you for your help!
love shoes!
seba

Re: [shoes] duplicate hot-key verifier

From:
ashbb
Date:
2013-05-08 @ 13:19
Hi Seba,

#1 Ah, sorry. I misunderstood what you meant to say.
   Try out decimal fraction style like this. ;-)

require 'green_shoes'
Shoes.app do
  flow height: 0.3 do
    background green
  end
  flow height: 0.7 do
    background yellow
  end
end

Regards,
ashbb