Class: FileBrowserModel

Inherits:
Object
  • Object
show all
Defined in:
lib/terminal-file-picker/file_browser_model.rb

Overview

Functions related to retrieving and formatting information related to directories and their contents.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(starting_path, options = {}) ⇒ FileBrowserModel

Returns a new instance of FileBrowserModel.



6
7
8
9
10
11
12
# File 'lib/terminal-file-picker/file_browser_model.rb', line 6

def initialize(starting_path, options = {})
  @options = options
  @current_path = starting_path
  @page = 0
  @selected = 0
  @files = order_files(files_in_dir)
end

Instance Attribute Details

#current_pathObject

Returns the value of attribute current_path.



4
5
6
# File 'lib/terminal-file-picker/file_browser_model.rb', line 4

def current_path
  @current_path
end

#filesObject

Returns the value of attribute files.



4
5
6
# File 'lib/terminal-file-picker/file_browser_model.rb', line 4

def files
  @files
end

#pageObject

Returns the value of attribute page.



4
5
6
# File 'lib/terminal-file-picker/file_browser_model.rb', line 4

def page
  @page
end

#selectedObject

Returns the value of attribute selected.



4
5
6
# File 'lib/terminal-file-picker/file_browser_model.rb', line 4

def selected
  @selected
end

Instance Method Details

#files_in_dirObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/terminal-file-picker/file_browser_model.rb', line 14

def files_in_dir
  date_format = @options.fetch(:date_format, '%d/%m/%Y')
  time_format = @options.fetch(:time_format, '%H:%M')

  Dir.entries(@current_path).map do |f|
    file_path = File.join(@current_path, f)

    name = add_indicator(f)

    size_bytes = File.size(file_path)

    mtime = File.mtime(file_path)
    date_mod = mtime.strftime(date_format)
    time_mod = mtime.strftime(time_format)

    [name, size_bytes, date_mod, time_mod]
  end
end

#order_files(files) ⇒ Object

Order files such that ‘.’ and ‘..’ come before all the other files.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/terminal-file-picker/file_browser_model.rb', line 35

def order_files(files)
  # Pre-sort files.
  files.sort!{|a,b| a[0]<=>b[0]}
  # Put "." and ".." at the start
  groups = files.group_by do |f|
    if f.first == './' || f.first == '../'
      :dots
    else
      :files
    end
  end

  # Sort so that "." comes before ".."
  (groups[:dots] || []).sort.reverse + (groups[:files] || [])
end

#path_rel_to_start(file_name) ⇒ Object



59
60
61
# File 'lib/terminal-file-picker/file_browser_model.rb', line 59

def path_rel_to_start(file_name)
  File.join(@current_path, file_name)
end

#selected_absolute_pathObject

Absolute path of the currently selected file



52
53
54
55
56
57
# File 'lib/terminal-file-picker/file_browser_model.rb', line 52

def selected_absolute_path
  selected_file_name = @files[@selected].first
  # This may not be the absolute path (e.g. file_name may be '.')
  selected_full_path = File.join(@current_path, selected_file_name)
  File.absolute_path(selected_full_path)
end