Skip to content

Commit

Permalink
feat: #81
Browse files Browse the repository at this point in the history
  • Loading branch information
uctakeoff committed Jan 4, 2023
1 parent 32b043e commit 3f1c2b2
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions package.nls.ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -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形式で出力する.",
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
20 changes: 17 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof loadConfig>;
Expand Down Expand Up @@ -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[] = [];
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -609,10 +610,17 @@ class ResultFormatter {
private dirResultTable = new Map<string, Statistics>();
private langResultTable = new Map<string, Statistics>();
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<string, Statistics>();
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);
Expand All @@ -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()];
Expand Down

0 comments on commit 3f1c2b2

Please sign in to comment.