completion_generator.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #pragma once
  2. #include "completer.h"
  3. #include "formatted_output.h"
  4. #include "last_getopt_opts.h"
  5. #include "modchooser.h"
  6. #include <util/generic/variant.h>
  7. #include <util/string/builder.h>
  8. namespace NLastGetopt {
  9. class TCompletionGenerator {
  10. public:
  11. explicit TCompletionGenerator(const TModChooser* modChooser);
  12. explicit TCompletionGenerator(const TOpts* opts);
  13. virtual ~TCompletionGenerator() = default;
  14. public:
  15. virtual void Generate(TStringBuf command, IOutputStream& stream) = 0;
  16. protected:
  17. std::variant<const TModChooser*, const TOpts*> Options_;
  18. };
  19. class TZshCompletionGenerator: public TCompletionGenerator {
  20. public:
  21. using TCompletionGenerator::TCompletionGenerator;
  22. public:
  23. void Generate(TStringBuf command, IOutputStream& stream) override;
  24. private:
  25. static void GenerateModesCompletion(TFormattedOutput& out, const TModChooser& chooser, NComp::TCompleterManager& manager);
  26. static void GenerateOptsCompletion(TFormattedOutput& out, const TOpts& opts, NComp::TCompleterManager& manager);
  27. static void GenerateDefaultOptsCompletion(TFormattedOutput& out, NComp::TCompleterManager& manager);
  28. static void GenerateOptCompletion(TFormattedOutput& out, const TOpts& opts, const TOpt& opt, NComp::TCompleterManager& manager);
  29. };
  30. class TBashCompletionGenerator: public TCompletionGenerator {
  31. public:
  32. using TCompletionGenerator::TCompletionGenerator;
  33. public:
  34. void Generate(TStringBuf command, IOutputStream& stream) override;
  35. private:
  36. static void GenerateModesCompletion(TFormattedOutput& out, const TModChooser& chooser, NComp::TCompleterManager& manager, size_t level);
  37. static void GenerateOptsCompletion(TFormattedOutput& out, const TOpts& opts, NComp::TCompleterManager& manager, size_t level);
  38. static void GenerateDefaultOptsCompletion(TFormattedOutput& out, NComp::TCompleterManager& manager);
  39. };
  40. namespace NEscaping {
  41. /// Escape ':', '-', '=', '[', ']' for use in zsh _arguments
  42. TString Q(TStringBuf string);
  43. TString QQ(TStringBuf string);
  44. /// Escape colons for use in zsh _alternative and _arguments
  45. TString C(TStringBuf string);
  46. TString CC(TStringBuf string);
  47. /// Simple escape for use in zsh single-quoted strings
  48. TString S(TStringBuf string);
  49. TString SS(TStringBuf string);
  50. /// Simple escape for use in bash single-quoted strings
  51. TString B(TStringBuf string);
  52. TString BB(TStringBuf string);
  53. }
  54. }