refactor: split FileSystemManager choose_filesystem into simpler pieces
Some checks reported errors
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build was killed

This commit is contained in:
Glenn Y. Rolland 2024-12-11 01:14:18 +01:00
parent 7243935bf9
commit bb7510704f
2 changed files with 46 additions and 22 deletions

View file

@ -8,15 +8,17 @@ require "../file_system_manager"
module GX::Commands module GX::Commands
class MappingMount < AbstractCommand class MappingMount < AbstractCommand
# @file_system_manager : FileSystemManager
def initialize(@config : GX::Config) def initialize(@config : GX::Config)
@config.load_from_env @config.load_from_env
@config.load_from_file @config.load_from_file
@file_system_manager = FileSystemManager.new(@config)
end end
def execute 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? # raise Models::InvalidFilesystemError.new("Invalid filesystem") if filesystem.nil?
# filesystem.mount # filesystem.mount
# @file_system_manager.auto_open(filesystem) if filesystem.mounted? && @config.auto_open? # @file_system_manager.auto_open(filesystem) if filesystem.mounted? && @config.auto_open?

View file

@ -3,7 +3,7 @@
# SPDX-FileCopyrightText: 2024 Glenn Y. Rolland <glenux@glenux.net> # SPDX-FileCopyrightText: 2024 Glenn Y. Rolland <glenux@glenux.net>
# Copyright © 2024 Glenn Y. Rolland <glenux@glenux.net> # Copyright © 2024 Glenn Y. Rolland <glenux@glenux.net>
# require "./models/abstract_filesystem_config" require "./models/abstract_filesystem_config"
require "./utils/fzf" require "./utils/fzf"
module GX module GX
@ -23,7 +23,7 @@ module GX
end end
def auto_open(filesystem) 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: detect mailcap and use it if no xdg-open found
# TODO: support user-defined command in configuration # TODO: support user-defined command in configuration
# TODO: detect graphical environment # TODO: detect graphical environment
@ -31,7 +31,7 @@ module GX
mount_point_safe = filesystem.mount_point mount_point_safe = filesystem.mount_point
raise Models::InvalidMountpointError.new("Invalid filesystem") if mount_point_safe.nil? raise Models::InvalidMountpointError.new("Invalid filesystem") if mount_point_safe.nil?
if graphical_environment? if _graphical_environment?
process = Process.new( process = Process.new(
"xdg-open", # # FIXME: make configurable "xdg-open", # # FIXME: make configurable
[mount_point_safe], [mount_point_safe],
@ -74,24 +74,14 @@ module GX
config_root.filesystems config_root.filesystems
end end
def choose_filesystem : GX::Models::AbstractFilesystemConfig? # Get filesystem by name
names_display = {} of String => NamedTuple( def detect_filesystem(filesystem_name : String) : GX::Models::AbstractFilesystemConfig?
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 end
# Choose filesystem with fzf
def choose_filesystem : GX::Models::AbstractFilesystemConfig?
names_display = _filesystem_table
# FIXME: feat: allow to sort by name or by filesystem # FIXME: feat: allow to sort by name or by filesystem
sorted_values = names_display.values.sort_by!(&.[:filesystem].name) sorted_values = names_display.values.sort_by!(&.[:filesystem].name)
result_filesystem_name = Utils::Fzf.run(sorted_values.map(&.[:ansi_name])).strip result_filesystem_name = Utils::Fzf.run(sorted_values.map(&.[:ansi_name])).strip
@ -117,11 +107,43 @@ module GX
"#{fs_str} #{filesystem.name} #{suffix}".strip "#{fs_str} #{filesystem.name} #{suffix}".strip
end end
private def graphical_environment? private def _graphical_environment?
if ENV["DISPLAY"]? || ENV["WAYLAND_DISPLAY"]? if ENV["DISPLAY"]? || ENV["WAYLAND_DISPLAY"]?
return true return true
end end
false false
end 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
end end