sieukrem / jn-npp-plugin

Plugin for Notepad++ allowing you to automate some tasks using JavaScript
https://github.com/sieukrem/jn-npp-plugin/wiki
110 stars 24 forks source link

Get CMD responses #66

Closed deasserted closed 5 years ago

deasserted commented 6 years ago

I love this add-on! I am using it to make a Git interface, following the Clearcase.js example. It is mostly done for a first version.

I hit trouble when I need to get responses to my queries: for example, I want to disable the menu if the file is not in a Git repository. The Clearcase example used helperCC = new ActiveXObject("ClearCase.Application"); but I can't figure out an equivalent for Git, so I considered using git rev-parse --git-dir > /dev/null 2>&1 But I don't know how to get that result / ERRORLEVEL.

Similarly, I want to display the current branch name, but I need to capture a git status -b response to do so. I found suggestions of shell.exec on the web, but they don't seem to work (can't create the shell object). I found that I could use | clip and read it from System.clipBoard, but I had to add a delay (alert) to get clipboard updating to be fast enough to be reliable in that context. Is there an easier way?

sieukrem commented 6 years ago

I am glad to hear that you like jN. For your purpose you can try following snippet.

var shell = new ActiveXObject("WScript.Shell");
shell.CurrentDirectory = currentView.files[currentView.file].replace(/[^\\\/]+$/,"");
var cmd = shell.Exec("cmd /c git branch 2>&1");

alert(cmd.StdOut.ReadAll());

The problem with Exec method is that you will see every time a cmd box. You can also use Run method, which is blocking and without access to StdOut stream, you will need to pipe the cmd output in to a file and then read it. Run method returns Errorlevel directly. Exec returns an object providing access to ExitCode

var shell = new ActiveXObject("WScript.Shell");
shell.CurrentDirectory = firstView.files[firstView.file].replace(/[^\\\/]+$/,"");
var errorlevel = shell.Run("cmd /c git branch 2>&1|clip",0,true);

alert("ErrorLevel "+errorlevel+", braches="+System.clipBoard);

Runmethod and |clip in combination work fine, but don't forget to restore original clipBoard to avoid astonishment.

KOLANICH commented 6 years ago

@deasserted, it's a bit offttop, but you may also be interested in https://github.com/KOLANICH/jN-npp-scripts/blob/master/includes/ktools.js , it is a framework doing some work like checking presence of binaries and construucting command line, see https://github.com/KOLANICH/jN-npp-scripts/blob/master/includes/kTools/PHPLint.js , https://github.com/KOLANICH/jN-npp-scripts/blob/master/includes/kTools/PHPCodeStyleFixer.js and https://github.com/KOLANICH/jN-npp-scripts/blob/master/includes/kTools/KaitaiStructCompiler.js for examples

deasserted commented 6 years ago

Sorry to be slow to reply -- too much going on.

Your snippets work! I get odd responses when I change it; I will continue to fiddle with them to figure out what I messed up. For this code, I expect a version number, but I get the following, when I expect no error:

ErrorLevel 0, Microsoft (R) Windows Script Host Version 5.8 ... Copyright (C) Microsoft Corporation. All rights reserved. C:\Program Files (x86)\Notepad++\plugins\jN\includes\git.js(1, 1) Microsoft JScript runtime error: Object expected Code: var shell = new ActiveXObject("WScript.Shell"); shell.CurrentDirectory = currentView.files[currentView.file].replace(/[^\\\/]+$/,""); var errorlevel = shell.Run("cmd /c git --version 2>&1|clip",0,true); // "is Git installed" alert("ErrorLevel "+errorlevel+", \n"+System.clipBoard);

sieukrem commented 6 years ago

The problem is that you have as current file one from includes folder, where your git.js is. Try cmd /c git.exe .... or use cmd /c where git to check if git is known program.