Improve formating
This commit is contained in:
@@ -4,4 +4,5 @@ public static class CustomStyles
|
||||
{
|
||||
public static Style Error { get; } = new Style(foreground: Color.Red);
|
||||
public static Style Note { get; } = new(foreground: Color.DarkSlateGray1);
|
||||
public static Style? Hex { get; } = new(foreground: Color.Blue);
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.ComponentModel;
|
||||
using System.Collections;
|
||||
using System.ComponentModel;
|
||||
using System.Reactive.Linq;
|
||||
using System.Reactive.Threading.Tasks;
|
||||
using JetBrains.Annotations;
|
||||
@@ -13,7 +14,7 @@ internal sealed class ReadPlcCommand : AsyncCommand<ReadPlcCommand.Settings>
|
||||
{
|
||||
public override async Task<int> ExecuteAsync(CommandContext context, Settings settings)
|
||||
{
|
||||
var token = (CancellationToken) (context.Data ?? CancellationToken.None);
|
||||
var token = (CancellationToken)(context.Data ?? CancellationToken.None);
|
||||
|
||||
try
|
||||
{
|
||||
@@ -28,23 +29,27 @@ internal sealed class ReadPlcCommand : AsyncCommand<ReadPlcCommand.Settings>
|
||||
|
||||
private static IRenderable FormatCellData(VariableRecord record)
|
||||
{
|
||||
if (record.Value is IRenderable renderable)
|
||||
return renderable;
|
||||
|
||||
if (record.Value is Exception ex)
|
||||
return new Text(ex.Message, CustomStyles.Error);
|
||||
|
||||
if (record.Value is byte[] byteArray)
|
||||
return record.Value switch
|
||||
{
|
||||
var text = string.Join(" ", byteArray.Select(b => $"0x{b:X2}"));
|
||||
return new Text(text);
|
||||
}
|
||||
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(),
|
||||
|
||||
return new Text(record.Value.ToString() ?? "");
|
||||
_ => new Text(record.Value.ToString() ?? "")
|
||||
};
|
||||
|
||||
Markup FormatNo() => new($"[blue]0x{record.Value:X2}[/] {record.Value}");
|
||||
}
|
||||
|
||||
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}[/]. ");
|
||||
AnsiConsole.MarkupLine("[gray]Press Ctrl + C to cancel.[/]");
|
||||
|
||||
@@ -104,11 +109,11 @@ internal sealed class ReadPlcCommand : AsyncCommand<ReadPlcCommand.Settings>
|
||||
await Task.Delay(100, token);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
[NoReorder]
|
||||
public sealed class Settings : CommandSettings
|
||||
{
|
||||
[NoReorder]
|
||||
public sealed class Settings : CommandSettings
|
||||
{
|
||||
[Description("IP address of S7")]
|
||||
[CommandArgument(0, "<IP address>")]
|
||||
public required string PlcIp { get; init; }
|
||||
@@ -137,5 +142,5 @@ internal sealed class ReadPlcCommand : AsyncCommand<ReadPlcCommand.Settings>
|
||||
|
||||
return ValidationResult.Success();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user