Skip to content

Commit

Permalink
(gep13-ossGH-12) Basic implementation of getting Clipboard contents
Browse files Browse the repository at this point in the history
- Still some work to be done here, but PoC works
  • Loading branch information
gep13 committed Feb 25, 2019
1 parent 8cea335 commit 299e3cf
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 4 deletions.
90 changes: 90 additions & 0 deletions src/clipboard-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { injectable, inject } from 'inversify';
import { spawn } from 'child_process';
import TYPES from './types';
import { FileSystemService } from './filesystem-service';
import { MessageService } from './message-service';

@injectable()
export class ClipboardService {
constructor(
@inject(TYPES.FileSystemService) private fileSystemService: FileSystemService,
@inject(TYPES.MessageService) private messageService: MessageService
){}

getContentsAndSaveToFile(path: string, callback: (imagePath: string, imagePathFromScript: string) => void) {
let context = this;

if (!path) return;

let platform = process.platform;
if (platform === 'win32') {
// Windows
const scriptPath = context.fileSystemService.combinePath(__dirname, '../resources/pc.ps1');

let command = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe";
let powershellExisted = context.fileSystemService.directoryExists(command)
if (!powershellExisted) {
command = "powershell"
}

const powershell = spawn(command, [
'-noprofile',
'-noninteractive',
'-nologo',
'-sta',
'-executionpolicy', 'unrestricted',
'-windowstyle', 'hidden',
'-file', scriptPath,
path
]);
powershell.on('error', function (e: any) {
if (e.code == "ENOENT") {
context.messageService.showError(`The powershell command is not in you PATH environment variables.Please add it and retry.`);
} else {
context.messageService.showError(e);
}
});
powershell.on('exit', function (code, signal) {
// console.log('exit', code, signal);
});
powershell.stdout.on('data', function (data: Buffer) {
callback(path, data.toString().trim());
});
}
else if (platform === 'darwin') {
// Mac
let scriptPath = context.fileSystemService.combinePath(__dirname, '../resources/mac.applescript');

let ascript = spawn('osascript', [scriptPath, path]);
ascript.on('error', function (e: any) {
context.messageService.showError(e);
});
ascript.on('exit', function (code, signal) {
// console.log('exit',code,signal);
});
ascript.stdout.on('data', function (data: Buffer) {
callback(path, data.toString().trim());
});
} else {
// Linux

let scriptPath = context.fileSystemService.combinePath(__dirname, '../resources/linux.sh');

let ascript = spawn('sh', [scriptPath, path]);
ascript.on('error', function (e: any) {
context.messageService.showError(e);
});
ascript.on('exit', function (code, signal) {
// console.log('exit',code,signal);
});
ascript.stdout.on('data', function (data: Buffer) {
let result = data.toString().trim();
if (result == "no xclip") {
context.messageService.showInformation('You need to install xclip command first.');
return;
}
callback(path, result);
});
}
}
}
37 changes: 34 additions & 3 deletions src/commands/helloworld-comand.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,49 @@
import * as vscode from 'vscode';
import { injectable, inject } from "inversify";
import { ICommand } from "./icommand";
import { MessageService } from '../message-service';
import TYPES from '../types';
import { ClipboardService } from '../clipboard-service';

@injectable()
export class HelloWorldCommand implements ICommand {

constructor(
@inject(TYPES.MessageService) private messageService: MessageService
@inject(TYPES.MessageService) private messageService: MessageService,
@inject(TYPES.ClipboardService) private clipBoardService: ClipboardService
) {}

get id() { return "extension.helloWorld"; }

execute(...args: any[]) {
this.messageService.showInformation('Hello gep13 stream!');
async execute(...args: any[]) {
let altText = await this.messageService.showInput("Provide description for image", "");

this.clipBoardService.getContentsAndSaveToFile("C:\\github\\gep13\\clipimg-vscode\\tempImages\\test.png", (imagePath, imagePathReturnByScript) => {
this.messageService.showInformation(imagePath);
this.messageService.showInformation(imagePathReturnByScript);

let editor = vscode.window.activeTextEditor;
if(!editor) {
return;
}

let selection = editor.selection;

editor.edit(edit => {
let current = selection;
let prefix = '![';
let middle = ']('
let suffix = ')'

let imageFilePath = `${prefix}${altText}${middle}${imagePathReturnByScript}${suffix}`;
if(current.isEmpty) {
edit.insert(current.start, imageFilePath);
} else {
edit.replace(current, imageFilePath);
}
});

this.messageService.showInformation(altText);
});
}
}
54 changes: 54 additions & 0 deletions src/filesystem-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { workspace } from "vscode";
import * as fs from "fs";
import * as path from "path";
import { injectable, inject } from "inversify";
import { MessageService } from "./message-service";
import TYPES from "./types";

@injectable()
export class FileSystemService {
constructor(
@inject(TYPES.MessageService) private messageService: MessageService
) {}

combinePath(directoryPath: string, filePath: string) {
return path.join(directoryPath, filePath);
}

checkForWorkspace(): string {
if (workspace.rootPath !== undefined) {
return workspace.rootPath;
} else {
this.messageService.showError("No workspace is currently open.");
return "";
}
}

async checkForExisting(path: string): Promise<boolean> {
if (fs.existsSync(path)) {
var message = `Overwrite the existing \'${path}\' file in this folder?`;
var option = await this.messageService.showQuestion(message, "Overwrite");
return option === "Overwrite";
}

return true;
}

createWriteStream(path: string) {
return fs.createWriteStream(path);
}

createAppendWriteStream(path: string) {
return fs.createWriteStream(path, {
flags: 'a'
});
}

directoryExists(path: string) {
return fs.existsSync(path);
}

directoryCreate(path: string) {
fs.mkdirSync(path);
}
}
4 changes: 4 additions & 0 deletions src/inversify.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ import { ICommand } from './commands/icommand';
import { CommandManager } from './commands/command-manager';
import { HelloWorldCommand } from './commands/helloworld-comand';
import { MessageService } from './message-service';
import { ClipboardService } from './clipboard-service';
import { FileSystemService } from './filesystem-service';

const container = new Container();
container.bind(TYPES.MessageService).to(MessageService).inSingletonScope();
container.bind(TYPES.ClipboardService).to(ClipboardService).inSingletonScope();
container.bind(TYPES.FileSystemService).to(FileSystemService).inSingletonScope();
container.bind<ICommand>(TYPES.Command).to(HelloWorldCommand);
//container.bind<ICommand>(TYPES.Command).to(UploadToAzureCommand);
container.bind<CommandManager>(TYPES.CommandManager).to(CommandManager);
Expand Down
19 changes: 19 additions & 0 deletions src/message-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,23 @@ export class MessageService {
showInformation(message: string): void {
vscode.window.showInformationMessage(message);
}

showWarning(message: string) {
vscode.window.showWarningMessage(message);
}

async showQuestion(message: string, options: string): Promise<string | undefined> {
return await vscode.window.showWarningMessage(message, options);
}

showError(message: string): void {
vscode.window.showErrorMessage(message);
}

async showInput(placeHolder: string, value: string): Promise<any> {
return await vscode.window.showInputBox({
placeHolder: placeHolder,
value: value
});
}
}
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const TYPES = {
Command: Symbol("Command"),
CommandManager: Symbol("CommandManager"),
MessageService: Symbol("MessageService")
MessageService: Symbol("MessageService"),
ClipboardService: Symbol("ClipboardService"),
FileSystemService: Symbol("FileSystemService")
};

export default TYPES;

0 comments on commit 299e3cf

Please sign in to comment.