Skip to content

Commit

Permalink
feat: Enable the real-time counter at start-up.
Browse files Browse the repository at this point in the history
However, it should not be resident.
  • Loading branch information
uctakeoff committed Dec 16, 2023
1 parent dfff05e commit 8817d4f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
### Changed
- Change the line counting method [#96](https://github.com/uctakeoff/vscode-counter/issues/96).
- In line with the POSIX definition of lines in text files, lines that do not end with a newline (i.e., the last line) are not included in the count.However, the default setting is the same as before to avoid confusion.
- Enable the real-time counter at start-up.
- However, it should not be resident.

## [3.2.2]
### Fixed
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"line"
],
"activationEvents": [
"workspaceContains:**/.VSCodeCounterCountRealtime",
"onCommand:extension.vscode-counter.countInFile",
"onCommand:extension.vscode-counter.countInDirectory",
"onCommand:extension.vscode-counter.countInWorkspace",
Expand Down
31 changes: 21 additions & 10 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { internalDefinitions } from './internalDefinitions';
const EXTENSION_ID = 'uctakeoff.vscode-counter';
const EXTENSION_NAME = 'VSCodeCounter';
const CONFIGURATION_SECTION = 'VSCodeCounter';
const REALTIME_COUNTER_FILE = '.VSCodeCounterCountRealtime';
const toZeroPadString = (num: number, fig: number) => num.toString().padStart(fig, '0');
const toLocalDateString = (date: Date, delims: [string, string, string] = ['-', ' ', ':']) => {
return `${date.getFullYear()}${delims[0]}${toZeroPadString(date.getMonth() + 1, 2)}${delims[0]}${toZeroPadString(date.getDate(), 2)}${delims[1]}`
Expand Down Expand Up @@ -116,6 +117,12 @@ class CodeCounterController {

// create a combined disposable from both event subscriptions
this.disposable = vscode.Disposable.from(...subscriptions);

currentWorkspaceFolder().then((workFolder) => {
vscode.workspace.fs.stat(buildUri(workFolder.uri, this.conf.outputDirectory, REALTIME_COUNTER_FILE)).then(st => {
this.toggleVisible();
});
});
}
dispose() {
this.statusBarItem?.dispose();
Expand Down Expand Up @@ -159,9 +166,15 @@ class CodeCounterController {
if (!this.statusBarItem) {
this.statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 100000);
this.countLinesInEditor(vscode.window.activeTextEditor);
currentWorkspaceFolder().then((workFolder) => {
writeTextFile(buildUri(workFolder.uri, this.conf.outputDirectory, REALTIME_COUNTER_FILE), '', {recursive: true});
});
} else {
this.statusBarItem.dispose();
this.statusBarItem = null;
currentWorkspaceFolder().then((workFolder) => {
vscode.workspace.fs.delete(buildUri(workFolder.uri, this.conf.outputDirectory, REALTIME_COUNTER_FILE));
});
}
}

Expand Down Expand Up @@ -455,14 +468,12 @@ const saveLanguageConfigurations = async (langs: { [key: string]: LanguageConf }
break;
case "output directory":{
const workFolder = await currentWorkspaceFolder();
const outputDir = buildUri(workFolder.uri, conf.outputDirectory);
await makeDirectories(outputDir);
await writeTextFile(outputDir, 'languages.json', JSON.stringify(langs));
await writeTextFile(buildUri(workFolder.uri, conf.outputDirectory, 'languages.json'), JSON.stringify(langs), {recursive: true});
break;
}
case "use languageConfUri":{
const workFolder = await currentWorkspaceFolder();
await writeTextFile(parseUriOrFile(conf.languageConfUri, workFolder.uri), undefined, JSON.stringify(langs));
await writeTextFile(parseUriOrFile(conf.languageConfUri, workFolder.uri), JSON.stringify(langs), {recursive: true});
break;
}
default: break;
Expand Down Expand Up @@ -501,7 +512,7 @@ const previewFiles = new Map<string, string>([
]);
const outputResults = async (date: Date, targetDirUri: vscode.Uri, results: Result[], outputDir: vscode.Uri, prevOutputDir: vscode.Uri | undefined, conf: Config) => {
await makeDirectories(outputDir);
writeTextFile(outputDir, `results.json`, resultsToJson(results));
writeTextFile(buildUri(outputDir, `results.json`), resultsToJson(results));

const resultTable = new ResultFormatter(targetDirUri, results, conf);
log(`OutputDir : ${outputDir}, count ${results.length} files`);
Expand Down Expand Up @@ -534,12 +545,12 @@ const outputResults = async (date: Date, targetDirUri: vscode.Uri, results: Resu
const diffTable = new ResultFormatter(targetDirUri, diffs, conf);

if (conf.outputAsText) {
await writeTextFile(outputDir, 'results.txt', resultTable.toTextLines(date));
await writeTextFile(outputDir, 'diff.txt', diffTable.toTextLines(date));
await writeTextFile(buildUri(outputDir, 'results.txt'), resultTable.toTextLines(date));
await writeTextFile(buildUri(outputDir, 'diff.txt'), diffTable.toTextLines(date));
}
if (conf.outputAsCSV) {
await writeTextFile(outputDir, 'results.csv', resultTable.toCSVLines());
await writeTextFile(outputDir, 'diff.csv', diffTable.toCSVLines());
await writeTextFile(buildUri(outputDir, 'results.csv'), resultTable.toCSVLines());
await writeTextFile(buildUri(outputDir, 'diff.csv'), diffTable.toCSVLines());
}
if (conf.outputAsMarkdown) {
const mds = [
Expand All @@ -549,7 +560,7 @@ const outputResults = async (date: Date, targetDirUri: vscode.Uri, results: Resu
{ title: 'Diff Details', path: 'diff-details.md', table: diffTable, detail: true },
];
await Promise.all(mds.map(({ title, path, table, detail }, index) => {
return writeTextFile(outputDir, path, table.toMarkdown(date, title, detail, mds.map((f, i) => [f.title, i === index ? undefined : f.path])));
return writeTextFile(buildUri(outputDir, path), table.toMarkdown(date, title, detail, mds.map((f, i) => [f.title, i === index ? undefined : f.path])));
}));
}
const previewFile = previewFiles.get(conf.outputPreviewType);
Expand Down
6 changes: 4 additions & 2 deletions src/vscode-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,10 @@ export const showTextPreview = async (uri: vscode.Uri) => {
await showTextFile(uri);
}
}
export const writeTextFile = async (baseUri: vscode.Uri, path: string | undefined, text: string) => {
const uri = path ? buildUri(baseUri, path) : baseUri;
export const writeTextFile = async (uri: vscode.Uri, text: string, option?: {recursive?: boolean}) => {
if (option?.recursive) {
await makeDirectories(dirUri(uri));
}
// log(`writeTextFile : ${uri} ${text.length}B`);
await vscode.workspace.fs.writeFile(uri, encoderU8.encode(text));
return uri;
Expand Down

0 comments on commit 8817d4f

Please sign in to comment.