Skip to content

Commit

Permalink
feat: countup files in dir size
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoluoboding committed Sep 3, 2020
1 parent 8b44bcf commit 06df266
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@
"package": "vsce package"
},
"devDependencies": {
"typescript": "^3.9.4",
"@types/node": "^14.6.3",
"@types/vscode": "^1.32.0",
"@typescript-eslint/eslint-plugin": "^3.0.2",
"@typescript-eslint/parser": "^3.0.2",
"eslint": "^7.1.0",
"@types/vscode": "^1.32.0"
"typescript": "^3.9.4"
}
}
46 changes: 44 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
*--------------------------------------------------------*/

import * as vscode from 'vscode';
import { posix } from 'path';

import { getHumanReadableSize } from './utils';

const { window, workspace } = vscode;

let myStatusBarItem: vscode.StatusBarItem;
let fileSize = '';
let [fileSize, dirSize] = ['', ''];

export function activate({ subscriptions }: vscode.ExtensionContext) {
// register a command that is invoked when the status bar
Expand Down Expand Up @@ -59,11 +60,52 @@ async function getFileSize(doc: vscode.TextDocument): Promise<void> {

if (fsStat.size > 0) {
fileSize = getHumanReadableSize(fsStat.size);
window.showInformationMessage(`${fileSize}`);
// window.showInformationMessage(`${fileSize}`);
myStatusBarItem.text = `$(file-code) ${fileSize}`;
myStatusBarItem.show();
getDirSize();
}
} catch {
myStatusBarItem.hide();
}
}


async function countAndTotalOfFilesInFolder(folder: vscode.Uri): Promise<{ total: number, count: number }> {
let total = 0;
let count = 0;

for (const [name, type] of await vscode.workspace.fs.readDirectory(folder)) {
if (type === vscode.FileType.File) {
const filePath = posix.join(folder.path, name);
const stat = await vscode.workspace.fs.stat(folder.with({ path: filePath }));
total += stat.size;
count += 1;
}
}

return { total, count };
}

async function getDirSize () {
if (!window.activeTextEditor) {
return window.showInformationMessage('Open a file first');
}
const fileUri = window.activeTextEditor.document.uri;

const folderPath = posix.dirname(fileUri.path);
const folderUri = fileUri.with({ path: folderPath });

const info = await countAndTotalOfFilesInFolder(folderUri);
/* open new text document */
// const doc = await workspace.openTextDocument({ content: `${info.count} files in ${folderUri.toString(true)} with a total of ${info.total} bytes` });
// vscode.window.showTextDocument(doc, { viewColumn: vscode.ViewColumn.Beside });

if (info) {
dirSize = getHumanReadableSize(info.total);
// window.showInformationMessage(`${info.count} files in ${folderUri.toString(true)} with a total of ${dirSize}`);
window.showInformationMessage(`${info.count} files with a total of ${info.total} Bytes [${dirSize}]`);
// myStatusBarItem.text = `$(file-directory) ${dirSize}`;
// myStatusBarItem.show();
}
}
Binary file modified vscode-project-size-0.0.1.vsix
Binary file not shown.
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.6.tgz#f4c7ec43e81b319a9815115031709f26987891f0"
integrity sha512-3c+yGKvVP5Y9TYBEibGNR+kLtijnj7mYrXRg+WpFb2X9xm04g/DXYkfg4hmzJQosc9snFNUPkbYIhu+KAm6jJw==

"@types/node@^14.6.3":
version "14.6.3"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.6.3.tgz#cc4f979548ca4d8e7b90bc0180052ab99ee64224"
integrity sha512-pC/hkcREG6YfDfui1FBmj8e20jFU5Exjw4NYDm8kEdrW+mOh0T1Zve8DWKnS7ZIZvgncrctcNCXF4Q2I+loyww==

"@types/vscode@^1.32.0":
version "1.48.0"
resolved "https://registry.yarnpkg.com/@types/vscode/-/vscode-1.48.0.tgz#c1841ccf80086d53b35a9d7f2eb3b4d949bd2d2f"
Expand Down

0 comments on commit 06df266

Please sign in to comment.