From 3f1c2b27b64826bf0b32f1540013892ffa65e839 Mon Sep 17 00:00:00 2001 From: uctakeoff Date: Wed, 4 Jan 2023 17:13:02 +0900 Subject: [PATCH] feat: #81 --- CHANGELOG.md | 11 +++++++++++ package.json | 7 ++++++- package.nls.ja.json | 1 + package.nls.json | 1 + src/extension.ts | 20 +++++++++++++++++--- 5 files changed, 36 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ee75e2..b51af3b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,17 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how * workspace counter in status bar. +## [3.2.0] + +### Changed + +- Count the files in the top-level as a separate item #81 + +## [3.1.0] + +### Changed + +- Support storing collected language elsewhere #78 ## [3.0.0] ### Changed diff --git a/package.json b/package.json index 0c06050..b915d91 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "vscode-counter", "displayName": "VS Code Counter", "description": "Count lines of code in many programming languages.", - "version": "3.1.0", + "version": "3.2.0", "publisher": "uctakeoff", "author": { "name": "Ushiyama Kentaro" @@ -147,6 +147,11 @@ "minimum": 0, "default": 5 }, + "VSCodeCounter.countDirectLevelFiles": { + "description": "%configuration.countDirectLevelFiles.description%", + "type": "boolean", + "default": true + }, "VSCodeCounter.outputDirectory": { "description": "%configuration.outputDirectory.description%", "type": "string", diff --git a/package.nls.ja.json b/package.nls.ja.json index e806589..159d8d0 100644 --- a/package.nls.ja.json +++ b/package.nls.ja.json @@ -15,6 +15,7 @@ "configuration.include.description": "ファイルとフォルダーを追加するための glob パターン.", "configuration.history.description": "最近の結果を履歴として保持する数. 1 より大きいとき 結果をディレクトリを分けて保存する.", "configuration.outputDirectory.description": "結果出力先のディレクトリパス.", + "configuration.countDirectLevelFiles.description": "ディレクトリ直下にあるファイルを別項目として数える.", "configuration.outputAsText.description": "結果をテキストファイルで出力する.", "configuration.outputAsCSV.description": "結果をCSVファイルで出力する.", "configuration.outputAsMarkdown.description": "結果をMarkdown形式で出力する.", diff --git a/package.nls.json b/package.nls.json index 5fbf5e7..1092ee2 100644 --- a/package.nls.json +++ b/package.nls.json @@ -14,6 +14,7 @@ "configuration.exclude.description": "Configure glob patterns for excluding files and folders.", "configuration.include.description": "Configure glob patterns for including files and folders.", "configuration.history.description": "Controls the number of recent result to keep in history. If it is greater than 1, save the results in separate directories.", + "configuration.countDirectLevelFiles.description": "Count the files in the direct level as a separate item.", "configuration.outputDirectory.description": "Directory path for outputting results.", "configuration.outputAsText.description": "Whether to output the result as a text file.", "configuration.outputAsCSV.description": "Whether to output the result as a CSV file.", diff --git a/src/extension.ts b/src/extension.ts index 5f225d7..6c562af 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -91,6 +91,7 @@ const loadConfig = () => { outputAsText: conf.get('outputAsText', true), outputAsCSV: conf.get('outputAsCSV', true), outputAsMarkdown: conf.get('outputAsMarkdown', true), + countDirectLevelFiles: conf.get('countDirectLevelFiles', true), }; } type Config = ReturnType; @@ -490,7 +491,7 @@ const outputResults = async (date: Date, targetDirUri: vscode.Uri, results: Resu await makeDirectories(outputDir); writeTextFile(outputDir, `results.json`, resultsToJson(results)); - const resultTable = new ResultFormatter(targetDirUri, results, conf.endOfLine, conf.printNumberWithCommas ? toStringWithCommas : undefined); + const resultTable = new ResultFormatter(targetDirUri, results, conf); log(`OutputDir : ${outputDir}, count ${results.length} files`); const diffs: Result[] = []; @@ -518,7 +519,7 @@ const outputResults = async (date: Date, targetDirUri: vscode.Uri, results: Resu diffs.length = 0; } } - const diffTable = new ResultFormatter(targetDirUri, diffs, conf.endOfLine, conf.printNumberWithCommas ? toStringWithCommas : undefined); + const diffTable = new ResultFormatter(targetDirUri, diffs, conf); if (conf.outputAsText) { await writeTextFile(outputDir, 'results.txt', resultTable.toTextLines(date)); @@ -609,10 +610,17 @@ class ResultFormatter { private dirResultTable = new Map(); private langResultTable = new Map(); private total = new Statistics('Total'); + private endOfLine: string; + private valueToString: (obj: any) => string; + + constructor(private targetDirUri: vscode.Uri, private results: Result[], conf: {countDirectLevelFiles: boolean, endOfLine: string, printNumberWithCommas: boolean}) { + this.endOfLine = conf.endOfLine; + this.valueToString = conf.printNumberWithCommas ? toStringWithCommas : (obj: any) => obj.toString(); - constructor(private targetDirUri: vscode.Uri, private results: Result[], private endOfLine: string, private valueToString = (obj: any) => obj.toString()) { + const directLevelResultTable = new Map(); results.forEach((result) => { let parent = path.dirname(path.relative(this.targetDirUri.fsPath, result.filename)); + getOrSet(directLevelResultTable, parent, () => new Statistics(parent + " (Files)")).add(result); while (parent.length >= 0) { getOrSet(this.dirResultTable, parent, () => new Statistics(parent)).add(result); const p = path.dirname(parent); @@ -624,6 +632,12 @@ class ResultFormatter { getOrSet(this.langResultTable, result.language, () => new Statistics(result.language)).add(result); this.total.add(result); }); + if (conf.countDirectLevelFiles) { + [...directLevelResultTable.entries()].filter(([key, value]) => { + log(` dir[${value.name}] total=${value.total} ${(this.dirResultTable.get(key)?.total??0)}` ); + return value.total !== (this.dirResultTable.get(key)?.total??0); + }).forEach(([, value]) => this.dirResultTable.set(value.name, value)); + } } toCSVLines() { const languages = [...this.langResultTable.keys()];