Class: ClipboardMonitor::ClipboardInternal

Inherits:
Object
  • Object
show all
Defined in:
lib/clipboard_monitor/clipboard_internal.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClipboardInternal

Returns a new instance of ClipboardInternal.



8
9
10
11
12
# File 'lib/clipboard_monitor/clipboard_internal.rb', line 8

def initialize
  self.last_text = clipboard_text
  self.emitter = Emittr::Emitter.new
  self.scheduler = Rufus::Scheduler.new(frequency: '.2s')
end

Instance Attribute Details

#emitterObject

Returns the value of attribute emitter.



7
8
9
# File 'lib/clipboard_monitor/clipboard_internal.rb', line 7

def emitter
  @emitter
end

#last_textObject

Returns the value of attribute last_text.



7
8
9
# File 'lib/clipboard_monitor/clipboard_internal.rb', line 7

def last_text
  @last_text
end

#schedulerObject

Returns the value of attribute scheduler.



7
8
9
# File 'lib/clipboard_monitor/clipboard_internal.rb', line 7

def scheduler
  @scheduler
end

Instance Method Details

#check_text_changeObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/clipboard_monitor/clipboard_internal.rb', line 19

def check_text_change
  current_text = clipboard_text
  if current_text == last_text
    return false
  else
    puts "Change"
    self.last_text = current_text
    return current_text
  end
end

#clipboard_textObject



13
14
15
# File 'lib/clipboard_monitor/clipboard_internal.rb', line 13

def clipboard_text
  return Clipboard.paste.strip
end

#start_monitoringObject



35
36
37
38
39
40
41
# File 'lib/clipboard_monitor/clipboard_internal.rb', line 35

def start_monitoring
  scheduler.every '.2s', overlap: false do
    result = check_text_change
    emitter.emit :clipboard_text_changed, result if result
  end
  scheduler.join
end

#stop_monitoringObject



16
17
18
# File 'lib/clipboard_monitor/clipboard_internal.rb', line 16

def stop_monitoring
  scheduler.shutdown
end

#watch_for_text(&block) ⇒ Object



29
30
31
32
33
34
# File 'lib/clipboard_monitor/clipboard_internal.rb', line 29

def watch_for_text(&block)
  emitter.on :clipboard_text_changed do |contents|
    block.call(contents)
  end
  start_monitoring
end