forked from gep13-oss/clipimg-vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(gep13-ossGH-12) Basic implementation of getting Clipboard contents
- Still some work to be done here, but PoC works
- Loading branch information
Showing
6 changed files
with
204 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |