Skip to content

Commit

Permalink
Fix/4130 disable color output for cli (#4785)
Browse files Browse the repository at this point in the history
* Disable Cli color scheme by default. Color scheme can be passed as option.

* Use System.Console by default

Co-authored-by: lukasz.rozmej <lukasz.rozmej@gmail.com>
  • Loading branch information
damian-orzechowski and LukaszRozmej authored Dec 28, 2022
1 parent 0f0a443 commit f556e04
Show file tree
Hide file tree
Showing 6 changed files with 257 additions and 110 deletions.
123 changes: 60 additions & 63 deletions src/Nethermind/Nethermind.Cli/Console/CliConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,52 @@

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.InteropServices;
using Nethermind.Core;

namespace Nethermind.Cli.Console;

public class CliConsole : ICliConsole
{
private static ColorScheme _colorScheme = DraculaColorScheme.Instance;
protected static Terminal _terminal;
public Terminal Terminal { get => _terminal; }

private static Terminal _terminal;

private static readonly Dictionary<string, Terminal> Terminals = new Dictionary<string, Terminal>
protected static readonly Dictionary<string, Terminal> Terminals = new Dictionary<string, Terminal>
{
["cmd.exe"] = Terminal.Cmd,
["cmd"] = Terminal.Cmder,
["powershell"] = Terminal.Powershell,
["cygwin"] = Terminal.Cygwin
};

public Terminal Init(ColorScheme colorScheme)
public CliConsole()
{
_colorScheme = colorScheme;
Colorful.Console.BackgroundColor = colorScheme.BackgroundColor;
Colorful.Console.ForegroundColor = colorScheme.Text;
_terminal = GetTerminal();
if (_terminal != Terminal.Powershell)
{
Colorful.Console.ResetColor();
}

if (_terminal != Terminal.Cygwin)
{
Colorful.Console.Clear();
}

string version = ProductInfo.Version;

Colorful.Console.WriteLine("**********************************************", _colorScheme.Comment);
Colorful.Console.WriteLine();
Colorful.Console.WriteLine("Nethermind CLI {0}", GetColor(_colorScheme.Good), version);
Colorful.Console.WriteLine(" https://github.com/NethermindEth/nethermind", GetColor(_colorScheme.Interesting));
Colorful.Console.WriteLine(" https://nethermind.readthedocs.io/en/latest/", GetColor(_colorScheme.Interesting));
Colorful.Console.WriteLine();
Colorful.Console.WriteLine("powered by:", _colorScheme.Text);
Colorful.Console.WriteLine(" https://github.com/sebastienros/jint", GetColor(_colorScheme.Interesting));
Colorful.Console.WriteLine(" https://github.com/tomakita/Colorful.Console", GetColor(_colorScheme.Interesting));
Colorful.Console.WriteLine(" https://github.com/tonerdo/readline", GetColor(_colorScheme.Interesting));
Colorful.Console.WriteLine();
Colorful.Console.WriteLine("**********************************************", _colorScheme.Comment);
Colorful.Console.WriteLine();

return _terminal;
_terminal = PrepareConsoleForTerminal();

System.Console.WriteLine("**********************************************");
System.Console.WriteLine();
System.Console.WriteLine("Nethermind CLI {0}", ProductInfo.Version);
System.Console.WriteLine(" https://github.com/NethermindEth/nethermind");
System.Console.WriteLine(" https://nethermind.readthedocs.io/en/latest/");
System.Console.WriteLine();
System.Console.WriteLine("powered by:");
System.Console.WriteLine(" https://github.com/sebastienros/jint");
System.Console.WriteLine(" https://github.com/tomakita/Colorful.Console");
System.Console.WriteLine(" https://github.com/tonerdo/readline");
System.Console.WriteLine();
System.Console.WriteLine("**********************************************");
System.Console.WriteLine();
}

private Terminal GetTerminal()
protected Terminal GetTerminal()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? Terminal.LinuxBash :
RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? Terminal.MacBash : Terminal.Unknown;
}

var title = Colorful.Console.Title.ToLowerInvariant();
var title = System.Console.Title.ToLowerInvariant();
foreach (var (key, value) in Terminals)
{
if (title.Contains(key))
Expand All @@ -78,63 +60,78 @@ private Terminal GetTerminal()
return Terminal.Unknown;
}

private Color GetColor(Color defaultColor)
protected Terminal PrepareConsoleForTerminal()
{
_terminal = GetTerminal();
if (_terminal != Terminal.Powershell)
{
System.Console.ResetColor();
}

if (_terminal != Terminal.Cygwin)
{
System.Console.Clear();
}
return _terminal;
}

public virtual void WriteException(Exception e)
{
return _terminal == Terminal.LinuxBash ? _colorScheme.Text : defaultColor;
System.Console.WriteLine(e.ToString());
}

public void WriteException(Exception e)
public virtual void WriteErrorLine(string errorMessage)
{
Colorful.Console.WriteLine(e.ToString(), GetColor(_colorScheme.ErrorColor));
System.Console.WriteLine(errorMessage);
}

public void WriteErrorLine(string errorMessage)
public virtual void WriteLine(object objectToWrite)
{
Colorful.Console.WriteLine(errorMessage, GetColor(_colorScheme.ErrorColor));
System.Console.WriteLine(objectToWrite.ToString());
}

public void WriteLine(object objectToWrite)
public virtual void Write(object objectToWrite)
{
Colorful.Console.WriteLine(objectToWrite.ToString(), _colorScheme.Text);
System.Console.Write(objectToWrite.ToString());
}

public void Write(object objectToWrite)
public virtual void WriteCommentLine(object objectToWrite)
{
Colorful.Console.Write(objectToWrite.ToString(), _colorScheme.Text);
System.Console.WriteLine(objectToWrite.ToString());
}

public void WriteCommentLine(object objectToWrite)
public virtual void WriteLessImportant(object objectToWrite)
{
Colorful.Console.WriteLine(objectToWrite.ToString(), _colorScheme.Comment);
System.Console.Write(objectToWrite.ToString());
}

public void WriteLessImportant(object objectToWrite)
public virtual void WriteKeyword(string keyword)
{
Colorful.Console.Write(objectToWrite.ToString(), GetColor(_colorScheme.LessImportant));
System.Console.Write(keyword);
}

public void WriteKeyword(string keyword)
public virtual void WriteInteresting(string interesting)
{
Colorful.Console.Write(keyword, GetColor(_colorScheme.Keyword));
System.Console.WriteLine(interesting);
}

public void WriteInteresting(string interesting)
public virtual void WriteLine()
{
Colorful.Console.WriteLine(interesting, GetColor(_colorScheme.Interesting));
System.Console.WriteLine();
}

public void WriteLine()
public virtual void WriteGood(string goodText)
{
Colorful.Console.WriteLine();
System.Console.WriteLine(goodText);
}

public void WriteGood(string goodText)
public virtual void WriteString(object result)
{
Colorful.Console.WriteLine(goodText, GetColor(_colorScheme.Good));
System.Console.WriteLine(result);
}

public void WriteString(object result)
public void ResetColor()
{
Colorful.Console.WriteLine(result, GetColor(_colorScheme.String));
System.Console.ResetColor();
}
}
112 changes: 112 additions & 0 deletions src/Nethermind/Nethermind.Cli/Console/ColorfulCliConsole.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Copyright (c) 2022 Demerzel Solutions Limited
// This file is part of the Nethermind library.
//
// The Nethermind library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The Nethermind library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Drawing;
using Nethermind.Core;

