Exemple #1
0
        public override string ToString()
        {
            object value = Object;
            string text  = null;

            if (Object is ScriptBlock)
            {
                value = ((ScriptBlock)Object).Invoke();
            }
            if (value is Block)
            {
                return(value.ToString());
            }

            if (Object is IEnumerable <ScriptBlock> )
            {
                text = AnsiHelper.GetString(((IEnumerable <ScriptBlock>)Object)
                                            .SelectMany(block => block.Invoke()
                                                        .Select(pso => pso.BaseObject))
                                            .Select(o => o is Block ? ((Block)o).ToString() : AnsiHelper.GetString(o)));
            }

            if (value is IEnumerable <Block> )
            {
                text = AnsiHelper.GetString(((IEnumerable <Block>)value).Select(block => block.ToString()));
            }

            if (value is IEnumerable <object> )
            {
                text = AnsiHelper.GetString(((IEnumerable <object>)value).Select(o => o is Block ? ((Block)o).ToString() : AnsiHelper.GetString(o)));
            }

            return(AnsiHelper.WriteAnsi(ForegroundColor, BackgroundColor, text ?? GetObjectText(), Clear));
        }
Exemple #2
0
 public override string ToString()
 {
     // If there's nothing but escape codes, don't bother outputting new colors
     if (Length == 0)
     {
         return((string)Object);
     }
     return(AnsiHelper.WriteAnsi(ForegroundColor, BackgroundColor, (string)Object));
 }
Exemple #3
0
        public string ToString(int width)
        {
            var columns = PreCalculateValues();

            var output = new StringBuilder();

            // Output each block with appropriate separators and caps
            for (int l = 0; l < columns.Count;)
            {
                var column = columns[l];
                // Use null columns as spacers
                if (column != null && column.Length > 0)
                {
                    string text = column.ToString(Prompt.Separator, Prompt.ColorSeparator);
                    output.Append(text);
                    output.Append(AnsiHelper.WriteAnsi(column.EndBackgroundColor, null, Prompt.ColorSeparator));
                }

                // Force the prompt location to the end of the first column
                output.Append(AnsiHelper.EscapeCodes.PromptLocation);

                // CURRENTLY we only support two columns, so ...
                // if there are more columns, the next one is right-aligned
                if (columns.Count > ++l)
                {
                    column = columns[l];
                    // Use null columns as spacers
                    if (column != null && column.Length > 0)
                    {
                        // Move to the start location for the next column
                        output.Append(AnsiHelper.EscapeCodes.Esc + (width - column.Length) + "G");

                        output.Append(AnsiHelper.WriteAnsi(column.StartBackgroundColor, null, Prompt.ReverseColorSeparator));
                        output.Append(column.ToString(Prompt.ReverseSeparator, Prompt.ReverseColorSeparator, true));
                    }
                }

                if (columns.Count > ++l)
                {
                    // Because we only support two columns, if there are still more columns, they must go on the next line
                    output.Append("\n");
                }
            }

            return(output.ToString());
        }
Exemple #4
0
        public string ToString(string separator, string colorSeparator, bool rightJustified = false)
        {
            // Initialize variables ...
            var output = new StringBuilder();

            PreCalculateValues();

            for (int l = 0; l < ValidBlocks.Length; l++)
            {
                var block = ValidBlocks[l];
                output.Append(block);

                // Write a separator between blocks, unless the next one has no (non-escape) text
                if (l < ValidBlocks.Length - 1 && ValidBlocks[l + 1].Length > 0)
                {
                    // if the colors are the same, use the separator
                    if (block.BackgroundColor == ValidBlocks[l + 1].BackgroundColor)
                    {
                        output.Append(separator);
                    }
                    // if they're different, use the colorSeparator
                    else
                    {
                        if (rightJustified)
                        {
                            output.Append(AnsiHelper.WriteAnsi(ValidBlocks[l + 1].BackgroundColor, block.BackgroundColor, colorSeparator));
                        }
                        else
                        {
                            output.Append(AnsiHelper.WriteAnsi(block.BackgroundColor, ValidBlocks[l + 1].BackgroundColor, colorSeparator));
                        }
                    }
                }
            }
            // clear colors at the end of each column
            output.Append(AnsiHelper.Foreground["Default"]);
            output.Append(AnsiHelper.Background["Default"]);
            return(output.ToString());
        }
