Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support copy name of active file #61

Merged
merged 7 commits into from
Oct 29, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/controller/CopyFileNameController.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { copy } from 'copy-paste';
import * as path from 'path';
import { promisify } from 'util';
import { window } from 'vscode';
import { BaseCopyController } from './BaseCopyController';

const copyAsync = promisify(copy);

const GENERIC_ERROR_MESSAGE = 'Could not perform copy file name to clipboard';

export class CopyFileNameController extends BaseCopyController {
// Possible errors and their suggested solutions
private readonly possibleErrorsMap: { [errorMessage: string]: string} = {
'spawn xclip ENOENT': 'Please install xclip package (`apt-get install xclip`)'
};

public async execute(): Promise<void> {
const sourcePath = this.sourcePath;
Expand All @@ -14,6 +21,18 @@ export class CopyFileNameController extends BaseCopyController {
}

const fileName = path.basename(sourcePath);
return copyAsync(fileName);
return copyAsync(fileName)
.catch((error: Error) => {
this.handleError(error.message);
sleistner marked this conversation as resolved.
Show resolved Hide resolved
});
}

private handleError(errorMessage: string): void {
// Can happen on unsupported platforms (e.g Linux machine without the xclip package installed).
// Attempting to provide a solution according to the error received
const errorSolution = this.possibleErrorsMap[errorMessage];
const warningMessageSuffix = errorSolution || errorMessage;

window.showWarningMessage(`${GENERIC_ERROR_MESSAGE}: ${warningMessageSuffix}`);
}
}
11 changes: 9 additions & 2 deletions test/command/CopyFileName.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,15 @@ describe('CopyFileNameCommand', () => {
it('check file name was copied to clipboard', () => {
return sut.execute()
.then(() => {
const clipboardData = clipboardPaste();
expect(clipboardData).to.equal(fixtureFile1Name);
clipboardPaste((err, pasteContent) => {
if (err) {
// paste is not supported on this platform, probably due to a
// missing xclip package.
return;
}

expect(pasteContent).to.equal(fixtureFile1Name);
});
});
});
});
Expand Down