Single title in JLine 3 help output - jline

How do I customize the help command in JLine 3? The help in my JLine 3 shell sample displays as:
manager> help
System:
exit exit from app/script
help command help
Builtins:
ShellCommandRegistry:
create Create some stuff with minimal fuss...
delete Deletes some stuff with minimal fuss...
list List some stuff with minimal fuss...
I'd like to replace the section titles ("System:", "Builtins:", and "ShellCommandRegistry:") with single "Commands:" title like:
manager> help
Commands:
exit exit from app/script
help command help
create Create some stuff with minimal fuss...
delete Deletes some stuff with minimal fuss...
list List some stuff with minimal fuss...
Any ideas how to control this in JLine 3?

At the moment it is not possible to customize command groupings.
Will be fixed in next JLine version (> 3.15.0):
Added help command options: --nogroups (--groups) and --info.
Default grouping behaviour can be controlled by setting
systemRegistry.setGroupCommandsInHelp(true/false)
.
groovy-repl> help --help
help - command help
Usage: help [TOPIC...]
-? --help Displays command help
--nogroups Commands are not grouped by registeries
-i --info List commands with a short command infos
groovy-repl>

Using jline 3.20.0:
I wrote my own Registry and registered the other into this one, like this:
MyAppCommands myAppCommands = new MyAppCommands(parser, terminal, workDir, null);
myAppCommands.setCommandRegistries(builtins, picocliCommands);
myAppCommands.register("help", myAppCommands);
where MyAppCommands extends SystemRegistryImpl. Then the help command shows only the class-name "MyAppCommands:"
Therefore if you name your registry class "Commands" you'll get your desired result!

Related

ZSH Autocomplete For Custom Script

I've been searching online for a lot of hours, puling an all nighter because i got so lost down this rabbit hole :D but i'm having trouble finding examples with working code.
I have a bash script that is called upon like the following examples:
foo-tools.sh switch <version number>
foo-tools.sh start
foo-tools.sh open-ports on
foo-tools.sh kube get logs
foo-tools.sh kube edit ing default-ingress -oyaml
I use zsh with oh-my-zsh as my shell.
I want to be able to type "foo-tools.sh " to list all availabe options and i want to be able to autocomplete an option with "foo-tools.sh k" meaning it should either list all availabe options starting with "foo-tools.sh k*" or if there is only one option autcomplete the command.
I found this code on some stackoverflow thread, it's the closes I've gotten to having it work fully:
#compdef foo-tools.sh
_foo_tools_sh() {
typeset -A opt_args
local alternatives
alternatives=(
'args:rv options:_foo_tools_sh_options'
)
_alternative $alternatives && return 0
return 1
}
_foo_tools_sh_options() {
local -a arguments=(
'start:Start VP'
'stop:Stop VP]'
'kube:Kube CMD'
'kube logs:Kube logs'
'info:Display info'
'get:Get CMD'
)
_sequence _describe 'cmds' arguments
return 1
}
# invoke the completion command during autoload
_foo_tools_sh "$#"
This works some what. When i type "foo-tools.sh " it shows me the options i configured:
all options displayed when TAB
But when i autocomplete one of the options with tab it adds a , after the command:
, added when TAB cmd
And the option with a space between the words, "kube logs" gets the space escaped when tabbed out:space escpaded + added ,
I also found these two commands, they goes in my .zshrc. The expansion is wierd, it ignores the commands that has spaces in them and it doesnt understand that the commands are nested.
I.e if typing foo-tools.sh open-ports it displays all the options, not just off/on.
local -a _foo_tools_sh=(
"start"
"stop"
"switch"
"install"
"kube"
"kube get"
"kube edit"
"open-ports on"
"open-ports off"
)
complete -W "${_foo_tools_sh}" 'foo-tools.sh'
Options not nestled
I would very much appreciate any help or insights in how to improve the script i have (or throw it out and replace it with something proper).

Get autocompletion list in bash variable

