Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 12th, 2012  |  syntax: None  |  size: 3.79 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. require "yaml"
  2.  
  3. class Clip < Shoes::Widget
  4.  
  5.   attr_reader :content
  6.  
  7.   def initialize(content=nil)
  8.     @content = content || app.clipboard.strip
  9.  
  10.     stack :margin => 5 do
  11.       @border = border gainsboro
  12.       @background = background lavender, :margin => 1
  13.       @para = para @content
  14.       click do |btn, left, top|
  15.         app.clipboard = @content
  16.         @para.stroke = firebrick
  17.         timer(1) do
  18.           @para.stroke = black
  19.           remove if btn == 2
  20.         end
  21.       end
  22.     end
  23.   end
  24.  
  25. end
  26.  
  27. class Pasty < Shoes::Widget
  28.  
  29.   def initialize
  30.     stack :margin => 15 do
  31.       flow :margin => 5 do
  32.         para link("Clear Pasty") { clear_pasty }, " | ",
  33.           link("Clear Clipboard") { clear_clipboard }, " | ",
  34.           link("Save") { save_clips }, " | ",
  35.           link("Load") { load_clips }, " | ",
  36.           link("Restart") { Shoes.visit __FILE__; close }, :align => "center"
  37.       end
  38.       stack :margin => 5 do
  39.         background aliceblue, :margin => 1
  40.         border lightblue
  41.         @info = para "Ready.", :stroke => mediumblue, :emphasis => "italic"
  42.       end
  43.       flow :margin => 5 do
  44.         @search = edit_line :width => 355
  45.         para link("Search"){ search }, " | ",
  46.           link("Clear"){ clear_search }
  47.       end
  48.       stack :margin_left => 150 do
  49.         button("Paste!", :width => 200, :margin_top => 5) { paste }
  50.       end
  51.       @dropstack = stack
  52.       load_clips
  53.     end
  54.   end
  55.  
  56.   def flash_message(msg, type=nil)
  57.     @info.replace msg
  58.     case type
  59.     when :error then
  60.       colorcode = red
  61.     when :warning then
  62.       colorcode = orange
  63.     when :success then
  64.       colorcode = green
  65.     else
  66.       colorcode = mediumblue
  67.     end
  68.     @info.style :stroke => colorcode
  69.     timer(1) do
  70.       @info.style :stroke => mediumblue
  71.       @info.replace "Ready."
  72.     end
  73.   end
  74.  
  75.   def paste
  76.     begin
  77.       if app.clipboard.to_s.strip != "" then
  78.         @dropstack.contents.delete_if {|p| p.content == app.clipboard.strip }
  79.         @dropstack.prepend do
  80.           clip
  81.         end
  82.         flash_message "Pasted!", :success
  83.       end
  84.     rescue Exception => e
  85.       clear_clipboard
  86.       flash_message "The system clipboard doesn't contain text. Not nice!", :error
  87.     end
  88.   end
  89.  
  90.  
  91.   def clear_search
  92.     @search.text = ""
  93.     flash_message "Search box cleared!", :success
  94.     @dropstack.clear
  95.     @dropstack_contents.each {|c| @dropstack.append { clip c.content } }
  96.   end
  97.  
  98.   def search
  99.     flash_message "Searching...", :success
  100.     regexp = @search.text
  101.     regexp = (regexp.match(/^\/.*\/[a-z]$/)) ? instance_eval(regexp) : Regexp.new(regexp)
  102.     @dropstack_contents = @dropstack.contents.dup
  103.     clips = @dropstack_contents.select{|c| c.content.match regexp}
  104.     flash_message "#{clips.length} matching clips found.", :success
  105.     @dropstack.clear
  106.     clips.each { |c| @dropstack.append{ clip c.content } }
  107.   end
  108.  
  109.   def clear_pasty
  110.     @dropstack.clear
  111.     flash_message "Pasty cleared!", :success
  112.   end
  113.  
  114.   def clear_clipboard
  115.     app.clipboard = ""
  116.     flash_message "Clipboard cleared!", :success
  117.   end
  118.  
  119.   def save_clips
  120.     File.open("pasty.yml", "w+") { |f| f.write @dropstack.contents.map{|c| c.content }.to_yaml}
  121.     flash_message "Clips saved!", :success
  122.   end
  123.  
  124.   def load_clips
  125.     if File.exist? "pasty.yml" then
  126.       clips = YAML.load_file("pasty.yml")
  127.       if clips.empty? || !clips.respond_to?(:each) then
  128.         flash_message "No clips to load...", :warning
  129.       else
  130.         clips.each { |c| @dropstack.append{ clip c } }
  131.         flash_message "#{clips.length} clips loaded!", :success
  132.       end
  133.     else
  134.       flash_message "No clips to load...", :warning
  135.     end
  136.   end
  137.  
  138. end
  139.  
  140. Shoes.app(:title => "Pasty", :resizable => false, :height => 700, :width => 500) do
  141.  
  142.   style Shoes::Para, :size => 10
  143.   style Shoes::Link, :weight => "bold", :underline => false
  144.   style Shoes::LinkHover, :weight => "bold"
  145.  
  146.   @pasty = pasty
  147.  
  148. end