示例#1
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="ProgressBar" /> class.
        /// </summary>
        /// <param name="spec">The specifications of the progress bar.</param>
        /// <param name="value">
        ///     The starting value of the progress bar. Defaults to the minimum value.
        /// </param>
        /// <param name="style">The style of the progress bar.</param>
        internal ProgressBar(ProgressBarSpec spec = null, int value = 0, ProgressBarStyle style = null)
        {
            _spec  = spec ?? new ProgressBarSpec();
            _style = style ?? new ProgressBarStyle();

            // Validate spec properties.
            if (_spec.MinValue >= _spec.MaxValue)
            {
                throw new ArgumentException("Progress bar minimum value cannot be greater or equal to the maximum value.", nameof(spec));
            }
            if (_style.Complete.Char == _style.Incomplete.Char)
            {
                throw new ArgumentException("Progress bar complete char cannot be the same as the incomplete char.", nameof(spec));
            }

            // Validate value param.
            if (value < _spec.MinValue)
            {
                _value = _spec.MinValue;
            }
            else if (value > _spec.MaxValue)
            {
                _value = _spec.MaxValue;
            }
            else
            {
                _value = value;
            }

            // Assign remaining properties.
            Line   = _spec.Line.GetValueOrDefault(Console.CursorTop);
            Column = _spec.Column.GetValueOrDefault(Console.CursorLeft);

            _placeholders = new Dictionary <string, string>(5, StringComparer.OrdinalIgnoreCase)
            {
                ["bar"]        = string.Empty,
                ["percentage"] = string.Empty,
                ["value"]      = value.ToString(),
                ["max"]        = _spec.MaxValue.ToString(),
                ["min"]        = _spec.MinValue.ToString(),
                ["status"]     = string.Empty,
            };

            // Display the progress bar with the initial value.
            Update(_value);

            // Move to the next line, so that the progress bar owns it's own line.
            Console.WriteLine();
        }
 public static ProgressBar ProgressBar(ProgressBarSpec spec = null, int value = 0, ProgressBarStyle style = null)
 {
     return(new ProgressBar(spec, value, style));
 }