mirror of
https://github.com/kristoferssolo/solorice.git
synced 2026-02-04 06:32:03 +00:00
Update 2025-04-16
Update 2025-04-07 Update 2025-04-12 Update 2025-04-16
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
|
||||
-- function to get paths of selected elements or current directory
|
||||
-- of no elements are selected
|
||||
-- if no elements are selected
|
||||
local get_paths = ya.sync(function()
|
||||
local paths = {}
|
||||
-- get selected files
|
||||
@@ -18,55 +18,79 @@ local get_paths = ya.sync(function()
|
||||
return paths
|
||||
end)
|
||||
|
||||
-- Function to get total size from du output
|
||||
local get_total_size = function(s)
|
||||
local lines = {}
|
||||
for line in s:gmatch("[^\n]+") do lines[#lines + 1] = line end
|
||||
local last_line = lines[#lines]
|
||||
local last_line_parts = {}
|
||||
for part in last_line:gmatch("%S+") do last_line_parts[#last_line_parts + 1] = part end
|
||||
local total_size = last_line_parts[1]
|
||||
return total_size
|
||||
-- Function to get total size from output
|
||||
-- Unix use `du`, Windows use PowerShell
|
||||
local function get_total_size(items)
|
||||
local is_windows = package.config:sub(1,1) == '\\'
|
||||
|
||||
if is_windows then
|
||||
local total = 0
|
||||
for _, path in ipairs(items) do
|
||||
path = path:gsub('"', '\\"')
|
||||
local ps_cmd = string.format(
|
||||
[[powershell -Command "& { $p = '%s'; if (Test-Path $p) { if ((Get-ChildItem -Path $p -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object Length -Sum).Sum) { (Get-ChildItem -Path $p -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object Length -Sum).Sum } else { (Get-Item $p).Length } } }"]],
|
||||
path
|
||||
)
|
||||
local pipe = io.popen(ps_cmd)
|
||||
local result = pipe:read("*a")
|
||||
-- Debug
|
||||
-- ya.notify {
|
||||
-- title = "Debug Output",
|
||||
-- content = result,
|
||||
-- timeout = 5,
|
||||
-- }
|
||||
pipe:close()
|
||||
local num = tonumber(result)
|
||||
if num then total = total + num end
|
||||
end
|
||||
return total
|
||||
else
|
||||
local arg = ya.target_os() == "macos" and "-scA" or "-scb"
|
||||
local output, err = Command("du"):arg(arg):args(items):output()
|
||||
if not output then
|
||||
ya.err("Failed to run du: " .. err)
|
||||
end
|
||||
local lines = {}
|
||||
for line in output.stdout:gmatch("[^\n]+") do
|
||||
lines[#lines + 1] = line
|
||||
end
|
||||
local last_line = lines[#lines]
|
||||
local size = tonumber(last_line:match("^(%d+)"))
|
||||
return ya.target_os() == "macos" and size * 512 or size
|
||||
end
|
||||
end
|
||||
|
||||
-- Function to format file size
|
||||
local function format_size(size)
|
||||
local units = { "B", "KB", "MB", "GB", "TB" }
|
||||
local unit_index = 1
|
||||
|
||||
while size > 1024 and unit_index < #units do
|
||||
size = size / 1024
|
||||
unit_index = unit_index + 1
|
||||
end
|
||||
|
||||
return string.format("%.2f %s", size, units[unit_index])
|
||||
end
|
||||
|
||||
return {
|
||||
entry = function(self, job)
|
||||
-- as per doc: https://yazi-rs.github.io/docs/plugins/overview#functional-plugin
|
||||
entry = function(_, job)
|
||||
-- defaults not to use clipboard, use it only if required by the user
|
||||
local clipboard = job.args.clipboard or job.args[1] == '-c'
|
||||
local clipboard = job.args.clipboard == true or job.args[1] == "--clipboard" or job.args[1] == "-c"
|
||||
local items = get_paths()
|
||||
|
||||
local cmd = "du"
|
||||
local output, err = Command(cmd):arg("-scb"):args(items):output()
|
||||
if not output then
|
||||
ya.err("Failed to run diff, error: " .. err)
|
||||
else
|
||||
local total_size = get_total_size(output.stdout)
|
||||
local formatted_size = format_size(tonumber(total_size))
|
||||
local total_size = get_total_size(items)
|
||||
local formatted_size = format_size(total_size)
|
||||
|
||||
local notification_content = "Total size: " .. formatted_size
|
||||
if clipboard then
|
||||
ya.clipboard(formatted_size)
|
||||
notification_content = notification_content .. "\nCopied to clipboard."
|
||||
end
|
||||
|
||||
ya.notify {
|
||||
title = "What size",
|
||||
content = notification_content,
|
||||
timeout = 5,
|
||||
}
|
||||
local notification_content = "Total size: " .. formatted_size
|
||||
if clipboard then
|
||||
ya.clipboard(formatted_size)
|
||||
notification_content = notification_content .. "\nCopied to clipboard."
|
||||
end
|
||||
|
||||
ya.notify {
|
||||
title = "What size",
|
||||
content = notification_content,
|
||||
timeout = 4,
|
||||
}
|
||||
end,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user