Skip to content

Commit

Permalink
Tune naming
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach committed Aug 20, 2023
1 parent 451e3c1 commit a54d869
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
27 changes: 17 additions & 10 deletions lib/commands/clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,24 @@ import _ from 'lodash';

const commands = {};

/**
* @typedef {'plaintext' | 'image'} ContentTypeEnum
*/

/**
* @type {Record<ContentTypeEnum, ContentTypeEnum>}
*/
const CONTENT_TYPE = Object.freeze({
TEXT: 'text',
IMAGE: 'image',
plaintext: 'plaintext',
image: 'image',
});

// https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/set-clipboard?view=powershell-7.3

/**
* @typedef {Object} SetClipboardOptions
* @property {string} b64Content base64-encoded clipboard content to set
* @property {'text' | 'image'} contentType [text] The clipboard content type to set
* @property {ContentTypeEnum} contentType [text] The clipboard content type to set
*/

/**
Expand All @@ -26,20 +33,20 @@ const CONTENT_TYPE = Object.freeze({
commands.windowsSetClipboard = async function windowsSetClipboard (opts = {}) {
const {
b64Content,
contentType = CONTENT_TYPE.TEXT,
contentType = CONTENT_TYPE.plaintext,
} = requireArgs(['b64Content'], opts);
if (b64Content && Buffer.from(b64Content, 'base64').toString('base64') !== b64Content || !b64Content) {
throw new errors.InvalidArgumentError(
`The 'b64Content' argument must be a valid non-empty base64-encoded string`
);
}
switch (contentType) {
case CONTENT_TYPE.TEXT:
case CONTENT_TYPE.plaintext:
return await exec('powershell', ['-command',
`$str=[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('${b64Content}'));`,
'Set-Clipboard -Value $str'
]);
case CONTENT_TYPE.IMAGE:
case CONTENT_TYPE.image:
return await exec('powershell', ['-command',
`$img=[Drawing.Bitmap]::FromStream([IO.MemoryStream][Convert]::FromBase64String('${b64Content}'));`,
'[System.Windows.Forms.Clipboard]::SetImage($img);',
Expand All @@ -57,7 +64,7 @@ commands.windowsSetClipboard = async function windowsSetClipboard (opts = {}) {

/**
* @typedef {Object} GetClipboardOptions
* @property {'text' | 'image'} contentType [text] The clipboard content type to get.
* @property {ContentTypeEnum} contentType [plaintext] The clipboard content type to get.
* Only PNG images are supported for extraction if set to 'image'.
*/

Expand All @@ -69,17 +76,17 @@ commands.windowsSetClipboard = async function windowsSetClipboard (opts = {}) {
*/
commands.windowsGetClipboard = async function windowsGetClipboard (opts = {}) {
const {
contentType = CONTENT_TYPE.TEXT,
contentType = CONTENT_TYPE.plaintext,
} = opts;
switch (contentType) {
case CONTENT_TYPE.TEXT: {
case CONTENT_TYPE.plaintext: {
const {stdout} = await exec('powershell', ['-command',
'$str=Get-Clipboard;',
'[Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($str));'
]);
return _.trim(stdout);
}
case CONTENT_TYPE.IMAGE: {
case CONTENT_TYPE.image: {
const {stdout} = await exec('powershell', ['-command',
'$s=New-Object System.IO.MemoryStream;',
'[System.Windows.Forms.Clipboard]::GetImage().Save($s,[System.Drawing.Imaging.ImageFormat]::Png);',
Expand Down
4 changes: 2 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ export async function shellExec(cmd, args = [], opts = {}) {
* Assert the presence of particular keys in the given object
*
* @template {Object} T
* @param {string|string[]} argNames one or more key names
* @param {keyof T|(keyof T)[]} argNames one or more key names
* @param {T} opts the object to check
* @returns {T} the same given object
*/
export function requireArgs (argNames, opts) {
for (const argName of (_.isArray(argNames) ? argNames : [argNames])) {
if (!_.has(opts, argName)) {
throw new errors.InvalidArgumentError(`'${argName}' argument must be provided`);
throw new errors.InvalidArgumentError(`'${String(argName)}' argument must be provided`);
}
}
return opts;
Expand Down

0 comments on commit a54d869

Please sign in to comment.