Add files via upload
This commit is contained in:
21
config/yazi/plugins/what-size.yazi/LICENSE
Normal file
21
config/yazi/plugins/what-size.yazi/LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2024 Francesco Pira
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
44
config/yazi/plugins/what-size.yazi/README.md
Normal file
44
config/yazi/plugins/what-size.yazi/README.md
Normal file
@@ -0,0 +1,44 @@
|
||||
# what-size.yazi
|
||||
|
||||
A plugin for [yazi](https://github.com/sxyazi/yazi) to calculate the size of the current selection or the current working directory (if no selection is made).
|
||||
|
||||
## Compatibility
|
||||
|
||||
- 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.
|
||||
|
||||
## Installation
|
||||
|
||||
```sh
|
||||
ya pack -a 'pirafrank/what-size'
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Add this to your `~/.config/yazi/keymap.toml`:
|
||||
|
||||
```toml
|
||||
[manager]
|
||||
prepend_keymap = [
|
||||
{ on = [ ".", "s" ], run = "plugin what-size", desc = "Calc size of selection or cwd" },
|
||||
]
|
||||
```
|
||||
|
||||
If you want to copy the result to clipboard, you can add `--clipboard` or `-c` as first argument:
|
||||
|
||||
```toml
|
||||
[manager]
|
||||
prepend_keymap = [
|
||||
{ on = [ ".", "s" ], run = "plugin what-size --args='--clipboard'", desc = "Calc size of selection or cwd" },
|
||||
]
|
||||
```
|
||||
|
||||
Change to whatever keybinding you like.
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
75
config/yazi/plugins/what-size.yazi/main.lua
Normal file
75
config/yazi/plugins/what-size.yazi/main.lua
Normal file
@@ -0,0 +1,75 @@
|
||||
-- function to get paths of selected elements or current directory
|
||||
-- of no elements are selected
|
||||
local get_paths = ya.sync(function()
|
||||
local paths = {}
|
||||
-- get selected files
|
||||
for _, u in pairs(cx.active.selected) do
|
||||
paths[#paths + 1] = tostring(u)
|
||||
end
|
||||
-- if no files are selected, get current directory
|
||||
if #paths == 0 then
|
||||
if cx.active.current.cwd then
|
||||
paths[1] = tostring(cx.active.current.cwd)
|
||||
else
|
||||
ya.err("what-size would return nil paths")
|
||||
end
|
||||
end
|
||||
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
|
||||
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)
|
||||
-- defaults not to use clipboard, use it only if required by the user
|
||||
local clipboard = job.args.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 notification_content = "Total size: " .. formatted_size
|
||||
if clipboard then
|
||||
ya.clipboard(formatted_size)
|
||||
notification_content = notification_content .. "\nCopied to clipboard."
|
||||
end
|
||||
|
||||
ya.notify({
|
||||
title = "Размер ",
|
||||
content = notification_content,
|
||||
timeout = 5,
|
||||
})
|
||||
end
|
||||
end,
|
||||
}
|
||||
Reference in New Issue
Block a user