Skip to content

Commit

Permalink
nano & less refactoring: added usage() method
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Sep 24, 2019
1 parent 241332c commit f2f911c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 109 deletions.
56 changes: 2 additions & 54 deletions builtins/src/main/java/org/jline/builtins/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.nio.file.Files;
import java.nio.file.FileSystems;
import java.nio.file.Path;
Expand Down Expand Up @@ -109,34 +108,7 @@ public static void nano(Terminal terminal, PrintStream out, PrintStream err,
Path currentDir,
String[] argv,
ConfigurationPath configPath) throws Exception {
final String[] usage = {
"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.",
" -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.",
" -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.",
" -$ --softwrap Enable 'soft wrapping'. ",
" -a --atblanks Wrap lines at whitespace instead of always at the edge of the screen.",
" -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.",
" -Y --syntax=name The name of the syntax highlighting to use.",
" -z --suspend Enable the ability to suspend nano using the system's suspend keystroke (usually ^Z).",
" -v --view Don't allow the contents of the file to be altered: read-only mode.",
" -k --cutfromcursor Make the 'Cut Text' command cut from the current cursor position to the end of the line",
" -t --tempfile Save a changed buffer without prompting (when exiting with ^X).",
" -H --historylog=name Log search strings to file, so they can be retrieved in later sessions"
};
Options opt = Options.compile(usage).parse(argv);
Options opt = Options.compile(Nano.usage()).parse(argv);
if (opt.isSet("help")) {
throw new HelpException(opt.usage());
}
Expand All @@ -155,34 +127,10 @@ public static void less(Terminal terminal, InputStream in, PrintStream out, Prin
Path currentDir,
String[] argv,
ConfigurationPath configPath) throws Exception {
final String[] usage = {
"less - file pager",
"Usage: less [OPTIONS] [FILES]",
" -? --help Show help",
" -e --quit-at-eof Exit on second EOF",
" -E --QUIT-AT-EOF Exit on EOF",
" -F --quit-if-one-screen Exit if entire file fits on first screen",
" -q --quiet --silent Silent mode",
" -Q --QUIET --SILENT Completely silent",
" -S --chop-long-lines Do not fold long lines",
" -i --ignore-case Search ignores lowercase case",
" -I --IGNORE-CASE Search ignores all case",
" -x --tabs=N[,...] Set tab stops",
" -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",
" --ignorercfiles Don't look at the system's lessrc nor at the user's lessrc.",
" -H --historylog=name Log search strings to file, so they can be retrieved in later sessions"

};

Options opt = Options.compile(usage).parse(argv);

Options opt = Options.compile(Less.usage()).parse(argv);
if (opt.isSet("help")) {
throw new HelpException(opt.usage());
}

Less less = new Less(terminal, currentDir, opt, configPath);
List<Source> sources = new ArrayList<>();
if (opt.args().isEmpty()) {
Expand Down
24 changes: 24 additions & 0 deletions builtins/src/main/java/org/jline/builtins/Less.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,30 @@ public class Less {
private List<Path> syntaxFiles = new ArrayList<>();
private boolean highlight = true;

public static String[] usage() {
final String[] usage = {
"less - file pager",
"Usage: less [OPTIONS] [FILES]",
" -? --help Show help",
" -e --quit-at-eof Exit on second EOF",
" -E --QUIT-AT-EOF Exit on EOF",
" -F --quit-if-one-screen Exit if entire file fits on first screen",
" -q --quiet --silent Silent mode",
" -Q --QUIET --SILENT Completely silent",
" -S --chop-long-lines Do not fold long lines",
" -i --ignore-case Search ignores lowercase case",
" -I --IGNORE-CASE Search ignores all case",
" -x --tabs=N[,...] Set tab stops",
" -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",
" --ignorercfiles Don't look at the system's lessrc nor at the user's lessrc.",
" -H --historylog=name Log search strings to file, so they can be retrieved in later sessions"
};
return usage;
}

public Less(Terminal terminal, Path currentDir) {
this(terminal, currentDir, null);
}
Expand Down
31 changes: 31 additions & 0 deletions builtins/src/main/java/org/jline/builtins/Nano.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,37 @@ protected enum WriteFormat {
MAC
}

public static String[] usage() {
final String[] usage = {
"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.",
" -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.",
" -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.",
" -$ --softwrap Enable 'soft wrapping'. ",
" -a --atblanks Wrap lines at whitespace instead of always at the edge of the screen.",
" -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.",
" -Y --syntax=name The name of the syntax highlighting to use.",
" -z --suspend Enable the ability to suspend nano using the system's suspend keystroke (usually ^Z).",
" -v --view Don't allow the contents of the file to be altered: read-only mode.",
" -k --cutfromcursor Make the 'Cut Text' command cut from the current cursor position to the end of the line",
" -t --tempfile Save a changed buffer without prompting (when exiting with ^X).",
" -H --historylog=name Log search strings to file, so they can be retrieved in later sessions"
};
return usage;
}

protected class Buffer {
String file;
Charset charset;
Expand Down
57 changes: 2 additions & 55 deletions demo/src/main/java/org/apache/felix/gogo/jline/Posix.java
Original file line number Diff line number Diff line change
Expand Up @@ -853,33 +853,7 @@ protected void ttop(final CommandSession session, Process process, String[] argv
}

protected void nano(final CommandSession session, Process process, String[] argv) throws Exception {
final String[] usage = {
"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.",
" -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.",
" -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.",
" -$ --softwrap Enable 'soft wrapping'. ",
" -a --atblanks Wrap lines at whitespace instead of always at the edge of the screen.",
" -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.",
" -Y --syntax=name The name of the syntax highlighting to use.",
" -z --suspend Enable the ability to suspend nano using the system's suspend keystroke (usually ^Z).",
" -v --view Don't allow the contents of the file to be altered: read-only mode.",
" -k --cutfromcursor Make the 'Cut Text' command cut from the current cursor position to the end of the line",
" -t --tempfile Save a changed buffer without prompting (when exiting with ^X)."
};
Options opt = parseOptions(session, usage, argv);
Options opt = parseOptions(session, Nano.usage(), argv);
Nano edit = new Nano(Shell.getTerminal(session), session.currentDir(), opt);
edit.open(opt.args());
edit.run();
Expand Down Expand Up @@ -943,34 +917,7 @@ protected void watch(final CommandSession session, Process process, String[] arg
}

protected void less(CommandSession session, Process process, String[] argv) throws Exception {
String[] usage = {
"less - file pager",
"Usage: less [OPTIONS] [FILES]",
" -? --help Show help",
" -e --quit-at-eof Exit on second EOF",
" -E --QUIT-AT-EOF Exit on EOF",
" -F --quit-if-one-screen Exit if entire file fits on first screen",
" -q --quiet --silent Silent mode",
" -Q --QUIET --SILENT Completely silent",
" -S --chop-long-lines Do not fold long lines",
" -i --ignore-case Search ignores lowercase case",
" -I --IGNORE-CASE Search ignores all case",
" -x --tabs=N[,...] Set tab stops",
" -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",
" --ignorercfiles Don't look at the system's lessrc nor at the user's lessrc."

};
try {
Less.class.getField("quitIfOneScreen");
} catch (NoSuchFieldException e) {
List<String> ustrs = new ArrayList<>(Arrays.asList(usage));
ustrs.removeIf(s -> s.contains("--quit-if-one-screen") || s.contains("--no-init") || s.contains("--no-keypad"));
usage = ustrs.toArray(new String[ustrs.size()]);
}
Options opt = parseOptions(session, usage, argv);
Options opt = parseOptions(session, Less.usage(), argv);
List<Source> sources = new ArrayList<>();
if (opt.args().isEmpty()) {
opt.args().add("-");
Expand Down

0 comments on commit f2f911c

Please sign in to comment.