Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.0.2 #3

Merged
merged 5 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
20 changes: 12 additions & 8 deletions ConsoleUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace OrnaLibs
{
public static class ConsoleUI

Check warning on line 6 in ConsoleUI.cs

View workflow job for this annotation

GitHub Actions / publish

Missing XML comment for publicly visible type or member 'ConsoleUI'
{
public static string TextBox(string title, string defaultValue = "", Predicate<string>? validate = null)

Check warning on line 8 in ConsoleUI.cs

View workflow job for this annotation

GitHub Actions / publish

Missing XML comment for publicly visible type or member 'ConsoleUI.TextBox(string, string, Predicate<string>?)'
{
string res;
do
Expand All @@ -17,7 +17,7 @@
return res;
}

public static int NumberBox(string title, int defaultValue = 0, int min = 0, int max = 100)

Check warning on line 20 in ConsoleUI.cs

View workflow job for this annotation

GitHub Actions / publish

Missing XML comment for publicly visible type or member 'ConsoleUI.NumberBox(string, int, int, int)'
{
Console.Clear();
Console.Write($"{title} [{min}{(defaultValue == 0?"":$"..{defaultValue}")}..{max}]: ");
Expand All @@ -27,7 +27,7 @@
return NumberBox(title, defaultValue, min, max);
}

public static void Error(string text)

Check warning on line 30 in ConsoleUI.cs

View workflow job for this annotation

GitHub Actions / publish

Missing XML comment for publicly visible type or member 'ConsoleUI.Error(string)'
{
Console.Clear();
Console.ForegroundColor = ConsoleColor.Red;
Expand All @@ -35,7 +35,7 @@
Console.ForegroundColor = ConsoleColor.White;
}

public static int Select(string title, string[] items)

Check warning on line 38 in ConsoleUI.cs

View workflow job for this annotation

GitHub Actions / publish

Missing XML comment for publicly visible type or member 'ConsoleUI.Select(string, string[])'
{
if(items.Length == 0) throw new ArgumentNullException("items");
Console.CursorVisible = false;
Expand Down Expand Up @@ -74,7 +74,7 @@
Console.Write('>');
}

public static DateOnly DateBox(string title, string format = "yyyy'/'MM'/'dd")

Check warning on line 77 in ConsoleUI.cs

View workflow job for this annotation

GitHub Actions / publish

Missing XML comment for publicly visible type or member 'ConsoleUI.DateBox(string, string)'
{
DateOnly d;
string? res;
Expand All @@ -88,14 +88,18 @@
return d;
}

public static TimeRange TimeRangeBox(string title)
public static TimeOnly TimeBox(string title, string format = "HH:mm")

Check warning on line 91 in ConsoleUI.cs

View workflow job for this annotation

GitHub Actions / publish

Missing XML comment for publicly visible type or member 'ConsoleUI.TimeBox(string, string)'
{
Console.Clear();
Console.Write($"{title} (Формат: HH:MM-HH:MM): ");
var res = Console.ReadLine();
if (res is null) return new TimeRange();
if (TimeRange.TryParse(res, out var d)) return d;
return TimeRangeBox(title);
TimeOnly t;
string? res;
do
{
Console.Clear();
Console.Write($"{title} (Формат: {format.Replace("'", "").ToUpper()}): ");
res = Console.ReadLine();
if (res is null) return TimeOnly.FromDateTime(DateTime.Today);
} while (!TimeOnly.TryParseExact(res, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out t));
return t;
}

public static bool Ask(string title, bool defaultValue)
Expand All @@ -113,7 +117,7 @@
Console.SetCursorPosition(pos, 0);
} while (!waitKeys.Contains(key));

return (key) switch
return key switch
{
(ConsoleKey.L) => true,
(ConsoleKey.Y) => false,
Expand Down
44 changes: 0 additions & 44 deletions DataTypes/TimeRange.cs

This file was deleted.

2 changes: 0 additions & 2 deletions RegExp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,5 @@ internal static partial class RegExp
{
[GeneratedRegex(@"^(?<user>[\w\d-\.]+)@(?<domain>([\w-]+\.)+[\w-]{2,4})$")]
public static partial Regex Email();
[GeneratedRegex(@"^(?<begin>([0-1]\d|2[0-3])\:[0-5]\d)\-(?<end>([0-1]\d|2[0-4])\:[0-5]\d)$")]
public static partial Regex TimeRange();
}
}
34 changes: 34 additions & 0 deletions Utils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using OrnaLibs.DataTypes;

namespace OrnaLibs
{
public static partial class Utils
{
#pragma warning disable CA1416
public static SerialPort[] GetSerialPorts() =>
Environment.OSVersion.Platform switch
{
PlatformID.Win32NT => GetSerialPortsWindows(),
_ => []
};

/// <summary>
/// Создание автоматически-запускаемого сервиса
/// </summary>
/// <param name="id">Идентификатор сервиса</param>
/// <param name="name">Отображаемое имя сервиса</param>
/// <param name="path">Путь к исполняемому файла сервиса</param>
public static void CreateService(string id, string name, string path)
{
switch (Environment.OSVersion.Platform)
{
case PlatformID.Win32NT:
CreateWindowsService(id, name, path);
break;
default:
break;
}
}
#pragma warning restore CA1416
}
}
13 changes: 12 additions & 1 deletion Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using OrnaLibs.DataTypes;
using System.Diagnostics;
using System.Runtime.Versioning;
using System.Text;

namespace OrnaLibs
{
Expand All @@ -25,7 +26,7 @@ public static void PowerShell(string script)
}

[SupportedOSPlatform("windows")]
public static SerialPort[] GetSerialPorts()
private static SerialPort[] GetSerialPortsWindows()
{
using var registry = Registry.LocalMachine.OpenSubKey(@"HARDWARE\DEVICEMAP\SERIALCOMM")!;
var names = registry.GetValueNames();
Expand All @@ -34,5 +35,15 @@ public static SerialPort[] GetSerialPorts()
ports[i] = new SerialPort((string)registry.GetValue(names[i])!, names[i].Split('\\')[^1]);
return ports;
}
[SupportedOSPlatform("windows")]
private static void CreateWindowsService(string name, string displayName, string path)
{
var builder = new StringBuilder("New-Service");
builder.AppendFormat(" -Name \"{0}\"", name);
builder.AppendFormat(" -DisplayName \"{0}\"", displayName);
builder.AppendFormat(" -BinaryPathName \"{0}\"", path);
builder.AppendFormat(" -StartupType \"{0}\"", "Automatic");
PowerShell(builder.ToString());
}
}
}
Loading