From 1b347b3156a5d03a867c3fbffd5b039fd09ad812 Mon Sep 17 00:00:00 2001 From: bhanufyi Date: Sat, 26 Oct 2024 15:41:03 -0400 Subject: [PATCH] =?UTF-8?q?feat:=20show=20cpm,=20wpm=20=F0=9F=90=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/tasks.json | 29 ++++++++--- .vscode/tasks.json.old | 30 +++++++++++ package.json | 18 ++++++- src/extension.ts | 116 +++++++++++++++++++++++------------------ 4 files changed, 132 insertions(+), 61 deletions(-) create mode 100644 .vscode/tasks.json.old diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 1e37eb7..55b4e53 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -8,17 +8,11 @@ // A task runner that calls a custom npm script that compiles the extension. { - "version": "0.1.0", + "version": "2.0.0", // we want to run npm "command": "npm", - // the command is a shell script - "isShellCommand": true, - - // show the output window only if unrecognized errors occur. - "showOutput": "silent", - // we run the custom script "compile" as defined in package.json "args": ["run", "compile", "--loglevel", "silent"], @@ -26,5 +20,24 @@ "isBackground": true, // use the standard tsc in watch mode problem matcher to find compile problems in the output. - "problemMatcher": "$tsc-watch" + "problemMatcher": "$tsc-watch", + "tasks": [ + { + "label": "npm", + "type": "shell", + "command": "npm", + "args": [ + "run", + "compile", + "--loglevel", + "silent" + ], + "isBackground": true, + "problemMatcher": "$tsc-watch", + "group": { + "_id": "build", + "isDefault": false + } + } + ] } \ No newline at end of file diff --git a/.vscode/tasks.json.old b/.vscode/tasks.json.old new file mode 100644 index 0000000..1e37eb7 --- /dev/null +++ b/.vscode/tasks.json.old @@ -0,0 +1,30 @@ +// Available variables which can be used inside of strings. +// ${workspaceRoot}: the root folder of the team +// ${file}: the current opened file +// ${fileBasename}: the current opened file's basename +// ${fileDirname}: the current opened file's dirname +// ${fileExtname}: the current opened file's extension +// ${cwd}: the current working directory of the spawned process + +// A task runner that calls a custom npm script that compiles the extension. +{ + "version": "0.1.0", + + // we want to run npm + "command": "npm", + + // the command is a shell script + "isShellCommand": true, + + // show the output window only if unrecognized errors occur. + "showOutput": "silent", + + // we run the custom script "compile" as defined in package.json + "args": ["run", "compile", "--loglevel", "silent"], + + // The tsc compiler is started in watching mode + "isBackground": true, + + // use the standard tsc in watch mode problem matcher to find compile problems in the output. + "problemMatcher": "$tsc-watch" +} \ No newline at end of file diff --git a/package.json b/package.json index fabe1ae..a95723f 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,22 @@ "categories": [ "Other" ], + "contributes": { + "configuration": { + "title": "Typing Speed", + "properties": { + "typingSpeed.display": { + "type": "string", + "default": "wpm", + "enum": [ + "wpm", + "cpm" + ], + "description": "Display typing speed as words per minute (wpm) or characters per minute (cpm)." + } + } + } + }, "activationEvents": [ "*" ], @@ -38,6 +54,6 @@ "mocha": "^2.3.3", "ts-node": "^3.0.4", "typescript": "^2.0.3", - "vscode": "^1.0.0" + "vscode": "^1.1.37" } } diff --git a/src/extension.ts b/src/extension.ts index c30dd8e..762d11a 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,73 +1,85 @@ -'use strict'; -import { Disposable, ExtensionContext, StatusBarAlignment, StatusBarItem, window } from 'vscode'; -import RollingArray from './rollingqueue'; +"use strict"; +import { + Disposable, + ExtensionContext, + StatusBarAlignment, + StatusBarItem, + window, + workspace, +} from "vscode"; +import RollingArray from "./rollingqueue"; export function activate(context: ExtensionContext) { - let typingSpeed = new TypingSpeed(); - let controller = new TypingSpeedController(typingSpeed); + let typingSpeed = new TypingSpeed(); + let controller = new TypingSpeedController(typingSpeed); - context.subscriptions.push(controller); - context.subscriptions.push(typingSpeed); + context.subscriptions.push(controller); + context.subscriptions.push(typingSpeed); } class TypingSpeed { - private _keystrokes: RollingArray = new RollingArray(100); - private _statusBarItem: StatusBarItem; - private _lastUpdate: number = 0; - - public updateSpeed() { - - if (!this._statusBarItem) { - this._statusBarItem = window.createStatusBarItem(StatusBarAlignment.Left); - } - - const now = Date.now(); - if (now - this._lastUpdate > 2000) { - this._keystrokes.clear(); - } - this._lastUpdate = now; - this._keystrokes.add(now); - - let cpm = 0; - const count = this._keystrokes.size(); - if (count > 1) { - const firstTime = this._keystrokes.first(); - cpm = count * 60000 / (now - firstTime); - } - - this._statusBarItem.text = `$(keyboard) ${cpm.toFixed(1)} cpm`; - this._statusBarItem.show(); + private _keystrokes: RollingArray = new RollingArray(100); + private _statusBarItem: StatusBarItem; + private _lastUpdate: number = 0; + + public updateSpeed() { + if (!this._statusBarItem) { + this._statusBarItem = window.createStatusBarItem(StatusBarAlignment.Left); } - dispose() { - this._statusBarItem.dispose(); + const now = Date.now(); + if (now - this._lastUpdate > 2000) { + this._keystrokes.clear(); + } + this._lastUpdate = now; + this._keystrokes.add(now); + // Read the configuration option + const config = workspace.getConfiguration("typingSpeed"); + const displayType = config.get("display", "wpm"); + + let cpm = 0; + let wpm = 0; + const count = this._keystrokes.size(); + if (count > 1) { + const firstTime = this._keystrokes.first(); + cpm = (count * 60000) / (now - firstTime); + wpm = cpm / 5; } + + this._statusBarItem.text = `$(keyboard) ${ + displayType === "cpm" ? `${cpm.toFixed(1)} cpm` : `${wpm.toFixed(1)} wpm` + }`; + this._statusBarItem.show(); + } + + dispose() { + this._statusBarItem.dispose(); + } } // this method is called when your extension is deactivated -export function deactivate() { -} +export function deactivate() {} class TypingSpeedController { - private _typingSpeed: TypingSpeed; - private _disposable: Disposable; + private _typingSpeed: TypingSpeed; + private _disposable: Disposable; - constructor(typingSpeed: TypingSpeed) { - this._typingSpeed = typingSpeed; + constructor(typingSpeed: TypingSpeed) { + this._typingSpeed = typingSpeed; - let subscriptions: Disposable[] = []; - window.onDidChangeTextEditorSelection(this._onEvent, this, subscriptions); + let subscriptions: Disposable[] = []; + window.onDidChangeTextEditorSelection(this._onEvent, this, subscriptions); - this._typingSpeed.updateSpeed(); + this._typingSpeed.updateSpeed(); - this._disposable = Disposable.from(...subscriptions); - } + this._disposable = Disposable.from(...subscriptions); + } - dispose() { - this._disposable.dispose(); - } + dispose() { + this._disposable.dispose(); + } - private _onEvent() { - this._typingSpeed.updateSpeed(); - } + private _onEvent() { + this._typingSpeed.updateSpeed(); + } }