Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pretty printing with colors and adding the help uri if present, to the console output #174

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

### Changed
* [Add path to ASTCollecting](https://github.com/ionide/FSharp.Analyzers.SDK/pull/171) (thanks @nojaf!)
* [Console printing enhancements](https://github.com/ionide/FSharp.Analyzers.SDK/pull/174) (thanks @smoothdeveloper!)

## [0.21.0] - 2023-11-22

Expand Down
51 changes: 32 additions & 19 deletions src/FSharp.Analyzers.Cli/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,14 @@ let runFscArgs

runProjectAux client projectOptions globs mappings

let switchConsoleColor newColor =
let savedColor = Console.ForegroundColor
Console.ForegroundColor <- newColor

{ new IDisposable with
member x.Dispose() = Console.ForegroundColor <- savedColor
}

let printMessages (msgs: AnalyzerMessage list) =
if verbose then
printfn ""
Expand All @@ -251,25 +259,30 @@ let printMessages (msgs: AnalyzerMessage list) =
|> Seq.iter (fun analyzerMessage ->
let m = analyzerMessage.Message

let color =
match m.Severity with
| Error -> ConsoleColor.Red
| Warning -> ConsoleColor.DarkYellow
| Info -> ConsoleColor.Blue
| Hint -> ConsoleColor.Cyan

Console.ForegroundColor <- color

printfn
"%s(%d,%d): %s %s - %s"
m.Range.FileName
m.Range.StartLine
m.Range.StartColumn
(m.Severity.ToString())
m.Code
m.Message

Console.ForegroundColor <- origForegroundColor
do
use _ = switchConsoleColor ConsoleColor.Gray
printf "%s(%d,%d) " m.Range.FileName m.Range.StartLine m.Range.StartColumn

do
let color =
match m.Severity with
| Error -> ConsoleColor.Red
| Warning -> ConsoleColor.DarkYellow
| Info -> ConsoleColor.Blue
| Hint -> ConsoleColor.Cyan

use _ = switchConsoleColor color
printf "%s %s " (m.Severity.ToString()) m.Code

do
use _ = switchConsoleColor ConsoleColor.Gray
printf "%s" m.Message

match analyzerMessage.HelpUri with
| Some helpUri ->
use _ = switchConsoleColor ConsoleColor.Cyan
printfn $" %s{helpUri}"
| None -> printfn ""
)

()
Expand Down