Skip to content

Commit

Permalink
Move to file scope namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
rubo committed Oct 31, 2024
1 parent 8a3e895 commit 7d0e564
Show file tree
Hide file tree
Showing 9 changed files with 857 additions and 867 deletions.
239 changes: 119 additions & 120 deletions src/Nethermind/Nethermind.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,161 +16,160 @@
using Nethermind.Serialization.Json;

[assembly: InternalsVisibleTo("Nethermind.Cli.Test")]
namespace Nethermind.Cli
namespace Nethermind.Cli;

public static class Program
{
public static class Program
private static readonly Dictionary<string, ColorScheme> _availableColorSchemes = new(){ {"basic", BasicColorScheme.Instance },
{"dracula", DraculaColorScheme.Instance }};
private static readonly IJsonSerializer Serializer = new EthereumJsonSerializer();

public static void Main(string[] args)
{
private static readonly Dictionary<string, ColorScheme> _availableColorSchemes = new(){ {"basic", BasicColorScheme.Instance },
{"dracula", DraculaColorScheme.Instance }};
private static readonly IJsonSerializer Serializer = new EthereumJsonSerializer();
CliOption<string> colorSchemeOption = new("--colorScheme", "-cs")
{
Description = "Color Scheme. Possible values: Basic|Dracula",
HelpName = "colorScheme"
};
CliOption<string> nodeAddressOption = new("--address", "-a")
{
Description = "Node Address",
HelpName = "address"
};
CliRootCommand rootCommand = [colorSchemeOption, nodeAddressOption];

public static void Main(string[] args)
rootCommand.SetAction(parseResult =>
{
CliOption<string> colorSchemeOption = new("--colorScheme", "-cs")
string? colorSchemeValue = parseResult.GetValue(colorSchemeOption);
ColorScheme? cs;
ICliConsole cliConsole = colorSchemeValue is not null && (cs = MapColorScheme(colorSchemeValue)) is not null
? new ColorfulCliConsole(cs)
: new CliConsole();
var historyManager = new StatementHistoryManager(cliConsole, new FileSystem());
ILogManager logManager = new OneLoggerLogManager(new(new CliLogger(cliConsole)));
ICliEngine engine = new CliEngine(cliConsole);
INodeManager nodeManager = new NodeManager(engine, Serializer, cliConsole, logManager);
var moduleLoader = new CliModuleLoader(engine, nodeManager, cliConsole);
engine.JintEngine.SetValue("serialize", new Action<JsValue>(v =>
{
Description = "Color Scheme. Possible values: Basic|Dracula",
HelpName = "colorScheme"
};
CliOption<string> nodeAddressOption = new("--address", "-a")
{
Description = "Node Address",
HelpName = "address"
};
CliRootCommand rootCommand = [colorSchemeOption, nodeAddressOption];
string text = Serializer.Serialize(v.ToObject(), true);
cliConsole.WriteGood(text);
}));
rootCommand.SetAction(parseResult =>
{
string? colorSchemeValue = parseResult.GetValue(colorSchemeOption);
ColorScheme? cs;
ICliConsole cliConsole = colorSchemeValue is not null && (cs = MapColorScheme(colorSchemeValue)) is not null
? new ColorfulCliConsole(cs)
: new CliConsole();
var historyManager = new StatementHistoryManager(cliConsole, new FileSystem());
ILogManager logManager = new OneLoggerLogManager(new(new CliLogger(cliConsole)));
ICliEngine engine = new CliEngine(cliConsole);
INodeManager nodeManager = new NodeManager(engine, Serializer, cliConsole, logManager);
var moduleLoader = new CliModuleLoader(engine, nodeManager, cliConsole);
engine.JintEngine.SetValue("serialize", new Action<JsValue>(v =>
{
string text = Serializer.Serialize(v.ToObject(), true);
cliConsole.WriteGood(text);
}));
moduleLoader.DiscoverAndLoadModules();
ReadLine.AutoCompletionHandler = new AutoCompletionHandler(moduleLoader);
moduleLoader.DiscoverAndLoadModules();
ReadLine.AutoCompletionHandler = new AutoCompletionHandler(moduleLoader);
string nodeAddress = parseResult.GetValue(nodeAddressOption) ?? "http://localhost:8545";
nodeManager.SwitchUri(new Uri(nodeAddress));
historyManager.Init();
TestConnection(nodeManager, engine, cliConsole);
cliConsole.WriteLine();
RunEvalLoop(engine, historyManager, cliConsole);
string nodeAddress = parseResult.GetValue(nodeAddressOption) ?? "http://localhost:8545";
nodeManager.SwitchUri(new Uri(nodeAddress));
historyManager.Init();
TestConnection(nodeManager, engine, cliConsole);
cliConsole.WriteLine();
RunEvalLoop(engine, historyManager, cliConsole);
cliConsole.ResetColor();
return ExitCodes.Ok;
});

cliConsole.ResetColor();
return ExitCodes.Ok;
});
CliConfiguration cli = new(rootCommand);

CliConfiguration cli = new(rootCommand);
cli.Invoke(args);
}

