Class: RMTools::FileWatcher

Inherits:
Object show all
Defined in:
lib/rmtools/dev/watching.rb

Direct Known Subclasses

ScpHelper

Constant Summary collapse

@@threads =
{}

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#threadObject (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
# File 'lib/rmtools/dev/watching.rb', line 16

def files_stats(*stat_params)
  Dir(@pwd || Dir.pwd).recursive_content.flatten.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

#killObject



31
32
33
# File 'lib/rmtools/dev/watching.rb', line 31

def kill
  @@threads[@thread_name].kill
end


76
77
78
# File 'lib/rmtools/dev/watching.rb', line 76

def print_idle_time
  print_time(Time.now - @cur_time)
end


45
46
47
# File 'lib/rmtools/dev/watching.rb', line 45

def print_temp(str, color=nil)
  print temp_string str, color
end

Памятка про printf для чисел: precision = минимальное число цифр;

f -> 

len = минимальная длина строки; если начинается с 0, заполняется нулями, иначе пробелами || 0 “%[<len>.]d” “%[<len>]f”



69
70
71
72
73
74
# File 'lib/rmtools/dev/watching.rb', line 69

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

#processObject

Raises:

  • (NotImplementedError)


91
92
93
# File 'lib/rmtools/dev/watching.rb', line 91

def process
  raise NotImplementedError, "do something with @files"
end

#puts_temp(str, color = nil) ⇒ Object



49
50
51
# File 'lib/rmtools/dev/watching.rb', line 49

def puts_temp(str, color=nil)
  print_temp(str.to_s+"\n", color)
end

#select_filesObject

> [pathname, …] or => [action, …] or => action



87
88
89
# File 'lib/rmtools/dev/watching.rb', line 87

def select_files
  {}
end

#temp_string(str, color = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/rmtools/dev/watching.rb', line 35

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

#waitObject



95
96
97
# File 'lib/rmtools/dev/watching.rb', line 95

def wait
  sleep @interval
end

#watch(opts = {}) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/rmtools/dev/watching.rb', line 53

def watch(opts={})
  if @@threads[@thread_name]
    kill
  end
  @@threads[@thread_name] = @thread = Thread.new {
    loop {watch_cycle}
  }
end

#watch_cycleObject



80
81
82
83
84
# File 'lib/rmtools/dev/watching.rb', line 80

def watch_cycle
  @files = select_files
  process
  wait
end