namespace Nethermind.Cli.Console;

public class ColorfulCliConsole : CliConsole
{
private static ColorScheme _colorScheme = DraculaColorScheme.Instance;

public ColorfulCliConsole(ColorScheme colorScheme)
{
_colorScheme = colorScheme;
Colorful.Console.BackgroundColor = colorScheme.BackgroundColor;
_terminal = PrepareConsoleForTerminal();

if (Terminal != Terminal.Cmder)
{
Colorful.Console.ForegroundColor = colorScheme.Text;
}

Colorful.Console.WriteLine("**********************************************", _colorScheme.Comment);
Colorful.Console.WriteLine();
Colorful.Console.WriteLine("Nethermind CLI {0}", GetColor(_colorScheme.Good), ProductInfo.Version);
Colorful.Console.WriteLine(" https://github.com/NethermindEth/nethermind", GetColor(_colorScheme.Interesting));
Colorful.Console.WriteLine(" https://nethermind.readthedocs.io/en/latest/", GetColor(_colorScheme.Interesting));
Colorful.Console.WriteLine();
Colorful.Console.WriteLine("powered by:", _colorScheme.Text);
Colorful.Console.WriteLine(" https://github.com/sebastienros/jint", GetColor(_colorScheme.Interesting));
Colorful.Console.WriteLine(" https://github.com/tomakita/Colorful.Console", GetColor(_colorScheme.Interesting));
Colorful.Console.WriteLine(" https://github.com/tonerdo/readline", GetColor(_colorScheme.Interesting));
Colorful.Console.WriteLine();
Colorful.Console.WriteLine("**********************************************", _colorScheme.Comment);
Colorful.Console.WriteLine();
}

private Color GetColor(Color defaultColor)
{
return _terminal == Terminal.LinuxBash ? _colorScheme.Text : defaultColor;
}

public override void WriteException(Exception e)
{
Colorful.Console.WriteLine(e.ToString(), GetColor(_colorScheme.ErrorColor));
}

public override void WriteErrorLine(string errorMessage)
{
Colorful.Console.WriteLine(errorMessage, GetColor(_colorScheme.ErrorColor));
}

public override void WriteLine(object objectToWrite)
{
Colorful.Console.WriteLine(objectToWrite.ToString(), _colorScheme.Text);
}

public override void Write(object objectToWrite)
{
Colorful.Console.Write(objectToWrite.ToString(), _colorScheme.Text);
}

public override void WriteCommentLine(object objectToWrite)
{
Colorful.Console.WriteLine(objectToWrite.ToString(), _colorScheme.Comment);
}

public override void WriteLessImportant(object objectToWrite)
{
Colorful.Console.Write(objectToWrite.ToString(), GetColor(_colorScheme.LessImportant));
}

public override void WriteKeyword(string keyword)
{
Colorful.Console.Write(keyword, GetColor(_colorScheme.Keyword));
}

public override void WriteInteresting(string interesting)
{
Colorful.Console.WriteLine(interesting, GetColor(_colorScheme.Interesting));
}

public override void WriteLine()
{
Colorful.Console.WriteLine();
}

public override void WriteGood(string goodText)
{
Colorful.Console.WriteLine(goodText, GetColor(_colorScheme.Good));
}

public override void WriteString(object result)
{
Colorful.Console.WriteLine(result, GetColor(_colorScheme.String));
}
}
4 changes: 4 additions & 0 deletions src/Nethermind/Nethermind.Cli/Console/ICliConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,9 @@ public interface ICliConsole
void WriteGood(string goodText);

void WriteString(object result);

void ResetColor();

Terminal Terminal { get; }
}
}
1 change: 1 addition & 0 deletions src/Nethermind/Nethermind.Cli/Nethermind.Cli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<ItemGroup>
<PackageReference Include="Colorful.Console" Version="1.2.15" />
<PackageReference Include="Jint" Version="2.11.58" />
<PackageReference Include="Microsoft.Extensions.CommandLineUtils" Version="1.1.1" />
<PackageReference Include="ReadLine" Version="2.0.1" />
</ItemGroup>

Expand Down
Loading

0 comments on commit f556e04

Please sign in to comment.