Skip to content

Autosuggestions

mattirn edited this page Oct 29, 2020 · 7 revisions

AutosuggestionWidgets

Fish-like autosuggestions for JLine.

AutosuggestionWidgets suggests commands as you type based on command history.

Note: Because 'a muted gray color' was not rendered correctly by asciinema the suggestions on above recording are colored in 'magenta'.

Java

LineReader reader = LineReaderBuilder.builder()
                    .terminal(terminal)
                    .completer(completer)
                    .parser(parser)
                    .build();
// Create autosuggestion widgets
AutosuggestionWidgets autosuggestionWidgets = new AutosuggestionWidgets(reader);
// Enable autosuggestions 
autosuggestionWidgets.enable();

Usage

As you type commands, you will see a completion offered after the cursor in a muted gray color.

If you press the key (forward-char widget) or End (end-of-line widget) with the cursor at the end of the buffer, it will accept the suggestion, replacing the contents of the command line buffer with the suggestion.

If you invoke the forward-word widget, it will partially accept the suggestion up to the point that the cursor moves to.

Key Bindings

This plugin provides autosuggest-toggle widget that toggles between enabled/disabled suggestions.

TailTipWidgets

TailTipWidgets suggests commands as you type based on command completer data and/or command arguments and options descriptions.

Note: Because 'a muted gray color' was not rendered correctly by asciinema the suggestions on above recording are colored in 'magenta'.

Java

LineReader reader = LineReaderBuilder.builder()
                    .terminal(terminal)
                    .completer(completer)
                    .parser(parser)
                    .build();
Map<String, CmdDesc> tailTips = new HashMap<>();
Map<String, List<AttributedString>> widgetOpts = new HashMap<>();
List<AttributedString> mainDesc = Arrays.asList(new AttributedString("widget -N new-widget [function-name]")
                                        , new AttributedString("widget -D widget ...")
                                        , new AttributedString("widget -A old-widget new-widget")
                                        , new AttributedString("widget -U string ...")
                                        , new AttributedString("widget -l [options]")
                       );
widgetOpts.put("-N", Arrays.asList(new AttributedString("Create new widget")));
widgetOpts.put("-D", Arrays.asList(new AttributedString("Delete widgets")));
widgetOpts.put("-A", Arrays.asList(new AttributedString("Create alias to widget")));
widgetOpts.put("-U", Arrays.asList(new AttributedString("Push characters to the stack")));
widgetOpts.put("-l", Arrays.asList(new AttributedString("List user-defined widgets")));

tailTips.put("widget", new CmdDesc(mainDesc, ArgDesc.doArgNames(Arrays.asList("[pN...]")), widgetOpts));
....
....
....
// Create tailtip widgets that uses description window size 5 and
// does not display suggestions after the cursor
TailTipWidgets tailtipWidgets = new TailTipWidgets(reader, tailTips, 5, TipType.COMPLETER);
// Enable autosuggestions 
tailtipWidgets.enable();

Usage

As you type commands, you will see a completion offered after the cursor in a muted gray color and argument/option description on status pane.

Command line tab completion works as normal.

Configuration

Description pane can be disabled by setting descriptionSize = 0. Suggestions type can be configured using constructor parameter tipType:

  1. COMPLETER Tab completions are displayed below command line. No suggestions are displayed after cursor.
  2. TAIL_TIP Argument suggestions are displayed after cursor. No tab completions are displayed.
  3. COMBINED Argument suggestions are shown if available otherwise tab completions are displayed.

The maximum number of completer suggestions displayed can be controlled by JLine variable list-max.

For example creating TailTipWidgets as

TailTipWidgets tailtipWidgets = new TailTipWidgets(reader, tailTips, 0, TipType.TAIL_TIP);

you will obtain Redis like suggestions.

Key Bindings

This plugin provides two widgets:

  1. tailtip-toggle Toggles between enabled/disabled suggestions.
  2. tailtip-window Toggles tailtip description pane.

For example, this would bind alt + w to toggle tailtip description pane.

keymap ^[w tailtip-window