I'm working with a big software project with many build targets. When typing make <tab> <tab> it shows over 1000 possible make targets.
What I want is a bash script that filters those targets by certain rules. Therefore I would like to have this list of make targets in a bash variable.
make_targets=$(???)
[do something with make_targets]
make $make_targets
It would be best if I wouldn't have to change anything with my project.
How can I get such a List?
#yuyichao created a function to get autocomplete output:
comp() {
COMP_LINE="$*"
COMP_WORDS=("$#")
COMP_CWORD=${#COMP_WORDS[#]}
((COMP_CWORD--))
COMP_POINT=${#COMP_LINE}
COMP_WORDBREAKS='"'"'><=;|&(:"
# Don't really thing any real autocompletion script will rely on
# the following 2 vars, but on principle they could ~~~ LOL.
COMP_TYPE=9
COMP_KEY=9
_command_offset 0
echo ${COMPREPLY[#]}
}
Just run comp make '' to get the results, and you can manipulate that. Example:
$ comp make ''
test foo clean
You would need to overwrite / modify the completion function for make. On Ubuntu it is located at:
/usr/share/bash-completion/completions/make
(Other distributions may store the file at /etc/bash_completion.d/make)
If you don't want to change the completion behavior for the whole system, you might write a small wrapper script like build-project, which calls make. Then write a completion function for that mapper which is derived from make's one.

How to set processing_root in rp5rc

I'm attempting to install ruby processing. I followed this tutorial:
https://github.com/jashkenas/ruby-processing/wiki/Getting-Started
After I rake ( before I install jruby ), all of the tests fail. I get the following result before every print out and not sure how to fix it.
WARNING: you need to set PROCESSING_ROOT in ~/.rp5rc
Following the tipp by Oscar here is an easy Copy & Paste solution for Mac Users:
echo PROCESSING_ROOT: "/Applications/Processing.app/Contents/Java" > ~/.rp5rc
The instructions on the wiki have been updated since you asked this question.
As is now suggested, you can use this gist to create your .rp5rc file. Create a new sketch in Processing using the contents of SetProcessingRoot.pde in the gist, and it will suggest the correct PROCESSING_ROOT value for your system and create the file. Note that you'll have to delete the default text ("enter your processing root here") and enter the suggested (or another) path.
Or, if you know the correct PROCESSING_ROOT path for your system, do the following:
echo PROCESSING_ROOT: \"correct_path\" > ~/.rp5rc

Is there a Vim plugin for Ruby which provides a "switch to/from test" command outside of Rails?

Tim Pope's rails.vim provides a command :A (and a set of related commands) which opens the "alternate" file. For most classes, that's the test, and for the test, the class.
It would sure be nice to have that functionality in non-Rails Ruby projects. Is there a plugin which provides that? Bonus points if it helps me create the test file when I create the implementation file. :)
Our hero tpope wrote rake.vim too. It does the very same things rails.vim does but in Ruby projects.
I created the following command that makes it possible to do
:E /pattern/replace
to jump to the file that is the current filename and substituting pattern by replace
For example, if your tests files are in /test/code.js and your src files in /src/code.js you could write the following command:
command! -nargs=* Es :call EditSubstitute("/test/src")
command! -nargs=* Et :call EditSubstitute("/src/test")
to have the command :Es to jump from testfile to source file and the command :Et to jump from source file to testfile.
Here's the function that does that :
function! EditSubstitute(args)
if (len(a:args))<2
return
endif
let s:delimiter = (a:args[0])
let s:split = split(a:args,s:delimiter,1)[1:]
let s:fullpath = expand('%:p')
let s:bar = substitute(s:fullpath, s:split[0], s:split[1], "")
echo (s:bar)
silent execute('edit '.s:bar)
endfunction
command! -nargs=* E :call EditSubstitute(<q-args>)
I know this doesn't really answer your question at all... but I use VIM buffers to provide easy accessibility to a file and its tests.
I keep my test on top, and the file on the bottom. Then I can view both at the same time.
I use NERDTree to make browsing easier too, but that is not a per-requisite.
You can get a full reference of what I use here:
https://github.com/coderjoe/dotfiles
If you like it I'd recommend NOT using my dotfiles from the above repo, but start with something like RyanB's dotfiles and build your own sets based on your own preferences. :)
Have a look at the vimrc of the guy from 'Destroy all software' https://github.com/garybernhardt/dotfiles/blob/master/.vimrc#L280
pressing <leader>. will switch you between your code and the spec code.
-frbl

Uncaught Throw generated by JLink or UseFrontEnd

This example routine generates two Throw::nocatch warning messages in the kernel window. Can they be handled somehow?
The example consists of this code in a file "test.m" created in C:\Temp:
Needs["JLink`"];
$FrontEndLaunchCommand = "Mathematica.exe";
UseFrontEnd[NotebookWrite[CreateDocument[], "Testing"]];
Then these commands pasted and run at the Windows Command Prompt:
PATH = C:\Program Files\Wolfram Research\Mathematica\8.0\;%PATH%
start MathKernel -noprompt -initfile "C:\Temp\test.m"
Addendum
The reason for using UseFrontEnd as opposed to UsingFrontEnd is that an interactive front end may be required to preserve output and messages from notebooks that are usually run interactively. For example, with C:\Temp\test.m modified like so:
Needs["JLink`"];
$FrontEndLaunchCommand="Mathematica.exe";
UseFrontEnd[
nb = NotebookOpen["C:\\Temp\\run.nb"];
SelectionMove[nb, Next, Cell];
SelectionEvaluate[nb];
];
Pause[10];
CloseFrontEnd[];
and a notebook C:\Temp\run.nb created with a single cell containing:
x1 = 0; While[x1 < 1000000,
If[Mod[x1, 100000] == 0,
Print["x1=" <> ToString[x1]]]; x1++];
NotebookSave[EvaluationNotebook[]];
NotebookClose[EvaluationNotebook[]];
this code, launched from a Windows Command Prompt, will run interactively and save its output. This is not possible to achieve using UsingFrontEnd or MathKernel -script "C:\Temp\test.m".
During the initialization, the kernel code is in a mode which prevents aborts.
Throw/Catch are implemented with Abort, therefore they do not work during initialization.
A simple example that shows the problem is to put this in your test.m file:
Catch[Throw[test]];
Similarly, functions like TimeConstrained, MemoryConstrained, Break, the Trace family, Abort and those that depend upon it (like certain data paclets) will have problems like this during initialization.
A possible solution to your problem might be to consider the -script option:
math.exe -script test.m
Also, note that in version 8 there is a documented function called UsingFrontEnd, which does what UseFrontEnd did, but is auto-configured, so this:
Needs["JLink`"];
UsingFrontEnd[NotebookWrite[CreateDocument[], "Testing"]];
should be all you need in your test.m file.
See also: Mathematica Scripts
Addendum
One possible solution to use the -script and UsingFrontEnd is to use the 'run.m script
included below. This does require setting up a 'Test' kernel in the kernel configuration options (basically a clone of the 'Local' kernel settings).
The script includes two utility functions, NotebookEvaluatingQ and NotebookPauseForEvaluation, which help the script to wait for the client notebook to finish evaluating before saving it. The upside of this approach is that all the evaluation control code is in the 'run.m' script, so the client notebook does not need to have a NotebookSave[EvaluationNotebook[]] statement at the end.
NotebookPauseForEvaluation[nb_] := Module[{},While[NotebookEvaluatingQ[nb],Pause[.25]]]
NotebookEvaluatingQ[nb_]:=Module[{},
SelectionMove[nb,All,Notebook];
Or##Map["Evaluating"/.#&,Developer`CellInformation[nb]]
]
UsingFrontEnd[
nb = NotebookOpen["c:\\users\\arnoudb\\run.nb"];
SetOptions[nb,Evaluator->"Test"];
SelectionMove[nb,All,Notebook];
SelectionEvaluate[nb];
NotebookPauseForEvaluation[nb];
NotebookSave[nb];
]
I hope this is useful in some way to you. It could use a few more improvements like resetting the notebook's kernel to its original and closing the notebook after saving it,
but this code should work for this particular purpose.
On a side note, I tried one other approach, using this:
UsingFrontEnd[ NotebookEvaluate[ "c:\\users\\arnoudb\\run.nb", InsertResults->True ] ]
But this is kicking the kernel terminal session into a dialog mode, which seems like a bug
to me (I'll check into this and get this reported if this is a valid issue).

Resources