Skip to content

Commit

Permalink
feat: show total folder size
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Cherng <[email protected]>
  • Loading branch information
jfcherng committed Jul 6, 2024
1 parent ca12ec3 commit 627a20f
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 11 deletions.
49 changes: 45 additions & 4 deletions assets/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,37 @@ const plugin_name = 'show_folder_size';
const config = rcmail.env[`${plugin_name}.config`] ?? {};
const prefs = rcmail.env[`${plugin_name}.prefs`] ?? {};

/**
* Format bytes as human-readable text.
*
* @param bytes Number of bytes.
* @param use_si True to use metric (SI) units, aka powers of 1000. False to use
* binary (IEC), aka powers of 1024.
* @param dp Number of decimal places to display.
*
* @return Formatted string.
*/
const humanizeBytes = (bytes, base_1k = true, dp = 1) => {
const thresh = base_1k ? 1000 : 1024;

if (Math.abs(bytes) < thresh) {
return bytes + ' B';
}

const units = base_1k
? ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
let u = -1;
const r = 10 ** dp;

do {
bytes /= thresh;
++u;
} while (Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1);

return bytes.toFixed(dp) + ' ' + units[u];
};

const generatePopupContent = (resp) => {
let html = `
<table id="show-folder-size-table" class="records-table" style="table-layout: auto;">
Expand All @@ -18,7 +49,8 @@ const generatePopupContent = (resp) => {
<tbody>
`;

for (let [id, [size, size_humanized, cumulative_size, cumulative_size_humanized]] of Object.entries(resp)) {
let total = 0;
for (let [id, [size, cumulative_size]] of Object.entries(resp)) {
let mailbox = rcmail.env.mailboxes[id];

// skip unsubscribed mailboxes
Expand All @@ -27,6 +59,7 @@ const generatePopupContent = (resp) => {
}

let level = (id.match(/\//g) ?? []).length;
total += size;

html += `
<tr>
Expand All @@ -37,13 +70,21 @@ const generatePopupContent = (resp) => {
>
<div style="margin-left: ${level * 1.5}em">${mailbox.name}</div>
</td>
<td data-size="${size}">${size_humanized}</td>
<td data-size="${cumulative_size}">${cumulative_size_humanized}</td>
<td data-size="${size}">${humanizeBytes(size)}</td>
<td data-size="${cumulative_size}">${humanizeBytes(cumulative_size)}</td>
</tr>
`;
}

html += '</tbody></table>';
html += `
<tfoot>
<tr>
<th><div>${rcmail.gettext('total', plugin_name)}</div></th>
<th data-size="${total}">${humanizeBytes(total)}</th>
<th data-size="-1">-</th>
</tr>
</tfoot>
</tbody></table>`;

return html;
};
Expand Down
1 change: 1 addition & 0 deletions localization/en_US.inc
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ $labels['folder_size'] = 'Folder Size';
$labels['name'] = 'Name';
$labels['size'] = 'Size';
$labels['cumulative_size'] = 'Size (Cumulative)';
$labels['total'] = 'Total';
1 change: 1 addition & 0 deletions localization/zh_CN.inc
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ $labels['folder_size'] = '文件夹大小';
$labels['name'] = '文件夹';
$labels['size'] = '大小';
$labels['cumulative_size'] = '大小(累计)';
$labels['total'] = '总计';
1 change: 1 addition & 0 deletions localization/zh_TW.inc
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ $labels['folder_size'] = '資料夾大小';
$labels['name'] = '資料夾';
$labels['size'] = '大小';
$labels['cumulative_size'] = '大小(累計)';
$labels['total'] = '總計';
9 changes: 2 additions & 7 deletions show_folder_size.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private function addPluginButtons(): void
* Get size for all folders.
*
* @return array an array in the form of [
* folder_1 => [size_1, size_1_humanized, cumulative_1, cumulative_1_humanized],
* folder_1 => [size_1, cumulative_1],
* ... ]
*/
private function getFolderSizes(): array
Expand All @@ -105,12 +105,7 @@ private function getFolderSizes(): array

$ret = [];
foreach ($folders as $folder) {
$ret[$folder] = [
$rawSizes[$folder],
$this->rcmail->show_bytes($rawSizes[$folder]),
$cumulativeSizes[$folder],
$this->rcmail->show_bytes($cumulativeSizes[$folder]),
];
$ret[$folder] = [$rawSizes[$folder], $cumulativeSizes[$folder]];
}

return $ret;
Expand Down

0 comments on commit 627a20f

Please sign in to comment.