Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

User friendly cli console write system #812

Merged
merged 33 commits into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
e06b2bb
user friendly cli log system
Jim8y Aug 14, 2021
cfd6831
add log class
Jim8y Aug 14, 2021
49c9813
add more log to wallet
Jim8y Aug 14, 2021
0673677
fix format
Jim8y Aug 14, 2021
e9504f2
change `consolelog` to `consolewrite`
Jim8y Aug 15, 2021
48d32fd
remove `WriteLine`
Jim8y Aug 15, 2021
5aa1153
Update ConsoleWrite.cs
shargon Aug 17, 2021
935db80
static class
erikzhang Aug 18, 2021
3397d76
Rename
erikzhang Aug 18, 2021
99af593
use colorset
Jim8y Aug 18, 2021
b257e8f
Update ConsoleHelper.cs
erikzhang Aug 18, 2021
b7fddd5
Optimize
erikzhang Aug 18, 2021
eac96f6
Update
erikzhang Aug 18, 2021
8994602
Update MainService.Plugins.cs
erikzhang Aug 18, 2021
ad5eab6
remove "\r"
Jim8y Aug 18, 2021
ac46dec
fix `list asset` color
Jim8y Aug 18, 2021
9e1b985
check all console output with `ConsoleHelper`
Jim8y Aug 18, 2021
40cdc85
update more console output
Jim8y Aug 18, 2021
05b47c4
add more `ConsoleHelper.Info` output
Jim8y Aug 18, 2021
f65a00f
fix some output format.
Jim8y Aug 18, 2021
5589219
add comments
Jim8y Aug 19, 2021
3324a05
Update MainService.NEP17.cs
erikzhang Aug 19, 2021
80693a3
Merge branch 'master' into color-output
shargon Aug 20, 2021
d59e1cf
update comment
Jim8y Aug 21, 2021
398c9a2
Merge branch 'master' into color-output
Jim8y Aug 25, 2021
2ca1312
update log
Jim8y Aug 26, 2021
0c1ff4c
Merge branch 'master' into color-output
superboyiii Aug 30, 2021
f8acfbf
improve
superboyiii Aug 30, 2021
159b039
improve more
superboyiii Aug 31, 2021
1b9e552
fix
superboyiii Aug 31, 2021
3452ade
Optimize
erikzhang Aug 31, 2021
a8fe751
fix
superboyiii Aug 31, 2021
0f983f3
Merge branch 'color-output' of https://github.com/Liaojinghui/neo-nod…
superboyiii Aug 31, 2021
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
65 changes: 65 additions & 0 deletions Neo.ConsoleService/ConsoleHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;

