Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding file size to File browser hover #7485

Merged
merged 3 commits into from Nov 10, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 25 additions & 1 deletion packages/filebrowser/src/listing.ts
Expand Up @@ -1833,8 +1833,13 @@ export namespace DirListing {
// clean up the svg icon annotation, if any
delete icon.dataset.icon;
}
// add file size to pop up if its available
if (model.size !== null && model.size !== undefined) {
node.title = model.name + ' - ' + Private.formatFileSize(model.size, 1);
Copy link
Member

@blink1073 blink1073 Nov 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this! I think it looks too sparse without adding more info. I think we should show the following:

Name: foo.bar
Size: as you have it
Path: path limited to 50 chars with an ellipsis if longer
Created: Formatted using Time.format.
Modified: Same as above

Similar to what Finder shows on MacOS about a file:

image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And just not show the size part if we don't have it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that looks better. Let me fix it.

} else {
node.title = model.name;
}

node.title = model.name;
// If an item is being edited currently, its text node is unavailable.
if (text && text.textContent !== model.name) {
text.textContent = model.name;
Expand Down Expand Up @@ -2023,4 +2028,23 @@ namespace Private {
ElementExt.hitTest(node, x, y)
);
}

/**
* Format bytes to human readable string.
*/
export function formatFileSize(bytes: number, decimalPoint: number): string {
// https://www.codexworld.com/how-to/convert-file-size-bytes-kb-mb-gb-javascript/
if (bytes === 0) {
return '0 Bytes';
}
const k = 1000;
const dm = decimalPoint || 2;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
if (i >= 0 && i < sizes.length) {
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
} else {
return String(bytes);
}
}
}
5 changes: 5 additions & 0 deletions packages/services/src/contents/index.ts
Expand Up @@ -96,6 +96,11 @@ export namespace Contents {
* Only relevant for type: 'file'
*/
readonly format: FileFormat;

/**
* The size of then file in bytes.
*/
readonly size?: number;
}

/**
Expand Down