You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2025/08/10 03:44:09 UTC

(groovy) branch master updated (a67e98b9ab -> b9401c31eb)

This is an automated email from the ASF dual-hosted git repository.

paulk pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


    from a67e98b9ab update dependency metadata
     new 19f96256db GROOVY-8162: Update Groovysh to JLine3 (improve help output for /nano and /less)
     new b9401c31eb GROOVY-8162: Update Groovysh to JLine3 (initial version of /ls, /tail, and /head)

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../groovy/org/apache/groovy/groovysh/Main.groovy  | 101 ++++++++++++++++++++-
 .../groovy/groovysh/jline/GroovyBuiltins.groovy    |  36 +++++---
 2 files changed, 118 insertions(+), 19 deletions(-)


(groovy) 01/02: GROOVY-8162: Update Groovysh to JLine3 (improve help output for /nano and /less)

Posted by pa...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

paulk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 19f96256db08ae037e2bab89b21ba14344d2ad9f
Author: Paul King <pa...@asert.com.au>
AuthorDate: Sun Aug 10 13:42:56 2025 +1000

    GROOVY-8162: Update Groovysh to JLine3 (improve help output for /nano and /less)
---
 .../groovy/groovysh/jline/GroovyBuiltins.groovy    | 36 +++++++++++++---------
 1 file changed, 21 insertions(+), 15 deletions(-)

