Exemple #1
0
        protected virtual void ValidateInput(string input, ref bool valid)
        {
            if (this.SingleCharacter)
            {
                CoEx.WriteLine();
            }

            if (this.AllowEmpty == false || !string.IsNullOrEmpty(input))
            {
                if (valid != false && this.ValidationRegex != null)
                {
                    valid = this.ValidationRegex.IsMatch(input);
                }

                if (valid != false && this.ValidateChoices && this.Choices != null && this.Choices.Length > 0)
                {
                    valid = this.Choices.Contains(input);
                }
            }

            if (valid == false && !string.IsNullOrWhiteSpace(this.ErrorMessage))
            {
                CoEx.WriteLine(this.ErrorMessage);
            }
        }
Exemple #2
0
 /// <summary>
 /// Clear progress bar
 /// </summary>
 protected void Clear()
 {
     if (this.StartY < CoEx.RealCursorY)
     {
         CoEx.Seek(0, (int)(this.StartY - CoEx.RealCursorY), true);
     }
     else
     {
         CoEx.Seek(0, null, true);
     }
 }
        public void Start()
        {
            var  message     = this.Message;
            bool writtenonce = false;

            this.CurrentThread = new Thread(() =>
            {
                try
                {
                    while (this.StopPending == false)
                    {
                        if (writtenonce)
                        {
                            CoEx.Seek(1, null);
                            CoEx.Write("{0}", this.Color, this.Animation.NextFrame());
                        }
                        else
                        {
                            this.Clear();
                            CoEx.Write(" {0} ", this.Color, this.Animation.NextFrame());
                            CoEx.Write(" {0} ", message);
                            writtenonce = true;
                        }

                        if (this.StopPending)
                        {
                            break;
                        }
                        Thread.Sleep(100);
                    }

                    this.Clear();
                    CoEx.Write(" {0} ", this.Color, LoadAnimation.ANIMATIONCOMPLETE);
                    CoEx.Write(" {0} ", message);
                    Thread.Sleep(500);
                }
                catch { }
                finally
                {
                    if (writtenonce)
                    {
                        this.Clear();
                    }
                }
            });

            this.CurrentThread.Start();
            this.IsRunning   = true;
            this.StopPending = false;

            CoEx.CursorVisible = false;
        }
Exemple #4
0
        public virtual string DoPrompt()
        {
            bool   valid = true;
            string temp  = null;

            do
            {
                valid = true;
                temp  = null;

                if (this.ChoicesText != null && this.ChoicesText.Select(v => v.Value).Any(v => v != null))
                {
                    int maxlen = this.ChoicesText.Select(v => v.Key.Length).Max();
                    foreach (var choice in this.ChoicesText)
                    {
                        CoEx.WriteLine("[{0}] {1}", choice.Key.PadRight(maxlen, ' '), choice.Value ?? "Unknown");
                    }
                    CoEx.WriteLine();
                }

                if (!string.IsNullOrWhiteSpace(this.Prefix))
                {
                    CoEx.Write(this.Prefix);
                }

                if (this.Choices != null && this.Choices.Length > 0)
                {
                    CoEx.Write(" (" + string.Join("/", this.Choices) + ")");
                }

                if (!string.IsNullOrEmpty(this.Default))
                {
                    CoEx.Write(" [" + this.Default + "]");
                }

                CoEx.Write(": ");

                temp = this.SingleCharacter ? CoEx.ReadKeyChar() : CoEx.ReadLine();

                if (!string.IsNullOrEmpty(this.Default) && string.IsNullOrEmpty(temp))
                {
                    temp = this.Default;
                }

                this.ValidateInput(temp, ref valid);
            }while (valid == false);

            return(temp);
        }
Exemple #5
0
        protected override void ValidateInput(string input, ref bool valid)
        {
            base.ValidateInput(input, ref valid);
            input = input.Trim().Trim(new char[] { '"', '\'' }).Trim();

            if (this.AllowEmpty == false || !string.IsNullOrEmpty(input))
            {
                if (this.ValidatePath && (this.AllowWildcard && input.Contains("*")) == false)
                {
                    try { new FileInfo(input); }
                    catch
                    {
                        valid = false;
                        CoEx.WriteLine(this.NoValidPathMessage);
                    }
                }

                if (valid != false && (this.AllowWildcard && input.Contains("*")) == false &&
                    this.ValidateFileExists && File.Exists(input) == false && Directory.Exists(input) == false)
                {
                    valid = false;
                    CoEx.WriteLine(this.FileNotFoundMessage);
                }

                if (valid != false && (this.AllowWildcard && input.Contains("*")) == false &&
                    this.Filetype != FILETYPE.BOTH && (File.Exists(input) || Directory.Exists(input)))
                {
                    FileAttributes attr = File.GetAttributes(input);
                    valid = (this.Filetype == FILETYPE.FILE && attr.HasFlag(FileAttributes.Directory) == false) ||
                            (this.Filetype == FILETYPE.DIR && attr.HasFlag(FileAttributes.Directory));

                    if (valid == false)
                    {
                        CoEx.WriteLine(this.NoValidFiletypeMessage);
                    }
                }
            }
        }
