Class: Charming::Components::Filepicker

Inherits:
Charming::Component show all
Defined in:
lib/charming/presentation/components/filepicker.rb

Overview

Filepicker is a directory browser built on List. Enter descends into the highlighted directory or returns [:selected, absolute_path] for a file; Backspace (or the "../" entry) goes up, never above the configured root. Dotfiles are hidden until toggle_hidden.

Constant Summary collapse

PARENT_ENTRY =
"../"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Charming::Component

#captures_text?

Methods inherited from View

#focused?, #layout_assigns

Constructor Details

#initialize(root: Dir.pwd, show_hidden: false, height: nil, theme: nil, keymap: :vim) ⇒ Filepicker

root is the starting directory and the upper boundary for navigation. show_hidden includes dotfiles from the start. height windows the listing like List's height.



18
19
20
21
22
23
24
25
26
# File 'lib/charming/presentation/components/filepicker.rb', line 18

def initialize(root: Dir.pwd, show_hidden: false, height: nil, theme: nil, keymap: :vim)
  super(theme: theme)
  @root = File.expand_path(root)
  @current_dir = @root
  @show_hidden = show_hidden
  @height = height
  @keymap = keymap
  rebuild_list
end

Instance Attribute Details

#current_dirObject (readonly)

The directory currently being browsed.



13
14
15
# File 'lib/charming/presentation/components/filepicker.rb', line 13

def current_dir
  @current_dir
end

Instance Method Details

#entriesObject

The display entries for the current directory: an optional parent entry, then directories ("name/") before files, alphabetically.



30
31
32
# File 'lib/charming/presentation/components/filepicker.rb', line 30

def entries
  list.items
end

#handle_key(event) ⇒ Object

Handles navigation: Enter descends/selects, Backspace ascends, and all other keys delegate to the underlying List.



36
37
38
39
40
41
42
# File 'lib/charming/presentation/components/filepicker.rb', line 36

def handle_key(event)
  case Charming.key_of(event)
  when :enter then activate(list.selected_item)
  when :backspace then ascend
  else list.handle_key(event)
  end
end

#renderObject

Renders the current directory's listing via the underlying List.



52
53
54
# File 'lib/charming/presentation/components/filepicker.rb', line 52

def render
  list.render
end

#toggle_hiddenObject

Shows or hides dotfiles, refreshing the listing. Returns self.



45
46
47
48
49
# File 'lib/charming/presentation/components/filepicker.rb', line 45

def toggle_hidden
  @show_hidden = !@show_hidden
  rebuild_list
  self
end