Skip to content

Commit

Permalink
- fixes #1895 adds a colours scheme for console messages
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Biret <[email protected]>
  • Loading branch information
baywet committed Oct 18, 2022
1 parent 1622aca commit 20e794e
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 55 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added support for enum and collection of enum return types for Java.
- Added support for types with more than 500 discriminator entries in Java.
- Added a confirmation message once the generation is successful. [#1898](https://github.com/microsoft/kiota/issues/1898)
- Added colour scheme for all console messages to improve readability. [#1895](https://github.com/microsoft/kiota/issues/1895)

### Changed

Expand Down
90 changes: 50 additions & 40 deletions src/kiota/Handlers/BaseKiotaCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,78 +67,88 @@ protected static string NormalizeSlashesInPath(string path) {
return true;
});
protected bool TutorialMode => tutorialMode.Value;
private static void DisplayHint(params string[] messages) {
Console.WriteLine();
DisplayMessages(ConsoleColor.Blue, messages);
}
private static void DisplayMessages(ConsoleColor color, params string[] messages) {
Console.ForegroundColor = color;
foreach(var message in messages)
Console.WriteLine(message);
Console.ResetColor();
}
protected static void DisplayError(params string[] messages) {
DisplayMessages(ConsoleColor.Red, messages);
}
protected static void DisplayWarning(params string[] messages) {
DisplayMessages(ConsoleColor.Yellow, messages);
}
protected static void DisplaySuccess(params string[] messages) {
DisplayMessages(ConsoleColor.Green, messages);
}
protected static void DisplayInfo(params string[] messages) {
DisplayMessages(ConsoleColor.White, messages);
}
protected void DisplayDownloadHint(string searchTerm, string version) {
if(TutorialMode) {
Console.WriteLine();
Console.WriteLine("Hint: use kiota download to download the OpenAPI description.");
if(string.IsNullOrEmpty(version))
Console.WriteLine($"Example: kiota download {searchTerm} -o <output path>");
else
Console.WriteLine($"Example: kiota download {searchTerm} -v {version} -o <output path>");
var example = string.IsNullOrEmpty(version) ?
$"Example: kiota download {searchTerm} -o <output path>" :
$"Example: kiota download {searchTerm} -v {version} -o <output path>";
DisplayHint("Hint: use kiota download to download the OpenAPI description.", example);
}
}
protected void DisplayShowHint(string searchTerm, string version, string path = null) {
if(TutorialMode) {
Console.WriteLine();
Console.WriteLine("Hint: use kiota show to display a tree of paths present in the OpenAPI description.");
if(!string.IsNullOrEmpty(path))
Console.WriteLine($"Example: kiota show -d {path}");
else if(string.IsNullOrEmpty(version))
Console.WriteLine($"Example: kiota show -k {searchTerm}");
else
Console.WriteLine($"Example: kiota show -k {searchTerm} -v {version}");
var example = path switch {
_ when !string.IsNullOrEmpty(path) => $"Example: kiota show -d {path}",
_ when string.IsNullOrEmpty(version) => $"Example: kiota show -k {searchTerm}",
_ => $"Example: kiota show -k {searchTerm} -v {version}",
};
DisplayHint("Hint: use kiota show to display a tree of paths present in the OpenAPI description.", example);
}
}
protected void DisplayShowAdvancedHint(string searchTerm, string version, IEnumerable<string> includePaths, IEnumerable<string> excludePaths, string path = null) {
if(TutorialMode && !includePaths.Any() && !excludePaths.Any()) {
Console.WriteLine();
Console.WriteLine("Hint: use the --include-path and --exclude-path options with glob patterns to filter the paths displayed.");
if(!string.IsNullOrEmpty(path))
Console.WriteLine($"Example: kiota show -d {path} --include-path **/foo");
else if(string.IsNullOrEmpty(version))
Console.WriteLine($"Example: kiota show -k {searchTerm} --include-path **/foo");
else
Console.WriteLine($"Example: kiota show -k {searchTerm} -v {version} --include-path **/foo");
var example = path switch {
_ when !string.IsNullOrEmpty(path) => $"Example: kiota show -d {path} --include-path **/foo",
_ when string.IsNullOrEmpty(version) => $"Example: kiota show -k {searchTerm} --include-path **/foo",
_ => $"Example: kiota show -k {searchTerm} -v {version} --include-path **/foo",
};
DisplayHint("Hint: use the --include-path and --exclude-path options with glob patterns to filter the paths displayed.", example);
}
}
protected void DisplaySearchHint(string firstKey, string version) {
if (TutorialMode &&!string.IsNullOrEmpty(firstKey)) {
Console.WriteLine();
Console.WriteLine("Hint: multiple matches found, use the key as the search term to display the details of a specific description.");
if(string.IsNullOrEmpty(version))
Console.WriteLine($"Example: kiota search {firstKey}");
else
Console.WriteLine($"Example: kiota search {firstKey} -v {version}");
var example = string.IsNullOrEmpty(version) ?
$"Example: kiota search {firstKey}" :
$"Example: kiota search {firstKey} -v {version}";
DisplayHint("Hint: multiple matches found, use the key as the search term to display the details of a specific description.", example);
}
}
protected void DisplayGenerateHint(string path, IEnumerable<string> includedPaths, IEnumerable<string> excludedPaths) {
if(TutorialMode) {
Console.WriteLine();
Console.WriteLine("Hint: use kiota generate to generate a client for the OpenAPI description.");
var includedPathsSuffix = ((includedPaths?.Any() ?? false)? " -i " : string.Empty) + string.Join(" -i ", includedPaths);
var excludedPathsSuffix = ((excludedPaths?.Any() ?? false)? " -e " : string.Empty) + string.Join(" -e ", excludedPaths);
Console.WriteLine($"Example: kiota generate -l <language> -o <output path> -d {path}{includedPathsSuffix}{excludedPathsSuffix}");
var example = $"Example: kiota generate -l <language> -o <output path> -d {path}{includedPathsSuffix}{excludedPathsSuffix}";
DisplayHint("Hint: use kiota generate to generate a client for the OpenAPI description.", example);
}
}
protected void DisplayGenerateAdvancedHint(IEnumerable<string> includePaths, IEnumerable<string> excludePaths, string path) {
if(TutorialMode && !includePaths.Any() && !excludePaths.Any()) {
Console.WriteLine();
Console.WriteLine("Hint: use the --include-path and --exclude-path options with glob patterns to filter the paths generated.");
Console.WriteLine($"Example: kiota generate --include-path **/foo -d {path}");
DisplayHint("Hint: use the --include-path and --exclude-path options with glob patterns to filter the paths generated.",
$"Example: kiota generate --include-path **/foo -d {path}");
}
}
protected void DisplayInfoHint(GenerationLanguage language, string path) {
if(TutorialMode) {
Console.WriteLine();
Console.WriteLine("Hint: use the info command to get the list of dependencies you need to add to your project.");
Console.WriteLine($"Example: kiota info -d {path} -l {language}");
DisplayHint("Hint: use the info command to get the list of dependencies you need to add to your project.",
$"Example: kiota info -d {path} -l {language}");
}
}
protected void DisplayInfoAdvanced() {
if(TutorialMode) {
Console.WriteLine();
Console.WriteLine("Hint: use the language argument to get the list of dependencies you need to add to your project.");
Console.WriteLine("Example: kiota info -l <language>");
DisplayHint("Hint: use the language argument to get the list of dependencies you need to add to your project.",
"Example: kiota info -l <language>");
}
}
}
6 changes: 3 additions & 3 deletions src/kiota/Handlers/KiotaDownloadCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ public override async Task<int> InvokeAsync(InvocationContext context)
private async Task<int> SaveResultsAsync(IDictionary<string, SearchResult> results, ILogger logger, CancellationToken cancellationToken){
var searchTerm = Configuration.Download.SearchTerm;
if (!results.Any())
Console.WriteLine("No matching result found, use the search command to find the right key");
DisplayError("No matching result found, use the search command to find the right key");
else if (results.Any() && !string.IsNullOrEmpty(searchTerm) && searchTerm.Contains(KiotaSearcher.ProviderSeparator) && results.ContainsKey(searchTerm)) {
var (path, statusCode) = await SaveResultAsync(results.First(), logger, cancellationToken);
Console.WriteLine($"File successfully downloaded to {path}");
DisplaySuccess($"File successfully downloaded to {path}");
DisplayShowHint(Configuration.Search.SearchTerm, Configuration.Search.Version, path);
DisplayGenerateHint(path, Enumerable.Empty<string>(), Enumerable.Empty<string>());
return statusCode;
} else
Console.WriteLine("Multiple matches found, use the key to select a specific description.");
DisplayError("Multiple matches found, use the key to select a specific description. You can find the key by using the search command.");

return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion src/kiota/Handlers/KiotaGenerationCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public override async Task<int> InvokeAsync(InvocationContext context)

try {
await new KiotaBuilder(logger, Configuration.Generation).GenerateClientAsync(cancellationToken);
Console.WriteLine("Generation completed successfully");
DisplaySuccess("Generation completed successfully");
DisplayInfoHint(language, Configuration.Generation.OpenAPIFilePath);
DisplayGenerateAdvancedHint(includePatterns, excludePatterns, Configuration.Generation.OpenAPIFilePath);
return 0;
Expand Down
8 changes: 4 additions & 4 deletions src/kiota/Handlers/KiotaInfoCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ private void ShowLanguagesTable() {
}
private static void ShowLanguageInformation(GenerationLanguage language, LanguagesInformation informationSource) {
if (informationSource.TryGetValue(language.ToString(), out var languageInformation)) {
Console.WriteLine($"The language {language} is currently in {languageInformation.MaturityLevel} maturity level.");
Console.WriteLine("After generating code for this language, you need to install the following packages:");
DisplayInfo($"The language {language} is currently in {languageInformation.MaturityLevel} maturity level.",
"After generating code for this language, you need to install the following packages:");
foreach(var dependency in languageInformation.Dependencies) {
Console.WriteLine(languageInformation.DependencyInstallCommand, dependency.Name, dependency.Version);
DisplayInfo(languageInformation.DependencyInstallCommand, dependency.Name, dependency.Version);
}
} else {
Console.WriteLine($"No information for {language}.");
DisplayInfo($"No information for {language}.");
}
}
}
4 changes: 2 additions & 2 deletions src/kiota/Handlers/KiotaSeachBasedCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ internal abstract class KiotaSearchBasedCommandHandler : BaseKiotaCommandHandler
if (results.Count == 1)
return (results.First().Value.DescriptionUrl.ToString(), null);
else if(!results.Any()) {
Console.WriteLine("No results found for the search term, use the search command to locate the description");
DisplayWarning("No results found for the search term, use the search command to locate the description");
return (string.Empty, 1);
} else {
Console.WriteLine("Multiple results found for the search term, use the search command to locate the description");
DisplayWarning("Multiple results found for the search term, use the search command to locate the description");
return (string.Empty, 1);
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/kiota/Handlers/KiotaSearchCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ private void DisplayResults(IDictionary<string, SearchResult> results){
var searchTerm = Configuration.Search.SearchTerm;
if (results.Any() && !string.IsNullOrEmpty(searchTerm) && searchTerm.Contains(KiotaSearcher.ProviderSeparator) && results.ContainsKey(searchTerm)) {
var result = results.First();
Console.WriteLine($"Key: {result.Key}");
Console.WriteLine($"Title: {result.Value.Title}");
Console.WriteLine($"Description: {result.Value.Description}");
Console.WriteLine($"Service: {result.Value.ServiceUrl}");
Console.WriteLine($"OpenAPI: {result.Value.DescriptionUrl}");
DisplayInfo($"Key: {result.Key}");
DisplayInfo($"Title: {result.Value.Title}");
DisplayInfo($"Description: {result.Value.Description}");
DisplayInfo($"Service: {result.Value.ServiceUrl}");
DisplayInfo($"OpenAPI: {result.Value.DescriptionUrl}");
DisplayDownloadHint(Configuration.Search.SearchTerm, Configuration.Search.Version);
DisplayShowHint(Configuration.Search.SearchTerm, Configuration.Search.Version);
} else {
Expand Down

0 comments on commit 20e794e

Please sign in to comment.