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

def files_stats(*stat_params)
  opts = {recursive: true, include_dot: true}.merge(stat_params.extract_options!)
  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

#killObject



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

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


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

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


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

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

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

%f -> справа || 6
%d -> слева, заполняется нулями || 1

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



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

#processObject

Raises:

  • (NotImplementedError)


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_filesObject

> [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

#waitObject



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_cycleObject



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

def watch_cycle
  @files = select_files
  process
  wait
end