Skip to content

Commit

Permalink
nano command: added commandline options
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Sep 17, 2019
1 parent 0af3036 commit fd8743f
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 24 deletions.
19 changes: 18 additions & 1 deletion builtins/src/main/java/org/jline/builtins/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,22 @@ public static void nano(Terminal terminal, PrintStream out, PrintStream err,
"nano - edit files",
"Usage: nano [OPTIONS] [FILES]",
" -? --help Show help",
" -B --backup When saving a file, back up the previous version of it, using the current filename",
" suffixed with a tilde (~)." ,
" -I --ignorercfiles Don't look at the system's nanorc nor at the user's nanorc." ,
" -Q --quotestr=regex Set the regular expression for matching the quoting part of a line. The default value",
" is \"^([ \t]*([!#%:;>|}]|//))+\".",
" -T --tabsize=number Set the size (width) of a tab to number columns.",
" -U --quickblank Do quick status-bar blanking: status-bar messages will disappear after 1 keystroke",
" instead of 25.",
" -c --constantshow Constantly show the cursor position on the status bar.",
" -e --emptyline Do not use the line below the title bar, leaving it entirely blank.",
" -j --jumpyscrolling Scroll the buffer contents per half-screen instead of per line.",
" -l --linenumbers Display line numbers to the left of the text area.",
" -m --mouse Enable mouse support, if available for your system. When enabled, mouse clicks can be",
" used to place the cursor, set the mark (with a double click), and execute shortcuts.",
" -$ --softwrap Enable 'soft wrapping'. This will make nano attempt to display the entire contents of any",
" line, even if it is longer than the screen width, by continuing it over multiple screen lines.",
" -R --restricted Restricted mode: don't allow suspending; don't allow a file to be appended to,",
" prepended to, or saved under a different name if it already has one;",
" and don't use backup files.",
Expand Down Expand Up @@ -149,7 +165,8 @@ public static void less(Terminal terminal, InputStream in, PrintStream out, Prin
" -N --LINE-NUMBERS Display line number for each line",
" -Y --syntax=name The name of the syntax highlighting to use.",
" --no-init Disable terminal initialization",
" --no-keypad Disable keypad handling"
" --no-keypad Disable keypad handling",
" --ignorercfiles Don't look at the system's lessrc nor at the user's lessrc."

};

Expand Down
27 changes: 14 additions & 13 deletions builtins/src/main/java/org/jline/builtins/Less.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public class Less {
protected String displayPattern;

protected final Size size = new Size();

SyntaxHighlighter syntaxHighlighter;
private List<Path> syntaxFiles = new ArrayList<>();
private boolean highlight = true;
Expand All @@ -123,13 +123,14 @@ public Less(Terminal terminal, Path currentDir, Options opts, Path lessrc) {
this.display = new Display(terminal, true);
this.bindingReader = new BindingReader(terminal.reader());
this.currentDir = currentDir;
if (lessrc != null && lessrc.toFile().exists()) {
boolean ignorercfiles = opts!=null && opts.isSet("ignorercfiles");
if (lessrc != null && lessrc.toFile().exists() && !ignorercfiles) {
try {
parseConfig(lessrc);
} catch (IOException e) {
errorMessage = "Encountered error while reading config file: " + lessrc;
}
} else if (new File("/usr/share/nano").exists()) {
} else if (new File("/usr/share/nano").exists() && !ignorercfiles) {
PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:/usr/share/nano/*.nanorc");
try {
Files.find(Paths.get("/usr/share/nano"), Integer.MAX_VALUE, (path, f) -> pathMatcher.matches(path))
Expand Down Expand Up @@ -180,7 +181,7 @@ public Less(Terminal terminal, Path currentDir, Options opts, Path lessrc) {
}
}
}

private void parseConfig(Path file) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(file.toFile()));
String line = reader.readLine();
Expand All @@ -200,23 +201,23 @@ private void parseConfig(Path file) throws IOException {
&& (parts.get(0).equals("set") || parts.get(0).equals("unset"))) {
String option = parts.get(1);
boolean val = parts.get(0).equals("set");
if (option.equals("quitatfirsteof")) {
if (option.equals("QUIT-AT-EOF")) {
quitAtFirstEof = val;
} else if (option.equals("quitatsecondeof")) {
} else if (option.equals("quit-at-eof")) {
quitAtSecondEof = val;
} else if (option.equals("quitifonescreen")) {
} else if (option.equals("quit-if-one-screen")) {
quitIfOneScreen = val;
} else if (option.equals("quiet")) {
} else if (option.equals("quiet") || option.equals("silent")) {
quiet = val;
} else if (option.equals("veryquiet")) {
} else if (option.equals("QUIET") || option.equals("SILENT")) {
veryQuiet = val;
} else if (option.equals("choplonglines")) {
} else if (option.equals("chop-long-lines")) {
chopLongLines = val;
} else if (option.equals("ignorecaseallways")) {
} else if (option.equals("IGNORE-CASE")) {
ignoreCaseAlways = val;
} else if (option.equals("ignorecasecond")) {
} else if (option.equals("ignore-case")) {
ignoreCaseCond = val;
} else if (option.equals("linenumbers")) {
} else if (option.equals("LINE-NUMBERS")) {
printLineNumbers = val;
} else {
errorMessage = "Less config: Unknown or unsupported configuration option " + option;
Expand Down
55 changes: 46 additions & 9 deletions builtins/src/main/java/org/jline/builtins/Nano.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,28 +82,29 @@ public class Nano implements Editor {

// Configuration
public String title = "JLine Nano 3.0.0";
public boolean printLineNumbers = true;
public boolean wrapping = true;
public boolean printLineNumbers = false;
public boolean wrapping = false;
public boolean smoothScrolling = true;
public boolean mouseSupport = false;
public boolean oneMoreLine = true;
public boolean constantCursor;
public boolean constantCursor = false;
public boolean quickBlank = false;
public int tabs = 1; // tabs are not currently supported!
public String brackets = "\"’)>]}";
public String matchBrackets = "(<[{)>]}";
public String punct = "!.?";
public String quoteStr = "^([ \\t]*[#:>\\|}])+";
private boolean restricted;
private boolean restricted = false;
private String syntaxName;
private List<Path> syntaxFiles = new ArrayList<>();
private boolean writeBackup = false;

// Input
protected final List<Buffer> buffers = new ArrayList<>();
protected int bufferIndex;
protected Buffer buffer;

protected String message;
protected String errorMessage = null;
protected int nbBindings = 0;

protected LinkedHashMap<String, String> shortcuts;
Expand All @@ -118,14 +119,13 @@ public class Nano implements Editor {
protected List<String> searchTerms = new ArrayList<>();
protected int searchTermId = -1;
protected WriteMode writeMode = WriteMode.WRITE;
protected boolean writeBackup;
protected List<String> cutbuffer = new ArrayList<>();
protected boolean cut2end = false;
protected boolean mark = false;
protected boolean highlight = true;
private List<Path> syntaxFiles = new ArrayList<>();

protected boolean readNewBuffer = true;
protected String errorMessage = null;

protected enum WriteMode {
WRITE,
Expand Down Expand Up @@ -1583,13 +1583,14 @@ public Nano(Terminal terminal, Path root, Options opts, Path nanorc) {
this.size = new Size();
this.vsusp = terminal.getAttributes().getControlChar(ControlChar.VSUSP);
bindKeys();
if (nanorc != null && nanorc.toFile().exists()) {
boolean ignorercfiles = opts!=null && opts.isSet("ignorercfiles");
if (nanorc != null && nanorc.toFile().exists() && !ignorercfiles) {
try {
parseConfig(nanorc);
} catch (IOException e) {
errorMessage = "Encountered error while reading config file: " + nanorc;
}
} else if (new File("/usr/share/nano").exists()) {
} else if (new File("/usr/share/nano").exists() && !ignorercfiles) {
PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:/usr/share/nano/*.nanorc");
try {
Files.find(Paths.get("/usr/share/nano"), Integer.MAX_VALUE, (path, f) -> pathMatcher.matches(path))
Expand All @@ -1601,6 +1602,36 @@ public Nano(Terminal terminal, Path root, Options opts, Path nanorc) {
if (opts != null) {
this.restricted = opts.isSet("restricted");
this.syntaxName = opts.isSet("syntax") ? opts.get("syntax") : null;
if (opts.isSet("backup")) {
writeBackup = true;
}
if (opts.isSet("quotestr")) {
quoteStr = opts.get("quotestr");
}
if (opts.isSet("tabsize")) {
tabs = opts.getNumber("tabsize");
}
if (opts.isSet("quickblank")) {
quickBlank = true;
}
if (opts.isSet("constantshow")) {
constantCursor = true;
}
if (opts.isSet("emptyline")) {
oneMoreLine = false;
}
if (opts.isSet("jumpyscrolling")) {
smoothScrolling = false;
}
if (opts.isSet("linenumbers")) {
printLineNumbers = true;
}
if (opts.isSet("mouse")) {
mouseSupport = true;
}
if (opts.isSet("softwrap")) {
wrapping = true;
}
}
}

Expand Down Expand Up @@ -2393,6 +2424,9 @@ void help(String help) {
this.printLineNumbers = false;
this.constantCursor = false;
this.buffer = newBuf;
if (!oldWrapping) {
buffer.computeAllOffsets();
}
try {
this.message = null;
terminal.puts(Capability.cursor_invisible);
Expand Down Expand Up @@ -2438,6 +2472,9 @@ void help(String help) {
this.constantCursor = oldConstantCursor;
this.shortcuts = oldShortcuts;
terminal.puts(Capability.cursor_visible);
if (!oldWrapping) {
buffer.computeAllOffsets();
}
}
}

Expand Down
20 changes: 19 additions & 1 deletion demo/src/main/java/org/apache/felix/gogo/jline/Posix.java
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,22 @@ protected void nano(final CommandSession session, Process process, String[] argv
"nano - edit files",
"Usage: nano [OPTIONS] [FILES]",
" -? --help Show help",
" -B --backup When saving a file, back up the previous version of it, using the current filename",
" suffixed with a tilde (~)." ,
" -I --ignorercfiles Don't look at the system's nanorc nor at the user's nanorc." ,
" -Q --quotestr=regex Set the regular expression for matching the quoting part of a line. The default value",
" is \"^([ \t]*([!#%:;>|}]|//))+\".",
" -T --tabsize=number Set the size (width) of a tab to number columns.",
" -U --quickblank Do quick status-bar blanking: status-bar messages will disappear after 1 keystroke",
" instead of 25.",
" -c --constantshow Constantly show the cursor position on the status bar.",
" -e --emptyline Do not use the line below the title bar, leaving it entirely blank.",
" -j --jumpyscrolling Scroll the buffer contents per half-screen instead of per line.",
" -l --linenumbers Display line numbers to the left of the text area.",
" -m --mouse Enable mouse support, if available for your system. When enabled, mouse clicks can be",
" used to place the cursor, set the mark (with a double click), and execute shortcuts.",
" -$ --softwrap Enable 'soft wrapping'. This will make nano attempt to display the entire contents of any",
" line, even if it is longer than the screen width, by continuing it over multiple screen lines.",
" -R --restricted Restricted mode: don't allow suspending; don't allow a file to be appended to,",
" prepended to, or saved under a different name if it already has one;",
" and don't use backup files.",
Expand Down Expand Up @@ -950,7 +966,9 @@ protected void less(CommandSession session, Process process, String[] argv) thro
" -N --LINE-NUMBERS Display line number for each line",
" -Y --syntax=name The name of the syntax highlighting to use.",
" --no-init Disable terminal initialization",
" --no-keypad Disable keypad handling"
" --no-keypad Disable keypad handling",
" --ignorercfiles Don't look at the system's lessrc nor at the user's lessrc."

};
try {
Less.class.getField("quitIfOneScreen");
Expand Down

1 comment on commit fd8743f

@mattirn
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.