namespace Neo.ConsoleService
{
public static class ConsoleHelper
{
private static readonly ConsoleColorSet InfoColor = new(ConsoleColor.Cyan);
private static readonly ConsoleColorSet WarningColor = new(ConsoleColor.Yellow);
private static readonly ConsoleColorSet ErrorColor = new(ConsoleColor.Red);

/// <summary>
/// Info handles message in the form of "[tag]:[msg]",
/// avoid using Info if the `tag` is too long
/// </summary>
/// <param name="values"></param>
Jim8y marked this conversation as resolved.
Show resolved Hide resolved
public static void Info(params string[] values)
{
var currentColor = new ConsoleColorSet();

for (int i = 0; i < values.Length; i++)
{
if (i % 2 == 0)
InfoColor.Apply();
else
currentColor.Apply();
Console.Write(values[i]);
}
currentColor.Apply();
Console.WriteLine();
}

/// <summary>
/// Use warning if something unexpected happens
/// or the execution result is not correct.
/// Also use warning if you just want to remind
/// user of doing something.
/// </summary>
/// <param name="msg"></param>
public static void Warning(string msg)
{
Log("Warning", WarningColor, msg);
}

/// <summary>
/// Use Error if the verification or input format check fails
/// or exception that breaks the execution of interactive
/// command throws.
/// </summary>
/// <param name="msg"></param>
public static void Error(string msg)
{
Log("Error", ErrorColor, msg);
}

private static void Log(string tag, ConsoleColorSet colorSet, string msg)
{
var currentColor = new ConsoleColorSet();

colorSet.Apply();
Console.Write($"{tag}: ");
currentColor.Apply();
Console.WriteLine(msg);
}
}
}
10 changes: 5 additions & 5 deletions Neo.ConsoleService/ConsoleServiceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ public void Run(string[] args)
{
if (Environment.OSVersion.Platform != PlatformID.Win32NT)
{
Console.WriteLine("Only support for installing services on Windows.");
ConsoleHelper.Warning("Only support for installing services on Windows.");
return;
}
string arguments = string.Format("create {0} start= auto binPath= \"{1}\"", ServiceName, Process.GetCurrentProcess().MainModule.FileName);
Expand All @@ -546,7 +546,7 @@ public void Run(string[] args)
{
if (Environment.OSVersion.Platform != PlatformID.Win32NT)
{
Console.WriteLine("Only support for installing services on Windows.");
ConsoleHelper.Warning("Only support for installing services on Windows.");
return;
}
Process process = Process.Start(new ProcessStartInfo
Expand Down Expand Up @@ -619,16 +619,16 @@ public virtual void RunConsole()
{
if (!OnCommand(line))
{
Console.WriteLine("error: Command not found");
ConsoleHelper.Error("Command not found");
}
}
catch (TargetInvocationException ex)
{
Console.WriteLine($"error: {ex.InnerException.Message}");
ConsoleHelper.Error(ex.InnerException.Message);
}
catch (Exception ex)
{
Console.WriteLine($"error: {ex.Message}");
ConsoleHelper.Error(ex.Message);
}
}

Expand Down
2 changes: 1 addition & 1 deletion neo-cli/CLI/MainService.Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ private void OnExportBlocksStartCountCommand(uint start, uint count = uint.MaxVa
uint height = NativeContract.Ledger.CurrentIndex(NeoSystem.StoreView);
if (height < start)
{
Console.WriteLine("Error: invalid start height.");
ConsoleHelper.Error("invalid start height.");
return;
}

Expand Down
30 changes: 16 additions & 14 deletions neo-cli/CLI/MainService.Contracts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ private void OnDeployCommand(string filePath, string manifestPath = null)

UInt160 hash = SmartContract.Helper.GetContractHash(tx.Sender, nef.CheckSum, manifest.Name);

Console.WriteLine($"Contract hash: {hash}");
Console.WriteLine($"Gas consumed: {new BigDecimal((BigInteger)tx.SystemFee, NativeContract.GAS.Decimals)}");
Console.WriteLine($"Network fee: {new BigDecimal((BigInteger)tx.NetworkFee, NativeContract.GAS.Decimals)}");
Console.WriteLine($"Total fee: {new BigDecimal((BigInteger)(tx.SystemFee + tx.NetworkFee), NativeContract.GAS.Decimals)} GAS");
ConsoleHelper.Info("Contract hash: ", $"{hash}");
ConsoleHelper.Info("Gas consumed: ", $"{new BigDecimal((BigInteger)tx.SystemFee, NativeContract.GAS.Decimals)}");
ConsoleHelper.Info("Network fee: ", $"{new BigDecimal((BigInteger)tx.NetworkFee, NativeContract.GAS.Decimals)}");
ConsoleHelper.Info("Total fee: ", $"{new BigDecimal((BigInteger)(tx.SystemFee + tx.NetworkFee), NativeContract.GAS.Decimals)} GAS");
if (!ReadUserInput("Relay tx? (no|yes)").IsYes()) // Add this in case just want to get hash but not relay
{
return;
Expand Down Expand Up @@ -98,22 +98,22 @@ private void OnUpdateCommand(UInt160 scriptHash, string filePath, string manifes
}
catch (InvalidOperationException e)
{
Console.WriteLine("Error: " + GetExceptionMessage(e));
ConsoleHelper.Error(GetExceptionMessage(e));
return;
}

ContractState contract = NativeContract.ContractManagement.GetContract(NeoSystem.StoreView, scriptHash);
if (contract == null)
{
Console.WriteLine($"Can't upgrade, contract hash not exist: {scriptHash}");
ConsoleHelper.Warning($"Can't upgrade, contract hash not exist: {scriptHash}");
}
else
{
Console.WriteLine($"Contract hash: {scriptHash}");
Console.WriteLine($"Updated times: {contract.UpdateCounter}");
Console.WriteLine($"Gas consumed: {new BigDecimal((BigInteger)tx.SystemFee, NativeContract.GAS.Decimals)}");
Console.WriteLine($"Network fee: {new BigDecimal((BigInteger)tx.NetworkFee, NativeContract.GAS.Decimals)}");
Console.WriteLine($"Total fee: {new BigDecimal((BigInteger)(tx.SystemFee + tx.NetworkFee), NativeContract.GAS.Decimals)} GAS");
ConsoleHelper.Info("Contract hash: ", $"{scriptHash}");
ConsoleHelper.Info("Updated times: ", $"{contract.UpdateCounter}");
ConsoleHelper.Info("Gas consumed: ", $"{new BigDecimal((BigInteger)tx.SystemFee, NativeContract.GAS.Decimals)}");
ConsoleHelper.Info("Network fee: ", $"{new BigDecimal((BigInteger)tx.NetworkFee, NativeContract.GAS.Decimals)}");
ConsoleHelper.Info("Total fee: ", $"{new BigDecimal((BigInteger)(tx.SystemFee + tx.NetworkFee), NativeContract.GAS.Decimals)} GAS");
if (!ReadUserInput("Relay tx? (no|yes)").IsYes()) // Add this in case just want to get hash but not relay
{
return;
Expand Down Expand Up @@ -169,11 +169,13 @@ private void OnInvokeCommand(UInt160 scriptHash, string operation, JArray contra
}
catch (InvalidOperationException e)
{
Console.WriteLine("Error: " + GetExceptionMessage(e));
ConsoleHelper.Error(GetExceptionMessage(e));
return;
}
Console.WriteLine($"Network fee: {new BigDecimal((BigInteger)tx.NetworkFee, NativeContract.GAS.Decimals)}");
Console.WriteLine($"Total fee: {new BigDecimal((BigInteger)(tx.SystemFee + tx.NetworkFee), NativeContract.GAS.Decimals)} GAS");
ConsoleHelper.Info("Network fee: ",
$"{new BigDecimal((BigInteger)tx.NetworkFee, NativeContract.GAS.Decimals)}",
"Total fee: ",
$"{new BigDecimal((BigInteger)(tx.SystemFee + tx.NetworkFee), NativeContract.GAS.Decimals)} GAS");
if (!ReadUserInput("Relay tx? (no|yes)").IsYes())
{
return;
Expand Down
8 changes: 4 additions & 4 deletions neo-cli/CLI/MainService.NEP17.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private void OnTransferCommand(UInt160 tokenHash, UInt160 to, decimal amount, UI
}
catch (InvalidOperationException e)
{
Console.WriteLine("Error: " + GetExceptionMessage(e));
ConsoleHelper.Error(GetExceptionMessage(e));
return;
}
if (!ReadUserInput("Relay tx(no|yes)").IsYes())
Expand Down Expand Up @@ -103,7 +103,7 @@ private void OnNameCommand(UInt160 tokenHash)
{
ContractState contract = NativeContract.ContractManagement.GetContract(NeoSystem.StoreView, tokenHash);
if (contract == null) Console.WriteLine($"Contract hash not exist: {tokenHash}");
else Console.WriteLine($"Result : {contract.Manifest.Name.ToString()}");
else ConsoleHelper.Info("Result: ", contract.Manifest.Name);
}

/// <summary>
Expand All @@ -115,7 +115,7 @@ private void OnDecimalsCommand(UInt160 tokenHash)
{
if (!OnInvokeWithResult(tokenHash, "decimals", out StackItem result, null)) return;

Console.WriteLine($"Result : {((PrimitiveType)result).GetInteger()}");
ConsoleHelper.Info("Result: ", $"{((PrimitiveType)result).GetInteger()}");
}

/// <summary>
Expand All @@ -130,7 +130,7 @@ private void OnTotalSupplyCommand(UInt160 tokenHash)
var asset = new AssetDescriptor(NeoSystem.StoreView, NeoSystem.Settings, tokenHash);
var totalSupply = new BigDecimal(((PrimitiveType)result).GetInteger(), asset.Decimals);

Console.WriteLine($"Result : {totalSupply}");
ConsoleHelper.Info("Result: ", $"{totalSupply}");
}
}
}
2 changes: 1 addition & 1 deletion neo-cli/CLI/MainService.Native.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ partial class MainService
[ConsoleCommand("list nativecontract", Category = "Native Contract")]
private void OnListNativeContract()
{
NativeContract.Contracts.ToList().ForEach(p => Console.WriteLine($"\t{p.Name,-20}{p.Hash}"));
NativeContract.Contracts.ToList().ForEach(p => ConsoleHelper.Info($"\t{p.Name,-20}", $"{p.Hash}"));
}
}
}
12 changes: 6 additions & 6 deletions neo-cli/CLI/MainService.Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private void OnBroadcastAddressCommand(IPAddress payload, ushort port)
{
if (payload == null)
{
Console.WriteLine("You must input the payload to relay.");
ConsoleHelper.Warning("You must input the payload to relay.");
return;
}

Expand Down Expand Up @@ -134,7 +134,7 @@ private void OnRelayCommand(JObject jsonObjectToRelay)
{
if (jsonObjectToRelay == null)
{
Console.WriteLine("You must input JSON object to relay.");
ConsoleHelper.Warning("You must input JSON object to relay.");
return;
}

Expand All @@ -143,21 +143,21 @@ private void OnRelayCommand(JObject jsonObjectToRelay)
ContractParametersContext context = ContractParametersContext.Parse(jsonObjectToRelay.ToString(), NeoSystem.StoreView);
if (!context.Completed)
{
Console.WriteLine("The signature is incomplete.");
ConsoleHelper.Error("The signature is incomplete.");
return;
}
if (!(context.Verifiable is Transaction tx))
{
Console.WriteLine($"Only support to relay transaction.");
ConsoleHelper.Warning("Only support to relay transaction.");
return;
}
tx.Witnesses = context.GetWitnesses();
NeoSystem.Blockchain.Tell(tx);
Console.WriteLine($"Data relay success, the hash is shown as follows:{Environment.NewLine}{tx.Hash}");
Console.WriteLine($"Data relay success, the hash is shown as follows: {Environment.NewLine}{tx.Hash}");
}
catch (Exception e)
{
Console.WriteLine("Error: " + GetExceptionMessage(e));
ConsoleHelper.Error(GetExceptionMessage(e));
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions neo-cli/CLI/MainService.Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,14 @@ private void OnShowStateCommand()
int linesWritten = 1;
foreach (RemoteNode node in LocalNode.GetRemoteNodes().OrderByDescending(u => u.LastBlockIndex).Take(Console.WindowHeight - 2).ToArray())
{
Console.WriteLine(
$" ip: {node.Remote.Address,-15}\tport: {node.Remote.Port,-5}\tlisten: {node.ListenerTcpPort,-5}\theight: {node.LastBlockIndex,-7}");
ConsoleHelper.Info(" ip: ",
$"{ node.Remote.Address,-15}\t",
"port: ",
$"{node.Remote.Port,-5}\t",
"listen: ",
$"{node.ListenerTcpPort,-5}\t",
"height: ",
$"{node.LastBlockIndex,-7}");
linesWritten++;
}

Expand Down
12 changes: 6 additions & 6 deletions neo-cli/CLI/MainService.Plugins.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private void OnInstallCommand(string pluginName)
try
{
zip.ExtractToDirectory(".");
Console.WriteLine($"Install successful, please restart neo-cli.");
ConsoleHelper.Warning("Install successful, please restart neo-cli.");
}
catch (IOException)
{
Expand All @@ -83,12 +83,12 @@ private void OnUnInstallCommand(string pluginName)
var plugin = Plugin.Plugins.FirstOrDefault(p => p.Name == pluginName);
if (plugin is null)
{
Console.WriteLine("Plugin not found");
ConsoleHelper.Warning("Plugin not found");
return;
}
if (plugin is Logger)
{
Console.WriteLine("You cannot uninstall a built-in plugin.");
ConsoleHelper.Error("You cannot uninstall a built-in plugin.");
return;
}

Expand All @@ -101,7 +101,7 @@ private void OnUnInstallCommand(string pluginName)
catch (IOException)
{
}
Console.WriteLine($"Uninstall successful, please restart neo-cli.");
ConsoleHelper.Warning("Uninstall successful, please restart neo-cli.");
}

/// <summary>
Expand All @@ -116,12 +116,12 @@ private void OnPluginsCommand()
foreach (Plugin plugin in Plugin.Plugins)
{
if (plugin is Logger) continue;
Console.WriteLine($"\t{plugin.Name,-20}{plugin.Description}");
ConsoleHelper.Info($"\t{plugin.Name,-20}", plugin.Description);
}
}
else
{
Console.WriteLine("No loaded plugins");
ConsoleHelper.Warning("No loaded plugins");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion neo-cli/CLI/MainService.Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private void OnParseCommand(string value)

if (!any)
{
Console.WriteLine($"Was not possible to convert: '{value}'");
ConsoleHelper.Warning($"Was not possible to convert: '{value}'");
}
}

Expand Down
Loading