From bb7510704f61ea7fd92407cec4f53b837339410f Mon Sep 17 00:00:00 2001 From: "Glenn Y. Rolland" Date: Wed, 11 Dec 2024 01:14:18 +0100 Subject: [PATCH] refactor: split FileSystemManager choose_filesystem into simpler pieces --- src/commands/mapping_mount.cr | 6 ++-- src/file_system_manager.cr | 62 ++++++++++++++++++++++++----------- 2 files changed, 46 insertions(+), 22 deletions(-) diff --git a/src/commands/mapping_mount.cr b/src/commands/mapping_mount.cr index d94f966..e8ab080 100644 --- a/src/commands/mapping_mount.cr +++ b/src/commands/mapping_mount.cr @@ -8,15 +8,17 @@ require "../file_system_manager" module GX::Commands class MappingMount < AbstractCommand - # @file_system_manager : FileSystemManager def initialize(@config : GX::Config) @config.load_from_env @config.load_from_file + @file_system_manager = FileSystemManager.new(@config) end def execute - # filesystem = @config.root.try &.file_system_manager.choose_filesystem + # get filesystem from config options + # filesystem = @config.mapping_mount_options.filesystem + # raise Models::InvalidFilesystemError.new("Invalid filesystem") if filesystem.nil? # filesystem.mount # @file_system_manager.auto_open(filesystem) if filesystem.mounted? && @config.auto_open? diff --git a/src/file_system_manager.cr b/src/file_system_manager.cr index e8bf6d9..a0d8093 100644 --- a/src/file_system_manager.cr +++ b/src/file_system_manager.cr @@ -3,7 +3,7 @@ # SPDX-FileCopyrightText: 2024 Glenn Y. Rolland # Copyright © 2024 Glenn Y. Rolland -# require "./models/abstract_filesystem_config" +require "./models/abstract_filesystem_config" require "./utils/fzf" module GX @@ -23,7 +23,7 @@ module GX end def auto_open(filesystem) - # TODO: detect xdg-open and use it if possible + # TODO: detect xdg-open presence and use it if possible # TODO: detect mailcap and use it if no xdg-open found # TODO: support user-defined command in configuration # TODO: detect graphical environment @@ -31,7 +31,7 @@ module GX mount_point_safe = filesystem.mount_point raise Models::InvalidMountpointError.new("Invalid filesystem") if mount_point_safe.nil? - if graphical_environment? + if _graphical_environment? process = Process.new( "xdg-open", # # FIXME: make configurable [mount_point_safe], @@ -74,23 +74,13 @@ module GX config_root.filesystems end + # Get filesystem by name + def detect_filesystem(filesystem_name : String) : GX::Models::AbstractFilesystemConfig? + end + + # Choose filesystem with fzf def choose_filesystem : GX::Models::AbstractFilesystemConfig? - names_display = {} of String => NamedTuple( - filesystem: Models::AbstractFilesystemConfig, - ansi_name: String) - - config_root = @config.root - return if config_root.nil? - - config_root.filesystems.each do |filesystem| - result_name = _fzf_plain_name(filesystem) - ansi_name = _fzf_ansi_name(filesystem) - - names_display[result_name] = { - filesystem: filesystem, - ansi_name: ansi_name, - } - end + names_display = _filesystem_table # FIXME: feat: allow to sort by name or by filesystem sorted_values = names_display.values.sort_by!(&.[:filesystem].name) @@ -117,11 +107,43 @@ module GX "#{fs_str} #{filesystem.name} #{suffix}".strip end - private def graphical_environment? + private def _graphical_environment? if ENV["DISPLAY"]? || ENV["WAYLAND_DISPLAY"]? return true end false end + + + alias FilesystemTableItem = + NamedTuple( + filesystem: Models::AbstractFilesystemConfig, + ansi_name: String + ) + + alias FilesystemTable = + Hash( + String, + FilesystemTableItem + ) + + private def _filesystem_table : FilesystemTable + names_display = {} of String => FilesystemTableItem + + config_root = @config.root + return {} of String => FilesystemTableItem if config_root.nil? + + config_root.filesystems.each do |filesystem| + result_name = _fzf_plain_name(filesystem) + ansi_name = _fzf_ansi_name(filesystem) + + names_display[result_name] = { + filesystem: filesystem, + ansi_name: ansi_name, + } + end + + names_display + end end end