cli.Invoke(args);
private static void TestConnection(INodeManager nodeManager, ICliEngine cliEngine, ICliConsole cliConsole)
{
cliConsole.WriteLine($"Connecting to {nodeManager.CurrentUri}");
JsValue result = cliEngine.Execute("web3.clientVersion");
if (result != JsValue.Null)
{
cliConsole.WriteGood("Connected");
}
}

private static void TestConnection(INodeManager nodeManager, ICliEngine cliEngine, ICliConsole cliConsole)
internal static string RemoveDangerousCharacters(string statement)
{
if (string.IsNullOrWhiteSpace(statement))
{
cliConsole.WriteLine($"Connecting to {nodeManager.CurrentUri}");
JsValue result = cliEngine.Execute("web3.clientVersion");
if (result != JsValue.Null)
{
cliConsole.WriteGood("Connected");
}
return "";
}

internal static string RemoveDangerousCharacters(string statement)
StringBuilder cleaned = new();
for (int i = 0; i < statement.Length; i++)
{
if (string.IsNullOrWhiteSpace(statement))
{
return "";
}

StringBuilder cleaned = new();
for (int i = 0; i < statement.Length; i++)
switch (statement[i])
{
switch (statement[i])
{
case '\x0008':
if (cleaned.Length != 0)
{
cleaned.Remove(cleaned.Length - 1, 1);
}
break;
case '\x0000':
return cleaned.ToString();
default:
cleaned.Append(statement[i]);
break;
}
case '\x0008':
if (cleaned.Length != 0)
{
cleaned.Remove(cleaned.Length - 1, 1);
}
break;
case '\x0000':
return cleaned.ToString();
default:
cleaned.Append(statement[i]);
break;
}

return cleaned.ToString();
}

private static void RunEvalLoop(ICliEngine engine, StatementHistoryManager historyManager, ICliConsole console)
return cleaned.ToString();
}

private static void RunEvalLoop(ICliEngine engine, StatementHistoryManager historyManager, ICliConsole console)
{
while (true)
{
while (true)
try
{
try
const int bufferSize = 1024 * 16;
string statement;
using (Stream inStream = System.Console.OpenStandardInput(bufferSize))
{
const int bufferSize = 1024 * 16;
string statement;
using (Stream inStream = System.Console.OpenStandardInput(bufferSize))
{
Colorful.Console.SetIn(new StreamReader(inStream, Colorful.Console.InputEncoding, false, bufferSize));
console.WriteLessImportant("nethermind> ");
statement = console.Terminal == Terminal.Cygwin ? Colorful.Console.ReadLine() : ReadLine.Read();
statement = RemoveDangerousCharacters(statement);

historyManager.UpdateHistory(statement);
}

if (statement == "exit")
{
break;
}
Colorful.Console.SetIn(new StreamReader(inStream, Colorful.Console.InputEncoding, false, bufferSize));
console.WriteLessImportant("nethermind> ");
statement = console.Terminal == Terminal.Cygwin ? Colorful.Console.ReadLine() : ReadLine.Read();
statement = RemoveDangerousCharacters(statement);

JsValue result = engine.Execute(statement);
WriteResult(console, result);
historyManager.UpdateHistory(statement);
}
catch (Exception e)

if (statement == "exit")
{
console.WriteException(e);
break;
}
}
}

private static void WriteResult(ICliConsole cliConsole, JsValue result)
{
if (result.IsObject() && result.AsObject().Class == "Function")
{
cliConsole.WriteGood(result.ToString());
cliConsole.WriteLine();
JsValue result = engine.Execute(statement);
WriteResult(console, result);
}
else if (!result.IsNull())
catch (Exception e)
{
string text = Serializer.Serialize(result.ToObject(), true);
cliConsole.WriteGood(text);
}
else
{
cliConsole.WriteLessImportant("null");
cliConsole.WriteLine();
console.WriteException(e);
}
}
}

private static ColorScheme? MapColorScheme(string colorSchemeOption)
private static void WriteResult(ICliConsole cliConsole, JsValue result)
{
if (result.IsObject() && result.AsObject().Class == "Function")
{
cliConsole.WriteGood(result.ToString());
cliConsole.WriteLine();
}
else if (!result.IsNull())
{
string text = Serializer.Serialize(result.ToObject(), true);
cliConsole.WriteGood(text);
}
else
{
return _availableColorSchemes.TryGetValue(colorSchemeOption.ToLower(), out var scheme) ? scheme : null;
cliConsole.WriteLessImportant("null");
cliConsole.WriteLine();
}
}

private static ColorScheme? MapColorScheme(string colorSchemeOption)
{
return _availableColorSchemes.TryGetValue(colorSchemeOption.ToLower(), out var scheme) ? scheme : null;
}
}
Loading

0 comments on commit 7d0e564

Please sign in to comment.