Skip to content
This repository was archived by the owner on Jan 10, 2019. It is now read-only.

Commit 3c6a8df

Browse files
committed
added 'hex view' for binary content
1 parent 1391844 commit 3c6a8df

File tree

5 files changed

+68
-5
lines changed

5 files changed

+68
-5
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Change Log (vs-script-commands)
22

3+
## 4.6.0 (May 1st, 2017; hex view of binary files)
4+
5+
* [Buffer](https://nodejs.org/api/buffer.html) results are displayed in "hex view" now, when displaying in tab
6+
* added `$toHexView()`, `$disableHexView()` functions and `$config` variable for [quick executions](https://github.com/mkloubert/vs-script-commands#quick-execution-)
7+
* added `toHexView()` method for [ScriptCommandExecutorArguments](https://mkloubert.github.io/vs-script-commands/interfaces/_contracts_.scriptcommandexecutorarguments.html) interface
8+
39
## 4.5.0 (May 1st, 2017; find files)
410

511
* added `$findFiles()` function for [quick executions](https://github.com/mkloubert/vs-script-commands#quick-execution-)

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "vs-script-commands",
33
"displayName": "Script Commands",
44
"description": "Adds additional commands to Visual Studio Code that uses scripts (JavaScript) for execution.",
5-
"version": "4.5.4",
5+
"version": "4.6.0",
66
"publisher": "mkloubert",
77
"engines": {
88
"vscode": "^1.6.0"
@@ -383,6 +383,11 @@
383383
"description": "The initial current directory for the executions.",
384384
"type": "string"
385385
},
386+
"disableHexView": {
387+
"description": "Do not show binary data in 'hex view'.",
388+
"type": "boolean",
389+
"default": false
390+
},
386391
"noResultInfo": {
387392
"description": "Do not show results of executions.",
388393
"type": "boolean",
@@ -446,6 +451,7 @@
446451
"dependencies": {
447452
"fs-extra": "^3.0.0",
448453
"glob": "^7.1.1",
454+
"hexy": "^0.2.9",
449455
"html-entities": "^1.2.1",
450456
"marked": "^0.3.6",
451457
"moment": "^2.17.1",

src/contracts.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ export interface Configuration {
6161
* The initial current directory for the executions.
6262
*/
6363
cwd?: string;
64+
/**
65+
* Do not show binary data in 'hex view'.
66+
*/
67+
disableHexView?: boolean;
6468
/**
6569
* Show result of execution or not.
6670
*/
@@ -535,6 +539,14 @@ export interface ScriptCommandExecutorArguments {
535539
* @return {Promise<any>} The promise.
536540
*/
537541
readonly stopCronJobs: (jobs: CronJobNames) => Promise<any>;
542+
/**
543+
* Converts a value, like a buffer or string, to "hex view".
544+
*
545+
* @param {any} val The value to convert.
546+
*
547+
* @return {string} The value in "hex view".
548+
*/
549+
readonly toHexView: (val: any) => string;
538550
}
539551

540552
/**

src/controller.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import * as Events from 'events';
2727
import * as Glob from 'glob';
28+
const Hexy = require('hexy');
2829
import * as HtmlEntities from 'html-entities';
2930
import * as Marked from 'marked';
3031
import * as Moment from 'moment';
@@ -921,7 +922,10 @@ export class ScriptCommandController extends Events.EventEmitter implements vsco
921922
reject(e);
922923
}
923924
});
924-
}
925+
},
926+
toHexView: (val) => {
927+
return Hexy.hexy(val);
928+
},
925929
};
926930

927931
// args.button

src/quick.ts

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import * as FS from 'fs';
2727
import * as FSExtra from 'fs-extra';
28+
const Hexy = require('hexy');
2829
import * as HtmlEntities from 'html-entities';
2930
import * as Glob from 'glob';
3031
import * as Marked from 'marked';
@@ -52,6 +53,7 @@ export interface ScriptModule {
5253

5354

5455
let _permanentCurrentDirectory: string;
56+
let _permanentDisableHexView: boolean;
5557
let _permanentNoResultInfo: boolean;
5658
let _permanentShowResultInTab: boolean;
5759
let _state: any;
@@ -204,6 +206,7 @@ function _generateHelpHTML(): string {
204206
markdown += "| ---- | --------- |\n";
205207
markdown += "| `$asString(val: any): string` | Returns a value as string. |\n";
206208
markdown += "| `$cwd(newPath?: string, permanent?: boolean = false): string` | Gets or sets the current working directory for the execution. |\n";
209+
markdown += "| `$disableHexView(flag?: boolean, permanent?: boolean = false): boolean` | Gets or sets if 'hex view' for binary results should be disabled or not. |\n";
207210
markdown += "| `$eval(code: string): any` | Executes code from execution / extension context. |\n";
208211
markdown += "| `$error(msg: string): vscode.Thenable<any>` | Shows an error popup. |\n";
209212
markdown += "| `$execute(scriptPath: string, ...args: any[]): any` | Executes a script ([module](https://mkloubert.github.io/vs-script-commands/interfaces/_quick_.scriptmodule.html)). |\n";
@@ -224,6 +227,7 @@ function _generateHelpHTML(): string {
224227
markdown += "| `$require(id: string): any` | Loads a module from execution / extension context. |\n";
225228
markdown += "| `$setState(newValue: any): any` | Sets the value of `$state` variable and returns the new value. |\n";
226229
markdown += "| `$showResultInTab(flag?: boolean, permanent?: boolean = false): boolean` | Gets or sets if result should be shown in a tab window or a popup. |\n";
230+
markdown += "| `$toHexView(val: any): string` | Converts a value, like a buffer or string, to 'hex view'. |\n";
227231
markdown += "| `$unlink(path: string): boolean` | Removes a file or folder. |\n";
228232
markdown += "| `$warn(msg: string): vscode.Thenable<any>` | Shows a warning popup. |\n";
229233
markdown += "| `$writeFile(path: string, data: any): void` | Writes data to a file. |\n";
@@ -234,8 +238,10 @@ function _generateHelpHTML(): string {
234238
markdown += "## Variables\n";
235239
markdown += "| Name | Description |\n";
236240
markdown += "| ---- | --------- |\n";
241+
markdown += "| `$config: Configuration` | The current [settings](https://mkloubert.github.io/vs-script-commands/interfaces/_contracts_.configuration.html) of that extension. |\n";
237242
markdown += "| `$extension: vscode.ExtensionContext` | Stores the [context](https://code.visualstudio.com/docs/extensionAPI/vscode-api#_a-nameextensioncontextaspan-classcodeitem-id1016extensioncontextspan) of that extension. |\n";
238243
markdown += "| `$globals: any` | Stores the global data from the settings. |\n";
244+
markdown += "| `$me: ScriptCommandController` | The [controller](https://mkloubert.github.io/vs-script-commands/classes/_controller_.scriptcommandcontroller.html) of that extension. |\n";
239245
markdown += "| `$output: vscode.OutputChannel` | Stores the [output channel](https://code.visualstudio.com/docs/extensionAPI/vscode-api#OutputChannel) of that extension. |\n";
240246
markdown += "| `$state: any` | Stores a value that should be available for next executions. |\n";
241247
markdown += "| `$workspace: string` | Stores the path of the current workspace. |\n";
@@ -252,7 +258,9 @@ function _generateHelpHTML(): string {
252258
return html;
253259
}
254260

255-
function _generateHTMLForResult(expr: string, result: any): string {
261+
function _generateHTMLForResult(expr: string, result: any, disableHexView: boolean): string {
262+
disableHexView = sc_helpers.toBooleanSafe(disableHexView);
263+
256264
let htmlEncoder = new HtmlEntities.AllHtmlEntities();
257265

258266
let html = '';
@@ -266,6 +274,14 @@ function _generateHTMLForResult(expr: string, result: any): string {
266274
if (sc_helpers.isNullOrUndefined(result)) {
267275
strResult = '' + result;
268276
}
277+
else if (Buffer.isBuffer(result)) {
278+
if (disableHexView) {
279+
strResult = result.toString('hex');
280+
}
281+
else {
282+
strResult = Hexy.hexy(result);
283+
}
284+
}
269285
else {
270286
strResult = JSON.stringify(result, null, 4);
271287
}
@@ -314,7 +330,8 @@ function _generateHTMLForResult(expr: string, result: any): string {
314330
* Does a "quick execution".
315331
*/
316332
export function quickExecution() {
317-
let $me: sc_controller.ScriptCommandController = this;
333+
const $me: sc_controller.ScriptCommandController = this;
334+
const $config = sc_helpers.cloneObject($me.config);
318335
let $state = _state;
319336

320337
let _inputBoxValue = $me.context.workspaceState.get<string>('vsscLastQuickCommand');
@@ -327,6 +344,7 @@ export function quickExecution() {
327344
prompt: "The JavaScript expression to execute...",
328345
value: _inputBoxValue,
329346
}).then((_expr) => {
347+
let _disableHexView = _permanentDisableHexView;
330348
let _noResultInfo = _permanentNoResultInfo;
331349
let _showResultInTab = _permanentShowResultInTab;
332350
const _completed = (err: any, result?: any) => {
@@ -351,7 +369,7 @@ export function quickExecution() {
351369
if (_showResultInTab) {
352370
// show in tab
353371

354-
$me.openHtml(_generateHTMLForResult(_expr, result), '[vs-script-commands] Quick execution result').then(() => {
372+
$me.openHtml(_generateHTMLForResult(_expr, result, _disableHexView), '[vs-script-commands] Quick execution result').then(() => {
355373
}, (err) => {
356374
$me.log(`[ERROR] ScriptCommandController.quickExecution(4): ${sc_helpers.toStringSafe(err)}`);
357375
});
@@ -418,6 +436,18 @@ export function quickExecution() {
418436
return _currentDir;
419437
};
420438

439+
const $disableHexView = function(flag?: boolean, permanent = false): boolean {
440+
if (arguments.length > 0) {
441+
_disableHexView = sc_helpers.toBooleanSafe(flag);
442+
443+
if (sc_helpers.toBooleanSafe(permanent)) {
444+
_permanentDisableHexView = _disableHexView;
445+
}
446+
}
447+
448+
return _disableHexView;
449+
};
450+
421451
const $error = function(msg: string) {
422452
return vscode.window
423453
.showErrorMessage( sc_helpers.toStringSafe(msg) );
@@ -569,6 +599,9 @@ export function quickExecution() {
569599
return _showResultInTab;
570600
};
571601
let $thisArgs: any;
602+
const $toHexView = function(val: any): string {
603+
return Hexy.hexy(val);
604+
};
572605
const $unlink = function(path: string) {
573606
path = sc_helpers.toStringSafe(path);
574607
if (!Path.isAbsolute(path)) {
@@ -664,11 +697,13 @@ export function reset() {
664697
let cfg = me.config;
665698

666699
_permanentCurrentDirectory = undefined;
700+
_permanentDisableHexView = false;
667701
_permanentNoResultInfo = false;
668702
_state = undefined;
669703

670704
if (cfg.quick) {
671705
_permanentCurrentDirectory = cfg.quick.cwd;
706+
_permanentDisableHexView = sc_helpers.toBooleanSafe(cfg.quick.disableHexView);
672707
_permanentNoResultInfo = sc_helpers.toBooleanSafe(cfg.quick.noResultInfo);
673708
_permanentShowResultInTab = sc_helpers.toBooleanSafe(cfg.quick.showResultInTab);
674709
_state = sc_helpers.cloneObject(cfg.quick.state);

0 commit comments

Comments
 (0)