Skip to content

Commit

Permalink
Merge pull request #85 from espenja/features/70-add-file-size
Browse files Browse the repository at this point in the history
feat: add support for file_size tag
  • Loading branch information
xhayper authored Oct 16, 2022
2 parents b71f422 + 194fc86 commit f829ca3
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 9 deletions.
41 changes: 35 additions & 6 deletions package.json

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getFileIcon, resolveFileIcon, toLower, toTitle, toUpper } from "./helpe
import { SetActivity } from "@xhayper/discord-rpc";
import { isExcluded } from "./helpers/isExcluded";
import { isObject } from "./helpers/isObject";
import { getFileSize } from "./helpers/getFileSize";
import { getConfig } from "./config";
import { dataClass } from "./data";
import { sep } from "path";
Expand Down Expand Up @@ -214,6 +215,8 @@ function details(
}`;

const fileIcon = resolveFileIcon(window.activeTextEditor.document);
const fileSize = getFileSize(config, dataClass);

const problems = config[CONFIG_KEYS.ShowProblems]
? config[CONFIG_KEYS.ProblemsText].replace(REPLACE_KEYS.ProblemsCount, totalProblems.toString())
: "";
Expand All @@ -233,6 +236,7 @@ function details(
raw = raw
.replace(REPLACE_KEYS.FileName, dataClass.fileName ?? FAKE_EMPTY)
.replace(REPLACE_KEYS.FileExtension, dataClass.fileExtension ?? FAKE_EMPTY)
.replace(REPLACE_KEYS.FileSize, fileSize ?? FAKE_EMPTY)
.replace(REPLACE_KEYS.DirName, dataClass.dirName ?? FAKE_EMPTY)
.replace(REPLACE_KEYS.Workspace, workspaceName)
.replace(REPLACE_KEYS.WorkspaceFolder, workspaceFolderName)
Expand Down
8 changes: 8 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { workspace, WorkspaceConfiguration } from "vscode";
import filesize from "file-size";

export type FileSizeConfig = Required<Parameters<typeof filesize>["1"]>;
export type FileSizeSpec = Parameters<ReturnType<typeof filesize<NonNullable<FileSizeConfig>>>["human"]>["0"];

export type WorkspaceExtensionConfiguration = WorkspaceConfiguration & {
id: string;
Expand Down Expand Up @@ -37,6 +41,10 @@ export type WorkspaceExtensionConfiguration = WorkspaceConfiguration & {
ignoreOrganizations: string[];
suppressNotifications: boolean;
prioritizeLanguagesOverExtensions: boolean;
fileSizeHumanReadable: boolean;
fileSizeSpec: FileSizeSpec;
fileSizeFixed: number;
fileSizeSpacer: string;
};

export function getConfig(): WorkspaceExtensionConfiguration {
Expand Down
9 changes: 7 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ export const enum CONFIG_KEYS {
IgnoreOrganizations = "ignoreOrganizations",
IgnoreGitHosts = "ignoreGitHosts",
SuppressNotifications = "suppressNotifications",
PrioritizeLanguagesOverExtensions = "prioritizeLanguagesOverExtensions"
PrioritizeLanguagesOverExtensions = "prioritizeLanguagesOverExtensions",
FileSizeHumanReadable = "fileSizeHumanReadable",
FileSizeSpec = "fileSizeSpec",
FileSizeFixed = "fileSizeFixed",
FileSizeSpacer = "fileSizeSpacer"
}

export const enum REPLACE_KEYS {
Expand All @@ -73,5 +77,6 @@ export const enum REPLACE_KEYS {
Problems = "{problems}",
ProblemsCount = "{problemsCount}",
GitRepo = "{git_repo}",
GitBranch = "{git_branch}"
GitBranch = "{git_branch}",
FileSize = "{file_size}"
}
11 changes: 10 additions & 1 deletion src/data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Disposable, EventEmitter, Extension, extensions, window, workspace, WorkspaceFolder } from "vscode";
import { API as GitApi, GitExtension, Remote, Repository } from "./@types/git";
import { parse, ParsedPath, sep } from "path";
import { parse, ParsedPath, sep, join } from "path";
import { statSync } from "fs";
import gitUrlParse from "git-url-parse";
import { logInfo } from "./logger";

Expand Down Expand Up @@ -60,6 +61,14 @@ export class Data implements DisposableLike {
return v;
}

public get fileSize(): number | undefined {
if (!this._file) return undefined;
const absolutePath = join(this._file.dir, this._file.base);
const v = statSync(absolutePath).size;
this.debug(4, `fileSize(): ${v}`);
return v;
}

public get dirName(): string | undefined {
const v = this._file?.dir.split(sep).pop();
this.debug(4, `dirName(): ${v}`);
Expand Down
33 changes: 33 additions & 0 deletions src/helpers/getFileSize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import humanFileSize from "file-size";
import type { Data } from "../data";
import { CONFIG_KEYS } from "../constants";
import { FileSizeConfig, FileSizeSpec, WorkspaceExtensionConfiguration } from "../config";

export const getFileSize = (config: WorkspaceExtensionConfiguration, dataClass: Data) => {
if (dataClass.fileSize === undefined) return undefined;

let fixed = 2;
if (config[CONFIG_KEYS.FileSizeFixed] === 0 || config[CONFIG_KEYS.FileSizeFixed]) {
fixed = config[CONFIG_KEYS.FileSizeFixed];
}

let spacer = " ";
if (config[CONFIG_KEYS.FileSizeSpacer] === "" || config[CONFIG_KEYS.FileSizeSpacer]) {
spacer = config[CONFIG_KEYS.FileSizeSpacer];
}

let fileSize: string | undefined = undefined;
const fileSizeSpec: FileSizeSpec = config[CONFIG_KEYS.FileSizeSpec] || "iec";
const fileSizeConfig: FileSizeConfig = {
fixed,
spacer
};

if (config[CONFIG_KEYS.FileSizeHumanReadable]) {
fileSize = humanFileSize(dataClass.fileSize, fileSizeConfig).human(fileSizeSpec);
} else {
fileSize = `${dataClass.fileSize.toLocaleString()}${fileSizeConfig.spacer}B`;
}

return fileSize;
};
16 changes: 16 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ __metadata:
languageName: node
linkType: hard

"@types/file-size@npm:^1.0.1":
version: 1.0.1
resolution: "@types/file-size@npm:1.0.1"
checksum: 4c748f1c81faf820b468ae83e81b54f6f87713246a00e077c5e1eafe673972d536f300ee7aedaa21a20d9802d38ef4235d6e907ac045a6b8c7523b0d55c04b06
languageName: node
linkType: hard

"@types/git-url-parse@npm:^9.0.1":
version: 9.0.1
resolution: "@types/git-url-parse@npm:9.0.1"
Expand Down Expand Up @@ -791,6 +798,13 @@ __metadata:
languageName: node
linkType: hard

"file-size@npm:^1.0.0":
version: 1.0.0
resolution: "file-size@npm:1.0.0"
checksum: 9de366e448db1c840a6b399619f53895c4225db9b6b4c78ab6e208a60aaafefa165920290bc98d8aea61564695e57d74f2f0ac5c57fc13a20e7ba250ca7fdb51
languageName: node
linkType: hard

"fill-range@npm:^7.0.1":
version: 7.0.1
resolution: "fill-range@npm:7.0.1"
Expand Down Expand Up @@ -2176,10 +2190,12 @@ __metadata:
version: 0.0.0-use.local
resolution: "vscord@workspace:."
dependencies:
"@types/file-size": ^1.0.1
"@types/git-url-parse": ^9.0.1
"@types/node": 16.x
"@types/vscode": ^1.72.0
"@xhayper/discord-rpc": ^1.0.12
file-size: ^1.0.0
git-url-parse: ^13.1.0
husky: ^8.0.1
prettier: ^2.7.1
Expand Down

0 comments on commit f829ca3

Please sign in to comment.