Exemple #6
0
        protected void PrintMessage(Msg msg)
        {
            string       level = "";
            ConsoleColor color = ConsoleColor.Gray;

            if (msg.Level == Msg.LEVEL.DEBUG)
            {
                color = ConsoleColor.Cyan;
                level = "DBG";
            }
            else if (msg.Level == Msg.LEVEL.INFO)
            {
                color = ConsoleColor.Blue;
                level = "INF";
            }
            else if (msg.Level == Msg.LEVEL.WARN)
            {
                color = ConsoleColor.Yellow;
                level = "WRN";
            }
            else if (msg.Level == Msg.LEVEL.ERROR)
            {
                color = ConsoleColor.Red;
                level = "ERR";
            }
            else if (msg.Level == Msg.LEVEL.SUCCESS)
            {
                color = ConsoleColor.Green;
                level = "SUC";
            }

            CoEx.SetColor(null, color);
            CoEx.Write("[{0}] ", level);
            CoEx.ResetColor();

            CoEx.WriteLine(msg.ToString());
        }
Exemple #7
0
        /// <summary>
        /// Draw the progress
        /// </summary>
        /// <param name="force">force drawing, ignore dirty state</param>
        protected void Draw(bool force)
        {
            double     cpercentage, crecentpercentage;
            bool       cisdirty, cismessages;
            List <Msg> cmsg;

            lock (this.instancelock)
            {
                cpercentage       = this.Percentage;
                crecentpercentage = this.RecentPercentage;
                cisdirty          = this.IsDirty;
                cismessages       = this.IsUsingMessages;
                cmsg = this.MessageQueue.ToList();

                this.RecentPercentage = this.Percentage;
                this.MessageQueue.Clear();
            }

            //--> Draw bar
            if (this.IsWaiting || cisdirty || force)
            {
                if (this.IsInitialized)
                {
                    CoEx.Seek(0, null, true);
                }

                // Draw messages
                if (cmsg.Count > 0)
                {
                    if (this.IsInitialized && this.IsUsingMessages)
                    {
                        CoEx.Seek(0, -1, true);
                    }

                    foreach (var msg in cmsg)
                    {
                        this.PrintMessage(msg);
                    }

                    this.IsUsingMessages = true;
                    CoEx.WriteLine();
                }

                string percstr    = this.IsWaiting ? PERCUNKNOWN : string.Format(PERCFORMAT, cpercentage);
                string loadingstr = string.Format(" {0} ", cpercentage >= 100 ? LoadAnimation.ANIMATIONCOMPLETE : this.Animation.NextFrame().ToString());
                int    barmax     = CoEx.Width - loadingstr.Length - 1 - percstr.Length - BARBEGIN.Length - BAREND.Length - 1;

                CoEx.Write(loadingstr, CoEx.ColorTitlePrimary);
                CoEx.Write(" " + percstr + BARBEGIN);

                if (this.IsWaiting)
                {
                    // Draw waiting animation
                    double progresslength   = Math.Round(barmax * WAITBARLENGTHPERC / 100.0);
                    int    whitespacelength = (int)(barmax - progresslength);

                    this.WaitingPadding += this.WaitingIncrement;
                    if (this.WaitingPadding > whitespacelength || this.WaitingPadding < 0)
                    {
                        this.WaitingIncrement = this.WaitingIncrement > 0 ? -1 : 1;
                        this.WaitingPadding  += this.WaitingIncrement;
                    }

                    string beforeprogress = "".PadLeft(this.WaitingPadding, BAREMPTY);
                    string progress       = "".PadLeft((int)progresslength, BARPROGRESS);
                    string afterprogress  = "".PadLeft(whitespacelength - this.WaitingPadding, BAREMPTY);

                    CoEx.Write(beforeprogress);
                    CoEx.Write(progress, CoEx.ColorTitleSecondary.Invert());
                    CoEx.Write(afterprogress);
                }
                else
                {
                    // Draw progress bar
                    int fill    = (int)Math.Round(barmax * crecentpercentage / 100.0);
                    int newfill = (int)Math.Round(barmax * (cpercentage - crecentpercentage) / 100.0);

                    string progress    = "".PadLeft(fill, BARPROGRESS);
                    string newprogress = "".PadLeft(newfill, BARPROGRESS);
                    string empty       = "".PadLeft(barmax - (fill + newfill), BAREMPTY);

                    CoEx.Write(progress, CoEx.ColorTitleSecondary.Invert());
                    CoEx.Write(newprogress, CoEx.ColorTitlePrimary.Invert());
                    CoEx.Write(empty);
                }

                CoEx.Write(BAREND);
                this.IsInitialized = true;

                // Set dirty status to false
                if (this.IsDirty)
                {
                    lock (this.instancelock)
                    {
                        this.IsDirty = false;
                    }
                }
            }
            //--> No action nessasary, only draw load indicator
            else if (cpercentage < 100)
            {
                string loadingstr = string.Format("{0}", cpercentage >= 100 ? LoadAnimation.ANIMATIONCOMPLETE : this.Animation.NextFrame().ToString());
                CoEx.Seek(1, null);
                CoEx.Write(loadingstr, CoEx.ColorTitlePrimary);
            }
        }
 public void Apply()
 {
     CoEx.SetColor(this.Background, this.Foreground);
 }
 public void ApplyBackground()
 {
     CoEx.SetColor(this.Background, null);
 }
