Skip to content

Commit

Permalink
Feat: Get ready for Yazi v0.4's handling of mime types
Browse files Browse the repository at this point in the history
Changes:
- Create another function called is_archive_mime_type that removes the
  "x-" prefix from the mime type before checking against the list of
  archive mime types.
- The hovered_item_is_archive function now uses the aforementioned
  function to check for the archive mime type.
- Similarly, the is_archive_file function also now uses the
  aforementioned function to check for the archive mime type.
  • Loading branch information
hankertrix committed Oct 1, 2024
1 parent 1b9327d commit b81c325
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,22 @@ local INPUT_OPTIONS_TABLE = {
local ARCHIVE_MIME_TYPES = {
"application/zip",
"application/gzip",
"application/x-tar",
"application/x-bzip",
"application/x-bzip2",
"application/x-7z-compressed",
"application/x-rar",
"application/x-xz",
"application/tar",
"application/bzip",
"application/bzip2",
"application/7z-compressed",
"application/rar",
"application/xz",
}

-- The pattern to get the double dash from the front of the argument
---@type string
local double_dash_pattern = "^%-%-"

-- The pattern to get the mime type without the "x-" prefix
---@type string
local get_mime_type_without_x_prefix_pattern = "^(%a-)/x%-([%-%d%a]-)$"

-- The pattern to get the information from an archive item
---@type string
local archive_item_info_pattern = "%s+([%.%a]+)%s+(%d+)%s+(%d+)%s+(.+)$"
Expand Down Expand Up @@ -418,6 +422,25 @@ local function initialise_plugin(opts)
return config
end

-- Function to check if a given mime type is an archive
---@param mime_type string The mime type of the file
---@return boolean is_archive Whether the mime type is an archive
local function is_archive_mime_type(mime_type)
--

-- Trim the whitespace from the mime type
mime_type = string_trim(mime_type)

-- Remove the "x-" prefix from the mime type
mime_type = mime_type:gsub(get_mime_type_without_x_prefix_pattern, "%1/%2")

-- Get if the mime type is an archive
local is_archive = list_contains(ARCHIVE_MIME_TYPES, mime_type)

-- Return if the mime type is an archive
return is_archive
end

-- Function to get the configuration from an async function
---@param state any
---@return Configuration
Expand Down Expand Up @@ -483,7 +506,7 @@ local hovered_item_is_archive = ya.sync(function(_)

-- Return if the hovered item exists and is an archive
return hovered_item
and list_contains(ARCHIVE_MIME_TYPES, hovered_item:mime())
and is_archive_mime_type(hovered_item:mime())
end)

-- Function to get the paths of the selected items
Expand Down Expand Up @@ -1238,7 +1261,7 @@ local function is_archive_file(file_path)
local mime_type = get_mime_type(file_path)

-- Set the is archive variable
is_archive = list_contains(ARCHIVE_MIME_TYPES, mime_type)
is_archive = is_archive_mime_type(mime_type)

-- Return the is archive variable
return is_archive
Expand Down

0 comments on commit b81c325

Please sign in to comment.