summaryrefslogtreecommitdiffstats
path: root/src/main.lib/Services/Interfaces/IInputService.cs
blob: f911beb824c1089ad2e6ea44f7bc0be140aef075 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace PKISharp.WACS.Services
{
    public interface IInputService
    {
        Task<TResult?> ChooseOptional<TSource, TResult>(string what, IEnumerable<TSource> options, Func<TSource, Choice<TResult?>> creator, string nullChoiceLabel) where TResult : class;
        Task<TResult> ChooseRequired<TSource, TResult>(string what, IEnumerable<TSource> options, Func<TSource, Choice<TResult>> creator);
        Task<TResult> ChooseFromMenu<TResult>(string what, List<Choice<TResult>> choices, Func<string, Choice<TResult>>? unexpected = null);
        Task<bool> PromptYesNo(string message, bool defaultOption);
        Task<string?> ReadPassword(string what);
        Task<string> RequestString(string what);
        Task<string> RequestString(string[] what);
        void CreateSpace();
        void Show(string? label, string? value = null, int level = 0);
        Task<bool> Wait(string message = "Press <Enter> to continue");
        Task WritePagedList(IEnumerable<Choice> listItems);
        string FormatDate(DateTime date);
    }


    public class Choice
    {
        public static Choice<TItem> Create<TItem>(
            TItem item,
            string? description = null,
            string? command = null,
            bool @default = false,
            (bool, string?)? disabled = null,
            ConsoleColor? color = null)
        {
            var newItem = new Choice<TItem>(item);
            if (disabled == null)
            {
                disabled = (false, null);
            }
            // Default description is item.ToString, but it may 
            // be overruled by the optional parameter here
            if (!string.IsNullOrEmpty(description))
            {
                newItem.Description = description;
            }
            newItem.Command = command;
            newItem.Color = color;
            newItem.Disabled = disabled.Value.Item1;
            newItem.DisabledReason = disabled.Value.Item2;
            newItem.Default = @default;
            return newItem;
        }

        public string? Command { get; set; }
        public string? Description { get; set; }
        public bool Default { get; set; }
        public bool Disabled { get; set; }
        public string? DisabledReason { get; set; }
        public ConsoleColor? Color { get; set; }
    }

    public class Choice<T> : Choice
    {
        public Choice(T item)
        {
            Item = item;
            if (item != null)
            {
                Description = item.ToString();
            }
        }
        public T Item { get; }
    }
}