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
-
.generate_sequencial_filenames(prefix, ext, start: nil, num: nil, dir: nil) ⇒ Object
Get file name in the format of
_ . If there are files that matches this format, get the next number of it. -
.get_next_file_number(prefix, ext, dir: nil) ⇒ Object
Get the next file number by searching files with format '
_\d+. ' in . -
.partial_and_unique_match(pattern, candidates) ⇒ Array<String, Fixnum>
Search pattern in candidates.
-
.print_array_in_columns(array, horizon, space, threshold) ⇒ Object
Print array.
Class Method Details
.generate_sequencial_filenames(prefix, ext, start: nil, num: nil, dir: nil) ⇒ Object
Get file name in the format of
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 '
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.
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_in_columns(array, horizon, space, threshold) ⇒ Object
Print array.
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 |