aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/getopt/small/completion_generator.h
blob: 4241bb7d6cc7b07ea929f8d505fa4813ab9ebddc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#pragma once

#include "completer.h"
#include "formatted_output.h"
#include "last_getopt_opts.h"
#include "modchooser.h"

#include <util/generic/variant.h>
#include <util/string/builder.h>

namespace NLastGetopt {
    class TCompletionGenerator {
    public:
        explicit TCompletionGenerator(const TModChooser* modChooser);
        explicit TCompletionGenerator(const TOpts* opts);
        virtual ~TCompletionGenerator() = default;

    public:
        virtual void Generate(TStringBuf command, IOutputStream& stream) = 0;

    protected:
        std::variant<const TModChooser*, const TOpts*> Options_;
    };

    class TZshCompletionGenerator: public TCompletionGenerator {
    public:
        using TCompletionGenerator::TCompletionGenerator;

    public:
        void Generate(TStringBuf command, IOutputStream& stream) override;

    private:
        static void GenerateModesCompletion(TFormattedOutput& out, const TModChooser& chooser, NComp::TCompleterManager& manager);
        static void GenerateOptsCompletion(TFormattedOutput& out, const TOpts& opts, NComp::TCompleterManager& manager);
        static void GenerateDefaultOptsCompletion(TFormattedOutput& out, NComp::TCompleterManager& manager);
        static void GenerateOptCompletion(TFormattedOutput& out, const TOpts& opts, const TOpt& opt, NComp::TCompleterManager& manager);
    };

    class TBashCompletionGenerator: public TCompletionGenerator {
    public:
        using TCompletionGenerator::TCompletionGenerator;

    public:
        void Generate(TStringBuf command, IOutputStream& stream) override;

    private:
        static void GenerateModesCompletion(TFormattedOutput& out, const TModChooser& chooser, NComp::TCompleterManager& manager, size_t level);
        static void GenerateOptsCompletion(TFormattedOutput& out, const TOpts& opts, NComp::TCompleterManager& manager, size_t level);
        static void GenerateDefaultOptsCompletion(TFormattedOutput& out, NComp::TCompleterManager& manager);
    };

    namespace NEscaping {
        /// Escape ':', '-', '=', '[', ']' for use in zsh _arguments
        TString Q(TStringBuf string);
        TString QQ(TStringBuf string);

        /// Escape colons for use in zsh _alternative and _arguments
        TString C(TStringBuf string);
        TString CC(TStringBuf string);

        /// Simple escape for use in zsh single-quoted strings
        TString S(TStringBuf string);
        TString SS(TStringBuf string);

        /// Simple escape for use in bash single-quoted strings
        TString B(TStringBuf string);
        TString BB(TStringBuf string);
    }
}