Coder Social home page Coder Social logo

mszgs / jktoolkit.spectre.autocompletion Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jkamsker/jktoolkit.spectre.autocompletion

0.0 0.0 0.0 691 KB

Supercharge your Spectre.Console with AutoCompletion

License: MIT License

C# 97.27% PowerShell 2.73%

jktoolkit.spectre.autocompletion's Introduction

Spectre.Console AutoCompletion

NuGet Nuget License PR GitHub Workflow Status

JKToolKit.Spectre.AutoCompletion is an extension package for adding auto completion to your Spectre.Console powered applications.
It comes with suggestions for Options and Branches out of the box, but you can also add your own suggestions for option and argument values.

Enabling Module

The extension can be enabled using the AddAutoCompletion method on the Configurator object.

using JKToolKit.Spectre.AutoCompletion.Completion;
using JKToolKit.Spectre.AutoCompletion.Integrations;

//... 

public static void Main(string[] args)
{
    var app = new CommandApp();
    app.Configure
    (
        config =>
        {
            config
                .AddAutoCompletion(x => x.AddPowershell())
                .AddCommand<LionCommand>("lion");
        }
    ) ;
}

Shell integrations

  1. PowerShell
  2. More to come...

PowerShell

You can add autocomplete to PowerShell by running your application with the completion powershell command, as shown below:

.\AutoCompletion.exe completion powershell | Out-String | Invoke-Expression

To add autocomplete to PowerShell permanently, use the --install flag:

.\AutoCompletion.exe completion powershell --install | Out-String | Invoke-Expression

How integrations get the suggestions

The shell integration uses the completion complete command to get the suggestions for the current command line like this:

.\AutoCompletion.exe completion complete "AutoCompletion.exe Li"

Customizations

  1. Static Autocomplete
  2. Dynamic Autocomplete

Static Autocomplete

Spectre.Console auto completion allows you to specify static autocomplete suggestions for your command arguments and options. This can be done using the CompletionSuggestions attribute in your command settings class.

Here's an example of how to add static autocomplete suggestions:

public class LionSettings : CommandSettings
{
    [CommandArgument(0, "<TEETH>")]
    [Description("The number of teeth the lion has.")]
    [CompletionSuggestions("10", "15", "20", "30")]
    public int Teeth { get; set; }

    [CommandOption("-a|--age <AGE>")]
    public int Age { get; set; }

    [CommandOption("-n|--name <NAME>")]
    public string Name { get; set; }
}

Dynamic Autocomplete

In addition to static autocomplete suggestions, you can also provide dynamic autocomplete suggestions based on the user's input. This can be done by implementing the IAsyncCommandCompletable interface in your command class and overriding the GetSuggestionsAsync method.

Here's an example of how to add dynamic autocomplete suggestions:

[Description("The lion command.")]
public class LionCommand : Command<LionSettings>, IAsyncCommandCompletable
{
    public override int Execute(CommandContext context, LionSettings settings)
    {
        return 0;
    }

    public async Task<CompletionResult> GetSuggestionsAsync(IMappedCommandParameter parameter, ICompletionContext context)
    {
        if (string.IsNullOrEmpty(parameter.Value))
        {
            return CompletionResult.None();
        }

        return await this.MatchAsync()
            .Add(x => x.Legs, (prefix) =>
            {
                if (prefix.Length != 0)
                {
                    return FindNextEvenNumber(prefix);
                }

                return "16";
            })
            .Add(x => x.Teeth, (prefix) =>
            {
                if (prefix.Length != 0)
                {
                    return FindNextEvenNumber(prefix);
                }

                return "32";
            })
            .Add(x => x.Name, prefix =>
            {
                var names = new List<string>
                {
                    "angel", "angelika", "robert",
                    "jennifer", "michael", "lucy",
                    "david", "sarah", "john", "katherine",
                    "mark"
                };

                var bestMatches = names
                    .Where(name => name.StartsWith(prefix))
                    .ToList();

                return new CompletionResult(bestMatches, bestMatches.Any());
            })
            .MatchAsync(parameter)
            .WithPreventDefault();
    }
}

There is a working example of the AutoCompletion feature demonstrating this.


Made with โ™ฅ in Austria

jktoolkit.spectre.autocompletion's People

Contributors

jkamsker avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.