Module: SonyCameraRemoteAPI::Utils

Included in:
Camera, CameraAPIGroupManager, Client::Main, Client::ShelfCmd, Shelf
Defined in:
lib/sony_camera_remote_api/utils.rb

Overview

Module providing utility methods

Class Method Summary collapse

Class Method Details

.generate_sequencial_filenames(prefix, ext, start: nil, num: nil, dir: nil) ⇒ Object

Get file name in the format of <prefix>_<num>.<ext> If there are files that matches this format, get the next number of it.

Parameters:

  • prefix (String)
  • ext (String)
  • start (Fixnum)
  • num (Fixnum)
  • dir (String)

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sony_camera_remote_api/utils.rb', line 14

def generate_sequencial_filenames(prefix, ext, start: nil, num: nil, dir: nil)
  if start
    count = start
  else
    count = get_next_file_number prefix, ext, dir: dir
  end
  gen = Enumerator.new do |y|
    loop do
      y << "#{prefix}_#{count}.#{ext}"
      count += 1
    end
  end
  if num
    return (0..(num-1)).map { gen.next }
  else
    return gen
  end
end

.get_next_file_number(prefix, ext, dir: nil) ⇒ Object

Get the next file number by searching files with format '<prefix>_d+.<ext>' in <dir>.

Parameters:

  • prefix (String)
  • ext (String)
  • dir (String)

38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/sony_camera_remote_api/utils.rb', line 38

def get_next_file_number(prefix, ext, dir: nil)
  numbers = []
  Dir["#{dir}/*"].map do |f|
    begin
      num = f[/#{dir}\/#{prefix}_(\d+).#{ext}/, 1]
      numbers << num.to_i if num.present?
    rescue
      nil
    end
  end
  if numbers.empty?
    0
  else
    numbers.sort[-1] + 1
  end
end

.partial_and_unique_match(pattern, candidates) ⇒ Array<String, Fixnum>

Search pattern in candidates.

Parameters:

  • pattern (String)

    Pattern

  • candidates (Array<String>)

    Candidates

Returns:

  • (Array<String, Fixnum>)

    matched candidate and the number of matched candidates.


60
61
62
63
64
65
66
67
68
# File 'lib/sony_camera_remote_api/utils.rb', line 60

def partial_and_unique_match(pattern, candidates)
  result = candidates.find { |c| c == pattern }
  return result, 1 if result
  result = candidates.select { |c| c =~ /#{pattern}/i }
  return result[0], 1 if result.size == 1
  result = candidates.select { |c| c =~ /#{pattern}/ }
  return result[0], 1 if result.size == 1
  return nil, result.size
end

Print array.

Parameters:

  • array (Array)
  • horizon (Fixnum)
  • space (Fixnum)
  • threshold (Fixnum)

76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/sony_camera_remote_api/utils.rb', line 76

def print_array_in_columns(array, horizon, space, threshold)
  if array.size >= threshold
    longest = array.map { |s| s.size }.max
    num_columns = (horizon + space ) / (longest + space)
    num_rows = array.size / num_columns + 1
    array += [''] * ((num_columns * num_rows) - array.size)
    array_2d = array.each_slice(num_rows).to_a
    longests = array_2d.map { |column| column.map { |e| e.size }.max }
    array_2d = array_2d.transpose
    array_2d.each do |row|
      row.zip(longests).each do |e, len|
        print e + ' ' * (len - e.size) + ' ' * space
      end
      puts ''
    end
  else
    array.each do |e|
        puts e
    end
  end
end