Skip to content

Commit

Permalink
fantomas run (note that there seems to be a change on a line I didn't…
Browse files Browse the repository at this point in the history
… touch in my PR AFAIR)
  • Loading branch information
smoothdeveloper committed Dec 19, 2023
1 parent bf372d8 commit de69ac4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
48 changes: 25 additions & 23 deletions src/FSharp.Analyzers.Cli/CustomLogging.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,39 @@ module AnsiColorHelpers =
// see https://learn.microsoft.com/en-us/dotnet/core/extensions/console-log-formatter#implement-custom-color-formatting
let private ansiForegroundEscapeCodeOfColorConsole color =
match color with
| ConsoleColor.Black -> "\x1B[30m"
| ConsoleColor.DarkRed -> "\x1B[31m"
| ConsoleColor.DarkGreen -> "\x1B[32m"
| ConsoleColor.DarkYellow -> "\x1B[33m"
| ConsoleColor.DarkBlue -> "\x1B[34m"
| ConsoleColor.Black -> "\x1B[30m"
| ConsoleColor.DarkRed -> "\x1B[31m"
| ConsoleColor.DarkGreen -> "\x1B[32m"
| ConsoleColor.DarkYellow -> "\x1B[33m"
| ConsoleColor.DarkBlue -> "\x1B[34m"
| ConsoleColor.DarkMagenta -> "\x1B[35m"
| ConsoleColor.DarkCyan -> "\x1B[36m"
| ConsoleColor.Gray -> "\x1B[37m"
| ConsoleColor.Red -> "\x1B[1m\x1B[31m"
| ConsoleColor.Green -> "\x1B[1m\x1B[32m"
| ConsoleColor.Yellow -> "\x1B[1m\x1B[33m"
| ConsoleColor.Blue -> "\x1B[1m\x1B[34m"
| ConsoleColor.Magenta -> "\x1B[1m\x1B[35m"
| ConsoleColor.Cyan -> "\x1B[1m\x1B[36m"
| ConsoleColor.White -> "\x1B[1m\x1B[37m"
| _ ->
#if DEBUG
| ConsoleColor.DarkCyan -> "\x1B[36m"
| ConsoleColor.Gray -> "\x1B[37m"
| ConsoleColor.Red -> "\x1B[1m\x1B[31m"
| ConsoleColor.Green -> "\x1B[1m\x1B[32m"
| ConsoleColor.Yellow -> "\x1B[1m\x1B[33m"
| ConsoleColor.Blue -> "\x1B[1m\x1B[34m"
| ConsoleColor.Magenta -> "\x1B[1m\x1B[35m"
| ConsoleColor.Cyan -> "\x1B[1m\x1B[36m"
| ConsoleColor.White -> "\x1B[1m\x1B[37m"
| _ ->
#if DEBUG
failwith $"didn't implement ansi code for color: {color}"
#else
#else
// do not break code analyzis to wrong runtime color or such thing for release
"\x1B[37m" // ConsoleColor.Gray
#endif
"\x1B[37m" // ConsoleColor.Gray
#endif

let consoleColorOfLogLevel logLevel =
match logLevel with
| LogLevel.Error -> ConsoleColor.Red
| LogLevel.Warning ->ConsoleColor.DarkYellow
| LogLevel.Error -> ConsoleColor.Red
| LogLevel.Warning -> ConsoleColor.DarkYellow
| LogLevel.Information -> ConsoleColor.Blue
| LogLevel.Trace -> ConsoleColor.Cyan
| _ -> ConsoleColor.Gray

let formatMessageAsAnsiColorizedString (color: ConsoleColor) (message: string) =
$"{ansiForegroundEscapeCodeOfColorConsole color}{message}{ansiForegroundEscapeCodeOfColorConsole initialConsoleColor}"
$"{ansiForegroundEscapeCodeOfColorConsole color}{message}{ansiForegroundEscapeCodeOfColorConsole initialConsoleColor}"

type CustomOptions() =
inherit ConsoleFormatterOptions()
Expand All @@ -72,8 +72,10 @@ type CustomFormatter(options: IOptionsMonitor<CustomOptions>) as this =
)
=
let message = logEntry.Formatter.Invoke(logEntry.State, logEntry.Exception)

if formatterOptions.UseLogLevelAsPrefix then
this.WritePrefix(textWriter, logEntry.LogLevel)

textWriter.WriteLine message

member private _.WritePrefix(textWriter: TextWriter, logLevel: LogLevel) =
Expand Down
7 changes: 5 additions & 2 deletions src/FSharp.Analyzers.Cli/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,9 @@ let printMessages (msgs: AnalyzerMessage list) =
for analyzerMessage in msgs do
let m = analyzerMessage.Message
let logLevel = severityToLogLevel[m.Severity]

let messageFormat, messageArgs =
let baseArgs : obj array =
let baseArgs: obj array =
[|
m.Range.FileName
m.Range.StartLine
Expand All @@ -269,10 +270,12 @@ let printMessages (msgs: AnalyzerMessage list) =
colorFormat (CustomLogging.AnsiColorHelpers.consoleColorOfLogLevel logLevel) m.Code
m.Message
|]

let baseFormat = "{0}({1},{2}): {3} {4} - {5}"

match analyzerMessage.HelpUri with
| None -> baseFormat, baseArgs
| Some uri -> baseFormat + " {6}", [|yield! baseArgs; colorFormat ConsoleColor.Cyan uri|]
| Some uri -> baseFormat + " {6}", [| yield! baseArgs; colorFormat ConsoleColor.Cyan uri |]

msgLogger.Log(logLevel, messageFormat, messageArgs)

Expand Down

0 comments on commit de69ac4

Please sign in to comment.