Skip to content
Permalink
Browse files

Expand edit-prefs command a bit

  • Loading branch information...
LadyCailin committed Oct 3, 2019
1 parent ff15ef9 commit c7f4aab2c821be66566737cf24d5e38e2c1e2662
@@ -451,6 +451,7 @@ Argument build() {
* A description of the command itself.
*/
String description = "";
String extendedDescription = "";
/**
* The model for the arguments
*/
@@ -1067,6 +1068,17 @@ public ArgumentParser addDescription(String description) {
return this;
}

/**
* Provides an extended description. This is not returned with {@link #getDescription()}, but is included
* as part of the built description.
* @param extendedDescription
* @return
*/
public ArgumentParser addExtendedDescription(String extendedDescription) {
this.extendedDescription = extendedDescription;
return this;
}

/**
* If set to true (which is the default), then unknown options will cause an error. If false,
* they will not cause an error, and can even still be accessed in the results. However, this is
@@ -1100,6 +1112,9 @@ public String getBuiltDescription() {
//Now, we need to go through and get all the switch names in alphabetical
//order.
b.append("\t").append(this.description).append("\n\n");
if(!this.extendedDescription.equals("")) {
b.append("\t").append(this.extendedDescription).append("\n\n");
}
List<Character> shortCodes = new ArrayList<>();
List<String> longCodes = new ArrayList<>();
List<Character> shortCodesDone = new ArrayList<>();
@@ -1284,10 +1299,9 @@ public String getBuiltDescription() {
}

/**
* Returns just the description that was registered with {
* Returns just the description that was registered with {@link #addDescription(java.lang.String)}
*
* @see #addDescription(String)}.
* @return The description, or null, if one has not been set yet.
* @return The description, or empty string, if one has not been set yet.
* @see #getBuiltDescription()
*/
public String getDescription() {
@@ -1642,32 +1642,61 @@ public void execute(ArgumentParser.ArgumentParserResults parsedArgs) throws Exce

}

public static String GetDirectoryTextEditor() {
String cmd;
if(OSUtils.GetOS().isWindows()) {
cmd = "code.cmd";
} else {
cmd = "vim";
}

if(OSUtils.GetOS().isUnixLike()) {
if(System.getenv("EDITOR") != null) {
cmd = System.getenv("EDITOR");
}
if(System.getenv("VISUAL") != null) {
cmd = System.getenv("VISUAL");
}
}


if(System.getenv("MS_EDITOR") != null && !System.getenv("MS_EDITOR").equals("")) {
cmd = System.getenv("MS_EDITOR");
}

return cmd;
}

@tool("edit-prefs")
public static class EditPrefsMode extends AbstractCommandLineTool {

@Override
public ArgumentParser getArgumentParser() {
return ArgumentParser.GetParser()
.addDescription("Launches the prefs directory in a default text editor, or your defined editor."
+ " The default varies based on OS and installed/detectible editors.")
.addDescription("Launches the prefs directory in a default text editor, or your defined editor.")
.addExtendedDescription("By default, on Windows, \"code.cmd\" (Visual Studio Code)"
+ " is the default editor. On"
+ " Linux systems, vim is default, though if $EDITOR is set, that is used, or if"
+ " $VISUAL is also set, that is used instead. In all OSes, you can override the"
+ " default editor with the MS_EDITOR environment variable. The prefs folder is passed"
+ " as the last argument to the command. Passing in the editor to the command will"
+ " bypass all these mechanisms, and use the specified editor in that one launch.")
.addArgument(new ArgumentBuilder()
.setDescription("Waits for the editor to finish. This is implied for some known programs,"
+ " where that is necessary (for instance known command line editors) but may be"
+ " specified manually. This is generally not necessary for GUI editors that"
+ " where that is necessary (" + StringUtils.Join(NEEDS_WAIT, ", ", ", and ", " and ")
+ ") but may be specified manually. This is generally not necessary for GUI editors that"
+ " open in a new window.")
.asFlag()
.setName("wait"))
.addArgument(new ArgumentBuilder()
.setDescription("Uses a different command to open the editor. This overrides the environment"
+ " value (if set), but uses the same format. The value should follow the format"
+ " \"command %s\" where %s is replaced with the path to the prefs directory for"
+ " this installation. Instead of setting this in command line mode, you can also"
+ " set the \"MS_EDITOR\" environment variable.")
+ " value (if set).")
.setUsageName("command")
.setOptionalAndDefault()
.setArgType(ArgumentBuilder.BuilderTypeNonFlag.STRING)
.setDefaultVal(""));
}
private static final String[] NEEDS_WAIT = new String[] {"vim", "nano", "emacs"};

@Override
public void execute(ArgumentParser.ArgumentParserResults parsedArgs) throws Exception {
@@ -1679,23 +1708,14 @@ public void execute(ArgumentParser.ArgumentParserResults parsedArgs) throws Exce
}
String cmd;
boolean wait = false;
String[] needsWait = new String[] {"vim", "nano"};
if(OSUtils.GetOS() == OSUtils.OS.WINDOWS) {
cmd = "code.cmd %s";
} else {
cmd = "vim %s";
}


if(System.getenv("MS_EDITOR") != null && !System.getenv("MS_EDITOR").equals("")) {
cmd = System.getenv("MS_EDITOR");
}
cmd = GetDirectoryTextEditor() + " %s";
if(!parsedArgs.getStringArgument().equals("")) {
cmd = parsedArgs.getStringArgument();
}

for(String nw : needsWait) {
if(cmd.startsWith(nw)) {
for(String nw : NEEDS_WAIT) {
if(cmd.startsWith(nw + " ")) {
wait = true;
break;
}
@@ -1704,7 +1724,11 @@ public void execute(ArgumentParser.ArgumentParserResults parsedArgs) throws Exce
CommandExecutor c = new CommandExecutor(String.format(cmd, "\"" + MethodScriptFileLocations.getDefault()
.getPreferencesDirectory()) + "\"");
c.setSystemInputsAndOutputs();
c.start();
try {
c.start();
} catch (IOException ex) {
System.err.println("Could not launch editor: " + ex.getMessage());
}
if(parsedArgs.isFlagSet("wait") || wait) {
c.waitFor();
}

0 comments on commit c7f4aab

Please sign in to comment.
You can’t perform that action at this time.