Class: Clipcellar::Command

Inherits:
Thor
  • Object
show all
Defined in:
lib/clipcellar/command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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.expand_path("~"), ".clipcellar")
  @database_dir = File.join(@base_dir, "db")
end

Instance Attribute Details

#database_dirObject (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

#destroyObject



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 = options[: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 options[: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

#showObject



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 options[:lines]
    records.reverse!
    records = records.take(options[:lines])
    records.reverse!
  end

  if options[: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

#versionObject



43
44
45
# File 'lib/clipcellar/command.rb', line 43

def version
  puts Clipcellar::VERSION
end

#watchObject



67
68
69
70
71
72
# File 'lib/clipcellar/command.rb', line 67

def watch
  $stderr.puts("Watching... (Ctrl+C to Stop)")
  Clipboard.watch do |text|
    add(text)
  end
end