Exemplo n.º 1
0
    /// <summary>
    /// Writes a collection of item for selecting.
    /// </summary>
    /// <param name="cli">The command line interface proxy.</param>
    /// <param name="path">The parent foler path.</param>
    /// <param name="options">The selection display options.</param>
    /// <param name="searchPattern">The search string to match against the names of directories and files. This parameter can contain a combination of valid literal path and wildcard (* and ?) characters, but it doesn't support regular expressions.</param>
    /// <returns>The result of selection.</returns>
    /// <exception cref="ArgumentException">searchPattern contains one or more invalid characters defined by the System.IO.Path.GetInvalidPathChars method.</exception>
    /// <exception cref="DirectoryNotFoundException">The specified path is invalid (for example, it is on an unmapped drive).</exception>
    /// <exception cref="SecurityException">The caller does not have the required permission.</exception>
    public static SelectionResult <FileSystemInfo> Select(this StyleConsole cli, DirectoryInfo path, SelectionConsoleOptions options = null, string searchPattern = null)
    {
        var c   = new SelectionData <FileSystemInfo>();
        var col = string.IsNullOrEmpty(searchPattern) ? path.GetFileSystemInfos() : path.GetFileSystemInfos(searchPattern);

        foreach (var f in col)
        {
            c.Add(f.Name, f);
        }

        return(Select(cli, c, options));
    }
Exemplo n.º 2
0
    /// <summary>
    /// Writes a collection of item for selecting.
    /// </summary>
    /// <param name="cli">The command line interface proxy.</param>
    /// <param name="path">The parent foler path.</param>
    /// <param name="predicate">A function to test each element for a condition.</param>
    /// <param name="options">The selection display options.</param>
    /// <returns>The result of selection.</returns>
    /// <exception cref="DirectoryNotFoundException">The specified path is invalid (for example, it is on an unmapped drive).</exception>
    /// <exception cref="SecurityException">The caller does not have the required permission.</exception>
    public static SelectionResult <FileSystemInfo> Select(this StyleConsole cli, DirectoryInfo path, Func <FileSystemInfo, bool> predicate, SelectionConsoleOptions options = null)
    {
        var c = new SelectionData <FileSystemInfo>();
        IEnumerable <FileSystemInfo> col = path.GetFileSystemInfos();

        if (predicate != null)
        {
            col = col.Where(predicate);
        }
        foreach (var f in col)
        {
            c.Add(f.Name, f);
        }

        return(Select(cli, c, options));
    }
Exemplo n.º 3
0
        /// <inheritdoc />
        protected override async Task OnProcessAsync(CancellationToken cancellationToken = default)
        {
            await RunAsync(null, cancellationToken);

            var cli = StyleConsole.Default;
            var col = new SelectionData();

            col.Add('a', "Aaa", null);
            col.Add('b', "Bbb 二号");
            col.Add('p', "Password");
            col.Add('l', "Progress");
            col.Add('j', "JSON");
            col.Add('e', "Error");
            col.Add('?', "Help", null);
            for (var i = 0; i < 16; i++)
            {
                var mix = (Drawing.ColorMixTypes)i;
                col.Add(mix.ToString(), mix);
            }

            col.Add("Saturate");
            col.Add("Brightness");
            col.Add("Invert");
            for (var i = 0; i < 120; i++)
            {
                col.Add(i.ToString());
            }

            var result = cli.Select(col, new SelectionConsoleOptions
            {
                SelectedBackgroundConsoleColor = ConsoleColor.Cyan,
                SelectedForegroundConsoleColor = ConsoleColor.Black,
                TipsForegroundConsoleColor     = ConsoleColor.Yellow,
                SelectedPrefix = "→ ",
                Column         = 5,
                MaxRow         = 10,
                ManualQuestion = "Type: "
            });

            if (result.IsCanceled)
            {
                cli.WriteLine("The operation is cancelled.");
                return;
            }

            if (result.IsNotSupported)
            {
                cli.WriteLine("The selection control is not supported.");
                return;
            }

            cli.WriteLine("-> {0}\t{1}", result.Value, result.InputType);
            switch ((result.Title ?? result.Value)?.ToLowerInvariant())
            {
            case "aaa":
            case "bbb":
                cli.WriteLine(result.Value + '\t' + result.Title + '\t' + result.InputType);
                return;

            case "password":
                cli.Write("Type password: "******"Your password is ");
                cli.Write(ConsoleColor.Magenta, Security.SecureStringExtensions.ToUnsecureString(p));
                cli.WriteLine('.');
                return;

            case "progress":
                await ShowProgressAsync();

                return;

            case "json":
                cli.WriteLine(JsonUnitTest.CreateModel(DateTime.Now));
                return;

            case "error":
                cli.WriteLine(new InvalidOperationException("Test", new NotSupportedException()));
                break;

            case "saturate":
                InitColors();
                for (var i = 0; i < 11; i++)
                {
                    var percent = i * 0.2;
                    foreach (var item in saturateColors)
                    {
                        cli.Write(Drawing.ColorCalculator.Saturate(item, percent), "■");
                    }

                    cli.WriteLine(percent.ToString("0%"));
                }

                for (var i = 0; i < 7; i++)
                {
                    var saturate = (Drawing.RelativeSaturationLevels)i;
                    foreach (var item in saturateColors)
                    {
                        cli.Write(Drawing.ColorCalculator.Saturate(item, saturate), "■");
                    }

                    cli.WriteLine(saturate.ToString());
                }

                break;

            case "brightness":
                InitColors();
                for (var i = -4; i < 5; i++)
                {
                    var percent = i * 0.2;
                    foreach (var item in saturateColors)
                    {
                        cli.Write(Drawing.ColorCalculator.Lighten(item, percent), "■");
                    }

                    cli.WriteLine(percent.ToString("0%"));
                }

                for (var i = 0; i < 7; i++)
                {
                    var brightness = (Drawing.RelativeBrightnessLevels)i;
                    foreach (var item in saturateColors)
                    {
                        cli.Write(Drawing.ColorCalculator.ToggleBrightness(item, brightness), "■");
                    }

                    cli.WriteLine(brightness.ToString());
                }

                break;

            case "invert":
                InitColors();
                foreach (var item in saturateColors)
                {
                    cli.Write(Drawing.ColorCalculator.Invert(item), "■");
                }

                cli.WriteLine("Invert");
                for (var i = 0; i < 6; i++)
                {
                    var percent = i * 0.2;
                    foreach (var item in saturateColors)
                    {
                        cli.Write(Drawing.ColorCalculator.Invert(item, percent), "■");
                    }

                    cli.WriteLine(percent.ToString("0%"));
                }

                break;

            case "help":
            case "?":
                cli.WriteLine("This is a sample.");
                return;
            }

            if (result.Data is Drawing.ColorMixTypes mixType)
            {
                InitColors();
                cli.Write("Blend");
                foreach (var item in blendColors)
                {
                    cli.Write(item, "■");
                }

                cli.WriteLine();
                cli.Write("Base ");
                foreach (var item in baseColors)
                {
                    cli.Write(item, "■");
                }

                cli.WriteLine();
                cli.Write("Mixed");
                var colors = Drawing.ColorCalculator.Mix(mixType, blendColors, baseColors);
                foreach (var item in colors)
                {
                    cli.Write(item, "■");
                }
            }
        }