Improve output formating

This commit is contained in:
Peter-B-
2024-05-14 20:49:47 +02:00
parent bc619259b8
commit eacb494794
2 changed files with 45 additions and 24 deletions

View File

@@ -6,7 +6,6 @@ using Sharp7.Rx;
using Sharp7.Rx.Enums;
using Spectre.Console;
using Spectre.Console.Cli;
using Spectre.Console.Rendering;
namespace Sharp7.Monitor;
@@ -31,27 +30,6 @@ internal sealed class ReadPlcCommand : AsyncCommand<ReadPlcCommand.Settings>
return 0;
}
private static IRenderable FormatCellData(object value)
{
return value switch
{
IRenderable renderable => renderable,
Exception ex => new Text(ex.Message, CustomStyles.Error),
byte[] byteArray => new Text(string.Join(" ", byteArray.Select(b => $"0x{b:X2}")), CustomStyles.Hex),
byte => FormatNo(),
short => FormatNo(),
ushort => FormatNo(),
int => FormatNo(),
uint => FormatNo(),
long => FormatNo(),
ulong => FormatNo(),
_ => new Text(value.ToString() ?? "", CustomStyles.Default)
};
Markup FormatNo() => new($"[lightgoldenrod2_1]0x{value:X2}[/] {value}", CustomStyles.Default);
}
private static async Task RunProgram(Settings settings, CancellationToken token)
{
AnsiConsole.MarkupLine($"Connecting to plc [green]{settings.PlcIp}[/], CPU [green]{settings.CpuMpiAddress}[/], rack [green]{settings.RackNumber}[/]. ");
@@ -105,8 +83,7 @@ internal sealed class ReadPlcCommand : AsyncCommand<ReadPlcCommand.Settings>
foreach (var record in variableContainer.VariableRecords)
if (record.HasUpdate(out var value))
table.Rows.Update(
record.RowIdx, 1,
FormatCellData(value)
record.RowIdx, 1, RenderUtil.FormatCellData(value)
);
ctx.Refresh();

View File

@@ -0,0 +1,44 @@
using Spectre.Console;
using Spectre.Console.Rendering;
namespace Sharp7.Monitor;
internal static class RenderUtil
{
public static IRenderable FormatCellData(object value)
{
return value switch
{
IRenderable renderable => renderable,
Exception ex => new Text(ex.Message, CustomStyles.Error),
byte[] byteArray => FormatByteArray(byteArray),
byte => FormatNo(),
short => FormatNo(),
ushort => FormatNo(),
int => FormatNo(),
uint => FormatNo(),
long => FormatNo(),
ulong => FormatNo(),
_ => new Text(value.ToString() ?? "", CustomStyles.Default)
};
IRenderable FormatNo() => new Paragraph()
.Append($"0x{value:X2}", CustomStyles.Hex)
.Append(" ")
.Append($"{value}", CustomStyles.Default)
;
IRenderable FormatByteArray(byte[] byteArray) =>
new Paragraph()
.Append("0x " + string.Join(" ", byteArray.Select(b => $"{b:X2}")), CustomStyles.Hex)
.Append(Environment.NewLine)
.Append(new string(
byteArray
.Select(b => (char) b)
.Select(c => char.IsControl(c) ? '·' : c)
.ToArray()
)
);
}
}