|
| 1 | +import 'package:cli_completion/src/system_shell.dart'; |
| 2 | +import 'package:mason_logger/mason_logger.dart'; |
| 3 | +import 'package:meta/meta.dart'; |
| 4 | + |
| 5 | +/// {@template completion_result} |
| 6 | +/// Describes the result of a completion handling process. |
| 7 | +/// {@endtemplate} |
| 8 | +/// |
| 9 | +/// Generated after parsing a completion request from the shell, it is |
| 10 | +/// responsible to contain the information to be sent back to the shell |
| 11 | +/// (via stdout) including suggestions and its metadata (description). |
| 12 | +/// |
| 13 | +/// See also: |
| 14 | +/// - [ValueCompletionResult] |
| 15 | +/// - [EmptyCompletionResult] |
| 16 | +@immutable |
| 17 | +abstract class CompletionResult { |
| 18 | + /// Creates a [CompletionResult] that contains predefined suggestions. |
| 19 | + const factory CompletionResult.fromMap(Map<String, String?> completions) = |
| 20 | + ValueCompletionResult._fromMap; |
| 21 | + |
| 22 | + const CompletionResult._(); |
| 23 | + |
| 24 | + /// Render the completion suggestions on the [shell]. |
| 25 | + void render(Logger logger, SystemShell shell); |
| 26 | +} |
| 27 | + |
| 28 | +/// {@template value_completion_result} |
| 29 | +/// A [CompletionResult] that contains completion suggestions. |
| 30 | +/// {@endtemplate} |
| 31 | +class ValueCompletionResult extends CompletionResult { |
| 32 | + /// {@macro value_completion_result} |
| 33 | + ValueCompletionResult() |
| 34 | + : _completions = <String, String?>{}, |
| 35 | + super._(); |
| 36 | + |
| 37 | + /// Create a [ValueCompletionResult] with predefined completion suggestions |
| 38 | + /// |
| 39 | + /// Since this can be const, calling "addSuggestion" on instances created |
| 40 | + /// with this constructor may result in runtime exceptions. |
| 41 | + /// Use [CompletionResult.fromMap] instead. |
| 42 | + const ValueCompletionResult._fromMap(this._completions) : super._(); |
| 43 | + |
| 44 | + /// A map of completion suggestions to their descriptions. |
| 45 | + final Map<String, String?> _completions; |
| 46 | + |
| 47 | + /// Adds an entry to the current pool of suggestions. Overrides any previous |
| 48 | + /// entry with the same [completion]. |
| 49 | + void addSuggestion(String completion, [String? description]) { |
| 50 | + _completions[completion] = description; |
| 51 | + } |
| 52 | + |
| 53 | + @override |
| 54 | + void render(Logger logger, SystemShell shell) { |
| 55 | + for (final entry in _completions.entries) { |
| 56 | + switch (shell) { |
| 57 | + case SystemShell.zsh: |
| 58 | + // On zsh, colon acts as delimitation between a suggestion and its |
| 59 | + // description. Any literal colon should be escaped. |
| 60 | + final suggestion = entry.key.replaceAll(':', r'\:'); |
| 61 | + final description = entry.value?.replaceAll(':', r'\:'); |
| 62 | + |
| 63 | + logger.info( |
| 64 | + '$suggestion${description != null ? ':$description' : ''}', |
| 65 | + ); |
| 66 | + break; |
| 67 | + case SystemShell.bash: |
| 68 | + logger.info(entry.key); |
| 69 | + break; |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +/// {@template no_completion_result} |
| 76 | +/// A [CompletionResult] that indicates that no completion suggestions should be |
| 77 | +/// displayed. |
| 78 | +/// {@endtemplate} |
| 79 | +class EmptyCompletionResult extends CompletionResult { |
| 80 | + /// {@macro no_completion_result} |
| 81 | + const EmptyCompletionResult() : super._(); |
| 82 | + |
| 83 | + @override |
| 84 | + void render(Logger logger, SystemShell shell) {} |
| 85 | +} |
0 commit comments