class Chamomile::FilePicker
Filesystem browser with async directory reading, navigation, and filtering.
Constants
- DEFAULT_KEY_MAP
Attributes
Public Class Methods
Source
# File lib/chamomile/components/file_picker.rb, line 28 def initialize(directory: Dir.pwd, key_map: DEFAULT_KEY_MAP, height: 10, allowed_types: [], show_permissions: false, show_size: false, show_hidden: false, dir_allowed: false, file_allowed: true, cursor_char: ">") @id = self.class.next_id @current_directory = File.expand_path(directory) @key_map = key_map @height = height @allowed_types = allowed_types @show_permissions = show_permissions @show_size = show_size @show_hidden = show_hidden @dir_allowed = dir_allowed @file_allowed = file_allowed @cursor_char = cursor_char @cursor = 0 @entries = [] @offset = 0 @selected_path = nil @disabled_selected_path = nil @dir_stack = [] end
Source
# File lib/chamomile/components/file_picker.rb, line 12 def self.next_id @id_mutex.synchronize do if Process.pid != @id_pid @id_pid = Process.pid @next_id = 0 @id_mutex = Mutex.new end @next_id += 1 "#{@id_pid}-fp-#{@next_id}" end end
Public Instance Methods
Source
# File lib/chamomile/components/file_picker.rb, line 109 def did_select_disabled_file?(msg) return [false, nil] unless msg.is_a?(Chamomile::KeyEvent) if @disabled_selected_path path = @disabled_selected_path @disabled_selected_path = nil [true, path] else [false, nil] end end
Source
# File lib/chamomile/components/file_picker.rb, line 97 def did_select_file?(msg) return [false, nil] unless msg.is_a?(Chamomile::KeyEvent) if @selected_path path = @selected_path @selected_path = nil [true, path] else [false, nil] end end
Source
# File lib/chamomile/components/file_picker.rb, line 55 def handle(msg) case msg when FilePickerReadDirMsg return unless msg.id == @id @entries = msg.entries @cursor = @cursor.clamp(0, [entries_size - 1, 0].max) clamp_offset nil when Chamomile::KeyEvent handle_key(msg) end end
Also aliased as: update
Source
# File lib/chamomile/components/file_picker.rb, line 88 def highlighted_path return nil if @entries.empty? entry = @entries[@cursor] return nil unless entry File.join(@current_directory, entry[:name]) end
Source
# File lib/chamomile/components/file_picker.rb, line 51 def init_cmd read_dir_cmd(@current_directory) end
Source
# File lib/chamomile/components/file_picker.rb, line 71 def view return " (empty)" if @entries.empty? visible = visible_entries lines = visible.each_with_index.map do |entry, i| idx = @offset + i prefix = idx == @cursor ? "#{@cursor_char} " : " " line = prefix + format_entry(entry) if idx == @cursor "\e[7m#{line}\e[0m" else line end end lines.join("\n") end