Update 2025-04-16

Update 2025-04-07

Update 2025-04-12

Update 2025-04-16
This commit is contained in:
2025-04-05 13:06:19 +03:00
parent f298468e99
commit 1077cee297
19 changed files with 369 additions and 261 deletions

View File

@@ -4,12 +4,24 @@ A plugin for [yazi](https://github.com/sxyazi/yazi) to calculate the size of the
## Compatibility
what-size supports Yazi on Linux, macOS, and Windows.
### OS
- Linux since first commit
- macOS since commit `42c6a0efb7245badb16781da5380be1a1705f3f2` ([link](https://github.com/pirafrank/what-size.yazi/commit/42c6a0efb7245badb16781da5380be1a1705f3f2))
- Windows since commit `4a56ead2a84c5969791fb17416e0b451ab906c5d` ([link](https://github.com/pirafrank/what-size.yazi/commit/4a56ead2a84c5969791fb17416e0b451ab906c5d))
### Yazi
- yazi `25.x` since commit `fce1778d911621dc57796cdfdf11dcda3c2e28de` ([link](https://github.com/pirafrank/what-size.yazi/commit/fce1778d911621dc57796cdfdf11dcda3c2e28de)).
- yazi `0.4.x` since commit `2780de5aeef1ed16d1973dd6e0cd4d630c900d56` ([link](https://github.com/pirafrank/what-size.yazi/commit/2780de5aeef1ed16d1973dd6e0cd4d630c900d56)).
- yazi `0.3.x` up to commit `f08f7f2d5c94958ac4cb66c51a7c24b4319c6c93` ([link](https://github.com/pirafrank/what-size.yazi/commit/f08f7f2d5c94958ac4cb66c51a7c24b4319c6c93)).
## Requirements
- `du` on Linux. macOS and Windows support is planned.
- `du` on Linux and macOS
- PowerShell on Windows
## Installation
@@ -28,17 +40,44 @@ prepend_keymap = [
]
```
If you want to copy the result to clipboard, you can add `--clipboard` or `-c` as first argument:
If you want to copy the result to clipboard, you can add `--clipboard` or `-c` as 2nd positional argument:
```toml
[manager]
prepend_keymap = [
{ on = [ ".", "s" ], run = "plugin what-size --args='--clipboard'", desc = "Calc size of selection or cwd" },
{ on = [ ".", "s" ], run = "plugin what-size -- '--clipboard'", desc = "Calc size of selection or cwd" },
]
```
```toml
[manager]
prepend_keymap = [
{ on = [ ".", "s" ], run = "plugin what-size -- '-c'", desc = "Calc size of selection or cwd" },
]
```
Change to whatever keybinding you like.
## Feedback
If you have any feedback, suggestions, or ideas please let me know by opening an issue.
## Dev setup
Check the debug config [here](https://yazi-rs.github.io/docs/plugins/overview/#debugging).
To get debug logs while develoing use `ya.dbg()` in your code, then set the `YAZI_LOG` environment variable to `debug` before running Yazi.
```sh
YAZI_LOG=debug yazi
```
Logs will be saved to `~.local/state/yazi/yazi.log` file.
## Contributing
Contributions are welcome. Please fork the repository and submit a PR.
## License
MIT

View File

@@ -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,
}