Exemple #10
0
 public void ApplyForeground()
 {
     CoEx.SetColor(null, this.Foreground);
 }
Exemple #11
0
        public void Draw(Dictionary <string, double> points)
        {
            var pointKeys      = points.Keys.ToArray();
            var pointValues    = points.Values.ToArray();
            var maxpointlength = pointValues.Max().ToString().Length + 1;
            var maxpoints      = maxpointlength - 3 - 1;

            if (pointValues.Min() < 0)
            {
                throw new ArgumentException("Negative values are not supported right now");
            }

            if (pointValues.Length * 2 > CoEx.Width - maxpoints)
            {
                throw new ArgumentException($"Maximum supported number of points is {(CoEx.Width - maxpoints) / 2}");
            }

            // prepare matrix
            this.CreateMatrix(this.Height, pointValues.Length, out bool[][] pointMatrix, out List <List <double> > rowLabel);

            // fill matrix
            var max          = pointValues.Max();
            int matrixColumn = 0;

            foreach (var point in pointValues)
            {
                var rowsToFill = (int)Math.Round(this.Height / max * point);
                if (rowsToFill > 0)
                {
                    rowLabel[this.Height - rowsToFill].Add(point);
                }

                for (int matrixRow = this.Height - 1; matrixRow >= this.Height - rowsToFill; matrixRow--)
                {
                    pointMatrix[matrixRow][matrixColumn] = true;
                }

                matrixColumn++;
            }

            this.FixLabelGaps(ref rowLabel);

            // draw matrix
            CoEx.WriteLine($"{"".PadLeft(maxpointlength, ' ')} {this.LineHorizontalJointTop}");

            int graphRow = 0;

            bool[] columnStatus = new bool[pointValues.Length];
            foreach (var row in pointMatrix)
            {
                var label = "";
                if (rowLabel[graphRow].Count > 0)
                {
                    label = Math.Round(rowLabel[graphRow].Average()).ToString();
                }

                CoEx.Write($"{label.PadLeft(maxpointlength, ' ')} {this.LineVertical} ");

                var pointIndex = 0;
                foreach (var point in row.Select((v, i) => this.BlockParser(v, i, ref columnStatus, rowLabel[graphRow].ToArray(), pointValues[i])))
                {
                    CoEx.Write(point, pointIndex % 2 == 0 ? CoEx.ColorTitlePrimary.Invert() : CoEx.ColorTitleSecondary.Invert());
                    CoEx.Write(" ");
                    pointIndex++;
                }

                CoEx.WriteLine();

                graphRow++;
            }

            // draw x axis
            CoEx.Write($"{this.LineVerticalJointLeft}{"".PadLeft(maxpointlength, this.LineHorizontal)}{this.LineTJoint}{this.LineHorizontal}");
            for (int xi = 0; xi < pointValues.Length; xi++)
            {
                CoEx.Write($"{this.LineTJoint}{this.LineHorizontal}");
            }

            CoEx.WriteLine($"{this.LineVerticalJointRight}");

            this.CreateMatrix(pointKeys.Max(v => v.Length), pointKeys.Length, out string[][] columnLabelMatrix);

            // fill x axis
            for (int rowIndex = 0; rowIndex < columnLabelMatrix.Length; rowIndex++)
            {
                for (int colIndex = 0; colIndex < columnLabelMatrix[rowIndex].Length; colIndex++)
                {
                    columnLabelMatrix[rowIndex][colIndex] = pointKeys[colIndex].ToCharArray().Select(v => v.ToString()).ElementAtOrDefault(rowIndex) ?? " ";
                }
            }

            // draw x axis
            var labelRowIndex = 0;

            foreach (var labelRow in columnLabelMatrix)
            {
                var joint = labelRowIndex < 1 ? this.LineHorizontalJointBottom : ' ';
                CoEx.Write($"{"".PadLeft(maxpointlength, ' ')} {joint} ");

                var cellIndex = 0;
                foreach (var labelCell in labelRow)
                {
                    CoEx.Write(labelCell, cellIndex % 2 == 0 ? CoEx.ColorTitlePrimary.Invert() : CoEx.ColorTitleSecondary.Invert());
                    CoEx.Write(" ");
                    cellIndex++;
                }

                CoEx.WriteLine();
                labelRowIndex++;
            }
        }
 protected void Clear()
 {
     CoEx.Seek(0, CoEx.CursorY, true);
 }