diff --git a/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/jline/GroovyBuiltins.groovy b/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/jline/GroovyBuiltins.groovy
index 7d63f72b93..b634448ba2 100644
--- a/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/jline/GroovyBuiltins.groovy
+++ b/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/jline/GroovyBuiltins.groovy
@@ -57,14 +57,17 @@ class GroovyBuiltins extends Builtins {
     }
 
     private void less(CommandInput input) {
-        Options opt = Options.compile(Less.usage()).parse(input.args())
-        boolean usingBuffer = opt.args().size() == 0
-        if (usingBuffer) {
-            def temp = File.createTempFile('groovysh', '.groovy')
-            temp.text = engine.buffer
-            input = new CommandInput(input.command(), [*input.args(), temp.absolutePath] as String[], input.terminal(), input.in(), input.out(), input.err())
-        }
+        Options opt = Options.compile(*Less.usage()*.replaceAll('less ', '/less ')).parse(input.args())
         try {
+            if (opt.isSet("help")) {
+                throw new Options.HelpException(opt.usage());
+            }
+            boolean usingBuffer = opt.args().size() == 0
+            if (usingBuffer) {
+                def temp = File.createTempFile('groovysh', '.groovy')
+                temp.text = engine.buffer
+                input = new CommandInput(input.command(), [*input.args(), temp.absolutePath] as String[], input.terminal(), input.in(), input.out(), input.err())
+            }
             Commands.less(input.terminal(), input.in(), input.out(), input.err(), workDir.get(), input.xargs(), configPath)
         } catch (Exception e) {
             saveException(e)
@@ -72,15 +75,18 @@ class GroovyBuiltins extends Builtins {
     }
 
     private void nano(CommandInput input) {
-        Options opt = Options.compile(Nano.usage()).parse(input.args())
-        boolean usingBuffer = opt.args().size() == 0
-        def temp = null
-        if (usingBuffer) {
-            temp = File.createTempFile('groovysh', '.groovy')
-            temp.text = engine.buffer
-            input = new CommandInput(input.command(), [*input.args(), temp.absolutePath] as String[], input.terminal(), input.in(), input.out(), input.err())
-        }
+        Options opt = Options.compile(*Nano.usage()*.replaceAll('nano ', '/nano ')).parse(input.args())
         try {
+            if (opt.isSet("help")) {
+                throw new Options.HelpException(opt.usage());
+            }
+            boolean usingBuffer = opt.args().size() == 0
+            def temp = null
+            if (usingBuffer) {
+                temp = File.createTempFile('groovysh', '.groovy')
+                temp.text = engine.buffer
+                input = new CommandInput(input.command(), [*input.args(), temp.absolutePath] as String[], input.terminal(), input.in(), input.out(), input.err())
+            }
             Commands.nano(input.terminal(), input.out(), input.err(), workDir.get(), input.args(), configPath)
             if (temp) GroovyCommands.loadFile(engine, temp)
         } catch (Exception e) {


(groovy) 02/02: GROOVY-8162: Update Groovysh to JLine3 (initial version of /ls, /tail, and /head)

Posted by pa...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

paulk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit b9401c31eba5c7c7f18e34ca82dcd4cb51d7aade
Author: Paul King <pa...@asert.com.au>
AuthorDate: Sun Aug 10 13:43:58 2025 +1000

    GROOVY-8162: Update Groovysh to JLine3 (initial version of /ls, /tail, and /head)
---
 .../groovy/org/apache/groovy/groovysh/Main.groovy  | 101 ++++++++++++++++++++-
 1 file changed, 97 insertions(+), 4 deletions(-)

diff --git a/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/Main.groovy b/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/Main.groovy
index 66ba3dcfa3..12306156e9 100644
--- a/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/Main.groovy
+++ b/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/Main.groovy
@@ -28,8 +28,11 @@ import org.apache.groovy.groovysh.jline.GroovySystemRegistry
 import org.apache.groovy.groovysh.util.DocFinder
 import org.codehaus.groovy.tools.shell.util.MessageSource
 import org.jline.builtins.ClasspathResourceUtil
+import org.jline.builtins.Completers
 import org.jline.builtins.ConfigurationPath
 import org.jline.builtins.Options
+import org.jline.builtins.PosixCommands
+import org.jline.builtins.PosixCommandsRegistry
 import org.jline.builtins.SyntaxHighlighter
 import org.jline.console.CommandInput
 import org.jline.console.CommandMethods
@@ -41,6 +44,7 @@ import org.jline.console.impl.JlineCommandRegistry
 import org.jline.console.impl.SystemHighlighter
 import org.jline.keymap.KeyMap
 import org.jline.reader.Binding
+import org.jline.reader.Completer
 import org.jline.reader.EndOfFileException
 import org.jline.reader.LineReader
 import org.jline.reader.LineReader.Option
@@ -49,6 +53,8 @@ import org.jline.reader.Reference
 import org.jline.reader.UserInterruptException
 import org.jline.reader.impl.DefaultParser
 import org.jline.reader.impl.DefaultParser.Bracket
+import org.jline.reader.impl.completer.ArgumentCompleter
+import org.jline.reader.impl.completer.NullCompleter
 import org.jline.terminal.Size
 import org.jline.terminal.Terminal
 import org.jline.terminal.Terminal.Signal
@@ -79,17 +85,37 @@ class Main {
     protected static class ExtraConsoleCommands extends JlineCommandRegistry implements CommandRegistry {
         private LineReader reader
         private final Supplier<Path> workDir
+        private final Map<String, Object> variables
+        private PosixCommandsRegistry posix
 
-        ExtraConsoleCommands(Supplier<Path> workDir) {
+        ExtraConsoleCommands(Supplier<Path> workDir, Map<String, Object> variables) {
             super()
             this.workDir = workDir
+            this.variables = variables
             registerCommands([
+                '/tail' : new CommandMethods((Function) this::tail, this::optFileCompleter),
+                '/head' : new CommandMethods((Function) this::head, this::optFileCompleter),
+                '/ls'   : new CommandMethods((Function) this::ls, this::optFileCompleter),
                 '/clear': new CommandMethods((Function) this::clear, this::defaultCompleter),
                 '/echo' : new CommandMethods((Function) this::echo, this::defaultCompleter),
                 "/!"    : new CommandMethods((Function) this::shell, this::defaultCompleter)
             ])
         }
 
+        private void init() {
+            def terminal = reader.terminal
+            posix = new PosixCommandsRegistry(
+                terminal.input(),
+                new PrintStream(terminal.output()),
+                new PrintStream(terminal.output()),
+                workDir.get(),
+                terminal,
+                variables::get)
+            posix.register('/tail', PosixCommands::tail)
+            posix.register('/head', PosixCommands::head)
+            posix.register('/ls', PosixCommands::ls)
+        }
+
         @Override
         String name() {
             'Console Commands'
@@ -100,7 +126,73 @@ class Main {
         }
 
         private Terminal terminal() {
-            return reader.terminal
+            return reader?.terminal
+        }
+
+        private List<Completer> optFileCompleter(String command) {
+            [new ArgumentCompleter(NullCompleter.INSTANCE, new Completers.OptionCompleter(new Completers.FilesCompleter(workDir), this::commandOptions, 1))]
+        }
+
+        private void tail(CommandInput input) {
+            def usage = new String[]{
+                "/tail - display last lines of files",
+                "Usage: /tail [-f] [-q] [-c # | -n #] [file ...]",
+                "  -? --help                    Show help",
+                "  -f --follow                  Do not stop at end of file",
+                "  -F --FOLLOW                  Follow and check for file renaming or rotation",
+                "  -n --lines=LINES             Number of lines to print",
+                "  -c --bytes=BYTES             Number of bytes to print",
+            }
+            try {
+                parseOptions(usage, input.args())
+                posix.execute('/tail', input.args())
+            } catch (Exception e) {
+                saveException(e)
+            }
+        }
+
+        private void head(CommandInput input) {
+            def usage = new String[]{
+                "/head - display first lines of files",
+                "Usage: /head [-n lines | -c bytes] [file ...]",
+                "  -? --help                    Show help",
+                "  -n --lines=LINES             Print line counts",
+                "  -c --bytes=BYTES             Print byte counts"
+            }
+            try {
+                parseOptions(usage, input.args())
+                posix.execute('/head', input.args())
+            } catch (Exception e) {
+                saveException(e)
+            }
+        }
+
+        private void ls(CommandInput input) {
+            def usage = new String[]{
+                "/ls - list files",
+                "Usage: /ls [OPTIONS] [PATTERNS...]",
+                "  -? --help                show help",
+                "  -1                       list one entry per line",
+                "  -C                       multi-column output",
+                "     --color=WHEN          colorize the output, may be `always', `never' or `auto'",
+                "  -a                       list entries starting with .",
+                "  -F                       append file type indicators",
+                "  -m                       comma separated",
+                "  -l                       long listing",
+                "  -S                       sort by size",
+                "  -f                       output is not sorted",
+                "  -r                       reverse sort order",
+                "  -t                       sort by modification time",
+                "  -x                       sort horizontally",
+                "  -L                       list referenced file for links",
+                "  -h                       print sizes in human readable form"
+            }
+            try {
+                parseOptions(usage, input.args())
+                posix.execute('/ls', input.args())
+            } catch (Exception e) {
+                saveException(e)
+            }
         }
 
         private void clear(CommandInput input) {
@@ -249,7 +341,7 @@ class Main {
             CommandRegistry builtins = new GroovyBuiltins(scriptEngine, workDir, configPath, (String fun) ->
                 new ConsoleEngine.WidgetCreator(consoleEngine, fun)
             )
-            def extra = new ExtraConsoleCommands(workDir)
+            def extra = new ExtraConsoleCommands(workDir, scriptEngine.variables)
 
             // Command line highlighter
             scriptEngine.put(GroovyEngine.NANORC_VALUE, rootURL.toString())
@@ -275,7 +367,7 @@ class Main {
                 if (!OSUtils.IS_WINDOWS) {
                     setSpecificHighlighter("/!", SyntaxHighlighter.build(jnanorc, "SH-REPL"))
                 }
-                addFileHighlight('/nano', '/less', '/slurp', '/load', '/save')
+                addFileHighlight('/nano', '/less', '/slurp', '/load', '/save', '/head', '/tail', '/ls')
                 addFileHighlight('/classloader', null, ['-a', '--add'])
                 addExternalHighlighterRefresh(printer::refresh)
                 addExternalHighlighterRefresh(scriptEngine::refresh)
@@ -302,6 +394,7 @@ class Main {
             }
 
             [consoleEngine, builtins, extra]*.setLineReader(reader)
+            extra.init()
 
             // widgets and console initialization
             new TailTipWidgets(reader, systemRegistry::commandDescription, 5, TipType.COMPLETER)