diff --git a/Sharp7.Monitor/ReadPlcCommand.cs b/Sharp7.Monitor/ReadPlcCommand.cs index dbcd2e9..207d07b 100644 --- a/Sharp7.Monitor/ReadPlcCommand.cs +++ b/Sharp7.Monitor/ReadPlcCommand.cs @@ -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 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 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(); diff --git a/Sharp7.Monitor/RenderUtil.cs b/Sharp7.Monitor/RenderUtil.cs new file mode 100644 index 0000000..db07b61 --- /dev/null +++ b/Sharp7.Monitor/RenderUtil.cs @@ -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() + ) + ); + } +}