Class: RMTools::FileWatcher
Direct Known Subclasses
Constant Summary collapse
- @@threads =
{}
Instance Attribute Summary collapse
-
#thread ⇒ Object
readonly
Returns the value of attribute thread.
Instance Method Summary collapse
- #files_stats(*stat_params) ⇒ Object
-
#initialize(params = {}) ⇒ FileWatcher
constructor
A new instance of FileWatcher.
- #kill ⇒ Object
- #print_idle_time ⇒ Object
- #print_temp(str, color = nil) ⇒ Object
- #print_time(seconds) ⇒ Object
- #process ⇒ Object
- #puts_temp(str, color = nil) ⇒ Object
-
#select_files ⇒ Object
> [pathname, …] or => [action, …] or => action.
- #temp_string(str, color = nil) ⇒ Object
- #wait ⇒ Object
- #watch(opts = {}) ⇒ Object
- #watch_cycle ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ FileWatcher
Returns a new instance of FileWatcher.
9 10 11 12 13 14 |
# File 'lib/rmtools/dev/watching.rb', line 9 def initialize(params={}) @debug ||= params[:debug] @pwd ||= params[:pwd] || Dir.pwd @thread_name ||= params[:thread_name] || "#{self.class.name.underscore}:#@host" @interval ||= params[:interval] || 1 end |
Instance Attribute Details
#thread ⇒ Object (readonly)
Returns the value of attribute thread.
6 7 8 |
# File 'lib/rmtools/dev/watching.rb', line 6 def thread @thread end |
Instance Method Details
#files_stats(*stat_params) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/rmtools/dev/watching.rb', line 16 def files_stats(*stat_params) opts = {recursive: true, include_dot: true}.merge(stat_params.) Dir(@pwd || Dir.pwd).content(opts).map_hash {|fn| if File.file? fn if stat_params stats = stat_params.map_hash {|param| [param, File.__send__(param, fn)] } else stats = File.stats fn end [fn, stats] end } end |
#kill ⇒ Object
32 33 34 |
# File 'lib/rmtools/dev/watching.rb', line 32 def kill @@threads[@thread_name].kill end |
#print_idle_time ⇒ Object
77 78 79 |
# File 'lib/rmtools/dev/watching.rb', line 77 def print_idle_time print_time(Time.now - @cur_time) end |
#print_temp(str, color = nil) ⇒ Object
46 47 48 |
# File 'lib/rmtools/dev/watching.rb', line 46 def print_temp(str, color=nil) print temp_string str, color end |
#print_time(seconds) ⇒ Object
70 71 72 73 74 75 |
# File 'lib/rmtools/dev/watching.rb', line 70 def print_time(seconds) minutes, seconds = seconds.to_i.divmod 60 hours, minutes = minutes.divmod 60 diff = "#{"#{hours}:" if hours.b}#{"%2d:"%minutes if hours.b or minutes.b}#{"%2d"%seconds}" print_temp(diff, :b_b) end |
#process ⇒ Object
92 93 94 |
# File 'lib/rmtools/dev/watching.rb', line 92 def process raise NotImplementedError, "do something with @files" end |
#puts_temp(str, color = nil) ⇒ Object
50 51 52 |
# File 'lib/rmtools/dev/watching.rb', line 50 def puts_temp(str, color=nil) print_temp(str.to_s+"\n", color) end |
#select_files ⇒ Object
> [pathname, …] or => [action, …] or => action
88 89 90 |
# File 'lib/rmtools/dev/watching.rb', line 88 def select_files {} end |
#temp_string(str, color = nil) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/rmtools/dev/watching.rb', line 36 def temp_string(str, color=nil) # space is needed because cursor is on the left str = " " + str.to_s backspace = "\b"*str.size if color str = Painter.send(color, str) end str << backspace end |
#wait ⇒ Object
96 97 98 |
# File 'lib/rmtools/dev/watching.rb', line 96 def wait sleep @interval end |
#watch(opts = {}) ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/rmtools/dev/watching.rb', line 54 def watch(opts={}) if @@threads[@thread_name] kill end @@threads[@thread_name] = @thread = Thread.new { loop {watch_cycle} } end |
#watch_cycle ⇒ Object
81 82 83 84 85 |
# File 'lib/rmtools/dev/watching.rb', line 81 def watch_cycle @files = select_files process wait end |