diff --git a/CHANGELOG.md b/CHANGELOG.md index 9fa72e7665..f86f7f3d5b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/kiota/Handlers/BaseKiotaCommandHandler.cs b/src/kiota/Handlers/BaseKiotaCommandHandler.cs index bb7f4e2f87..b58739222d 100644 --- a/src/kiota/Handlers/BaseKiotaCommandHandler.cs +++ b/src/kiota/Handlers/BaseKiotaCommandHandler.cs @@ -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 "); - else - Console.WriteLine($"Example: kiota download {searchTerm} -v {version} -o "); + var example = string.IsNullOrEmpty(version) ? + $"Example: kiota download {searchTerm} -o " : + $"Example: kiota download {searchTerm} -v {version} -o "; + 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 includePaths, IEnumerable 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 includedPaths, IEnumerable 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 -o -d {path}{includedPathsSuffix}{excludedPathsSuffix}"); + var example = $"Example: kiota generate -l -o -d {path}{includedPathsSuffix}{excludedPathsSuffix}"; + DisplayHint("Hint: use kiota generate to generate a client for the OpenAPI description.", example); } } protected void DisplayGenerateAdvancedHint(IEnumerable includePaths, IEnumerable 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 "); + DisplayHint("Hint: use the language argument to get the list of dependencies you need to add to your project.", + "Example: kiota info -l "); } } } diff --git a/src/kiota/Handlers/KiotaDownloadCommandHandler.cs b/src/kiota/Handlers/KiotaDownloadCommandHandler.cs index 7458e4672f..05ace12290 100644 --- a/src/kiota/Handlers/KiotaDownloadCommandHandler.cs +++ b/src/kiota/Handlers/KiotaDownloadCommandHandler.cs @@ -62,15 +62,15 @@ public override async Task InvokeAsync(InvocationContext context) private async Task SaveResultsAsync(IDictionary 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(), Enumerable.Empty()); 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; } diff --git a/src/kiota/Handlers/KiotaGenerationCommandHandler.cs b/src/kiota/Handlers/KiotaGenerationCommandHandler.cs index 95a7acba51..ee81d0ed41 100644 --- a/src/kiota/Handlers/KiotaGenerationCommandHandler.cs +++ b/src/kiota/Handlers/KiotaGenerationCommandHandler.cs @@ -76,7 +76,7 @@ public override async Task 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; diff --git a/src/kiota/Handlers/KiotaInfoCommandHandler.cs b/src/kiota/Handlers/KiotaInfoCommandHandler.cs index 4f7c614288..f81512daee 100644 --- a/src/kiota/Handlers/KiotaInfoCommandHandler.cs +++ b/src/kiota/Handlers/KiotaInfoCommandHandler.cs @@ -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}."); } } } diff --git a/src/kiota/Handlers/KiotaSeachBasedCommandHandler.cs b/src/kiota/Handlers/KiotaSeachBasedCommandHandler.cs index 28cffe4a19..c006e33791 100644 --- a/src/kiota/Handlers/KiotaSeachBasedCommandHandler.cs +++ b/src/kiota/Handlers/KiotaSeachBasedCommandHandler.cs @@ -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); } } diff --git a/src/kiota/Handlers/KiotaSearchCommandHandler.cs b/src/kiota/Handlers/KiotaSearchCommandHandler.cs index 43eb6b5bbe..8660f9d8f4 100644 --- a/src/kiota/Handlers/KiotaSearchCommandHandler.cs +++ b/src/kiota/Handlers/KiotaSearchCommandHandler.cs @@ -55,11 +55,11 @@ private void DisplayResults(IDictionary 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 {