Class: Clipcellar::Command
- Inherits:
-
Thor
- Object
- Thor
- Clipcellar::Command
- Defined in:
- lib/clipcellar/command.rb
Instance Attribute Summary collapse
-
#database_dir ⇒ Object
readonly
Returns the value of attribute database_dir.
Instance Method Summary collapse
- #destroy ⇒ Object
-
#initialize(*args) ⇒ Command
constructor
A new instance of Command.
- #input(*files) ⇒ Object
- #search(required_word, *optional_words) ⇒ Object
- #set(text = nil) ⇒ Object
- #show ⇒ Object
- #version ⇒ Object
- #watch ⇒ Object
Constructor Details
#initialize(*args) ⇒ Command
36 37 38 39 40 |
# File 'lib/clipcellar/command.rb', line 36 def initialize(*args) super @base_dir = File.join(File.("~"), ".clipcellar") @database_dir = File.join(@base_dir, "db") end |
Instance Attribute Details
#database_dir ⇒ Object (readonly)
Returns the value of attribute database_dir.
34 35 36 |
# File 'lib/clipcellar/command.rb', line 34 def database_dir @database_dir end |
Instance Method Details
#destroy ⇒ Object
151 152 153 154 |
# File 'lib/clipcellar/command.rb', line 151 def destroy FileUtils.rm(Dir.glob(File.join(@database_dir, "clipcellar.db*"))) FileUtils.rmdir(@database_dir) end |
#input(*files) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/clipcellar/command.rb', line 56 def input(*files) ARGV.shift text = "" while line = ARGF.gets text << line end Clipboard.set(text) add(text) end |
#search(required_word, *optional_words) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/clipcellar/command.rb', line 115 def search(required_word, *optional_words) words = [required_word] words += optional_words records = [] GroongaDatabase.new.open(@database_dir) do |database| reverse = [:gui] ? true : false sorted_clipboards = GroongaSearcher.search(database, words, :reverse => reverse) sorted_clipboards.each do |clipboard| record = {} record[:key] = clipboard._key record[:text] = clipboard.text record[:time] = clipboard.created_at records << record end end if [:gui] window = Window.new(records) window.run else text = nil records.each do |record| text = record[:text] time = record[:time].strftime("%Y-%m-%d %H:%M:%S") formatted_text = text.gsub(/\n/, " ") if text puts "#{time} #{formatted_text}" end Clipboard.set(text) if text end end |
#set(text = nil) ⇒ Object
48 49 50 51 52 53 |
# File 'lib/clipcellar/command.rb', line 48 def set(text=nil) text = Clipboard.get unless text return unless text Clipboard.set(text) add(text) end |
#show ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/clipcellar/command.rb', line 77 def show records = [] GroongaDatabase.new.open(@database_dir) do |database| database.clipboards.each do |clipboard| record = {} record[:key] = clipboard._key record[:text] = clipboard.text record[:time] = clipboard.created_at records << record end end records.sort_by! do |record| record[:time] end if [:lines] records.reverse! records = records.take([:lines]) records.reverse! end if [:gui] records.reverse! window = Window.new(records) window.run else records.each do |record| text = record[:text] time = record[:time].strftime("%Y-%m-%d %H:%M:%S") formatted_text = text.gsub(/\n/, " ") if text puts "#{time} #{formatted_text}" end end end |
#version ⇒ Object
43 44 45 |
# File 'lib/clipcellar/command.rb', line 43 def version puts Clipcellar::VERSION end |