vaspo wrote in nullzone

.Net: Console progress


Для отображения прогресса в консольных приложениях удобно использовать, как минимум проценты выполнения работы. Для этого есть куча решений, на основе одного из которых я добавил себе вот такой класс:

Code:

{
    using System;

    public class ConsoleSpiner : IDisposable
    {
        #region Constants and Fields

        private int frame;
        private readonly string [] frames = new [] { "|", "/", "-", @"\", "|", "/" }; 

        #endregion

        #region Constructors and Destructors

        public ConsoleSpiner()
        {
            this.frame = 0;
            Console.CursorVisible = false;
        }

        #endregion

        #region Public Methods

        public void Turn()
        {
            Console.Write(frames[frame++]);
            Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
            if (frame >= frames.Length)
            {
                frame = 0;
            }
        }

        #endregion

        public void Dispose()
        {
            Console.CursorVisible = true;
        }

        public void Turn(int current, int total)
        {
            int percent = (100 * (current + 1)) / total;
            Console.Write(string.Format("{0}% {1}", percent, frames[frame++]).PadRight(8));
            Console.SetCursorPosition(Console.CursorLeft - 8, Console.CursorTop);
            if (frame >= frames.Length)
            {
                frame = 0;
            }
        }
    }
}