Skip to content

Commit

Permalink
feat: Added option to limit size of clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Feb 10, 2020
1 parent d50ba39 commit 3ae3427
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ This extension contributes the following settings (default values):
// Time in milliseconds to check changes in clipboard. Set zero to disable.
"clipboard-manager.checkInterval": 500,

// Maximum clipboard size in bytes.
"clipboard-manager.maxClipboardSize": 1000000,

// Maximum number of clips to save in clipboard
"clipboard-manager.maxClips": 100,

Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@
"default": 500,
"description": "Time in milliseconds to check changes in clipboard. Set zero to disable."
},
"clipboard-manager.maxClipboardSize": {
"type": "integer",
"default": 1000000,
"description": "Maximum clipboard size in bytes."
},
"clipboard-manager.maxClips": {
"type": "integer",
"default": 100,
Expand Down
1 change: 1 addition & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export async function activate(context: vscode.ExtensionContext) {
const config = vscode.workspace.getConfiguration("clipboard-manager");
monitor.checkInterval = config.get("checkInterval", 500);
monitor.onlyWindowFocused = config.get("onlyWindowFocused", true);
monitor.maxClipboardSize = config.get("maxClipboardSize", 1000000);
};
updateConfig();

Expand Down
16 changes: 13 additions & 3 deletions src/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export class Monitor implements vscode.Disposable {

protected _timer: NodeJS.Timer | undefined;

public maxClipboardSize: number = 1000000;

protected _checkInterval: number = 500;
get checkInterval() {
return this._checkInterval;
Expand All @@ -41,7 +43,7 @@ export class Monitor implements vscode.Disposable {

constructor(readonly clipboard: BaseClipboard) {
// Update current clipboard to check changes after init
this.clipboard.readText().then(value => {
this.readText().then(value => {
this._previousText = value;

// Initialize the checkInterval
Expand Down Expand Up @@ -74,10 +76,18 @@ export class Monitor implements vscode.Disposable {
);
}

protected async readText(): Promise<string> {
const text = await this.clipboard.readText();
if (text.length > this.maxClipboardSize) {
return "";
}
return text;
}

protected async onDidChangeWindowState(state: vscode.WindowState) {
// Prevent detect change from external copy
if (this.onlyWindowFocused && state.focused) {
this._previousText = await this.clipboard.readText();
this._previousText = await this.readText();
}

this._windowFocused = state.focused;
Expand All @@ -89,7 +99,7 @@ export class Monitor implements vscode.Disposable {
return;
}

const newText = await this.clipboard.readText();
const newText = await this.readText();
if (newText === this._previousText) {
return;
}
Expand Down

0 comments on commit 3ae3427

Please sign in to comment.