Exemple #5
0
        public override string ToString()
        {
            // Initialize variables ...
            var width       = Console.BufferWidth;
            var leftLength  = 0;
            var rightLength = 0;

            // Precalculate all the text and remove empty blocks
            var ValidBlocks = this.Select(e => e.Cache()).Where(e => e.Length > 0).ToArray();

            var output = new StringBuilder();

            // Output each block with appropriate separators and caps
            for (int l = 0; l < ValidBlocks.Length; l++)
            {
                var block = ValidBlocks[l];

                // Console.WriteLine("Is '" + block + "' a Column? " + BlockCache.Column.Equals(block) + " or a Prompt? " + BlockCache.Prompt.Equals(block));
                if (BlockCache.Column.Equals(block))
                {
                    // the length of the second column
                    rightLength = ValidBlocks.Skip(l + 1).Sum(e => e.Length + 1) - 1;

                    var space = width - rightLength;

                    // Output a cap on the left if there isn't one already
                    if (l > 0 && !BlockCache.Prompt.Equals(ValidBlocks[l - 1]))
                    {
                        // Use the Background of the previous block as the foreground
                        output.Append(AnsiHelper.WriteAnsi(ValidBlocks[l - 1].BackgroundColor, null, Block.LeftCap, true));
                    }

                    output.Append(AnsiHelper.EscapeCodes.ESC + space + "G");

                    if (l < ValidBlocks.Length)
                    {
                        // the right cap uses the background of the next block as it's foreground
                        output.Append(AnsiHelper.WriteAnsi(ValidBlocks[l + 1].BackgroundColor, null, Block.RightCap));
                    }
                }
                else if (BlockCache.Prompt.Equals(block))
                {
                    output.Append(block.ToString());
                }
                else
                {
                    if (leftLength == 0 && rightLength == 0)
                    {
                        // On a new line, recalculate the length of the "left-aligned" line
                        leftLength = ValidBlocks.TakeWhile(e => !BlockCache.Column.Equals(e)).Sum(e => e.Length + 1);
                    }

                    output.Append(block.ToString());

                    // Write a separator between blocks
                    if (l + 1 < ValidBlocks.Length && !BlockCache.Column.Equals(ValidBlocks[l + 1]))
                    {
                        // if the next block is the sambe background color, use a >
                        if (block.BackgroundColor == ValidBlocks[l + 1].BackgroundColor)
                        {
                            output.Append(rightLength > 0 ? Block.RightSep : Block.LeftSep);
                        }
                        else
                        {
                            if (rightLength > 0)
                            {
                                output.Append(AnsiHelper.WriteAnsi(ValidBlocks[l + 1].BackgroundColor, block.BackgroundColor, Block.RightCap));
                            }
                            else
                            {
                                output.Append(AnsiHelper.WriteAnsi(block.BackgroundColor, ValidBlocks[l + 1].BackgroundColor, Block.LeftCap));
                            }
                        }
                    }
                }
            }

            // Output a cap on the left if we didn't already
            if (rightLength == 0 && leftLength > 0)
            {
                output.Append(AnsiHelper.WriteAnsi(ValidBlocks.Last().BackgroundColor, null, Block.LeftCap, true));
            }
            // clear the end of each line in case it's not part of a prompt.
            output.Append(AnsiHelper.Foreground["Clear"]);
            output.Append(AnsiHelper.Background["Clear"]);
            return(output.ToString());
        }
Exemple #6
0
 public override string ToString()
 {
     return(AnsiHelper.WriteAnsi(ForegroundColor, BackgroundColor, Object, Clear));
 }