Exemplo n.º 1
0
        private static string GenerateRowField(string val, int width, int alignment, DisplayCells dc)
        {
            // make sure the string does not have any embedded <CR> in it
            string s = StringManipulationHelper.TruncateAtNewLine(val) ?? "";

            string currentValue = s;
            int    currentValueDisplayLength = dc.Length(currentValue);

            if (currentValueDisplayLength < width)
            {
                // the string is shorter than the width of the column
                // need to pad with with blanks to reach the desired width
                int padCount = width - currentValueDisplayLength;
                switch (alignment)
                {
                case TextAlignment.Right:
                {
                    s = new string(' ', padCount) + s;
                }
                break;

                case TextAlignment.Center:
                {
                    // add a bit at both ends of the string
                    int padLeft  = padCount / 2;
                    int padRight = padCount - padLeft;

                    s = new string(' ', padLeft) + s + new string(' ', padRight);
                }
                break;

                default:
                {
                    // left align is the default
                    s += new string(' ', padCount);
                }
                break;
                }
            }
            else if (currentValueDisplayLength > width)
            {
                // the string is longer than the width of the column
                // truncate and add ellipsis if it's too long
                int truncationDisplayLength = width - ellipsis.Length;

                if (truncationDisplayLength > 0)
                {
                    // we have space for the ellipsis, add it
                    switch (alignment)
                    {
                    case TextAlignment.Right:
                    {
                        // get from "abcdef" to "...f"
                        int tailCount = dc.GetTailSplitLength(s, truncationDisplayLength);
                        s = s.Substring(s.Length - tailCount);
                        s = ellipsis + s;
                    }
                    break;

                    case TextAlignment.Center:
                    {
                        // get from "abcdef" to "a..."
                        s  = s.Substring(0, dc.GetHeadSplitLength(s, truncationDisplayLength));
                        s += ellipsis;
                    }
                    break;

                    default:
                    {
                        // left align is the default
                        // get from "abcdef" to "a..."
                        s  = s.Substring(0, dc.GetHeadSplitLength(s, truncationDisplayLength));
                        s += ellipsis;
                    }
                    break;
                    }
                }
                else
                {
                    // not enough space for the ellipsis, just truncate at the width
                    int len = width;

                    switch (alignment)
                    {
                    case TextAlignment.Right:
                    {
                        // get from "abcdef" to "f"
                        int tailCount = dc.GetTailSplitLength(s, len);
                        s = s.Substring(s.Length - tailCount, tailCount);
                    }
                    break;

                    case TextAlignment.Center:
                    {
                        // get from "abcdef" to "a"
                        s = s.Substring(0, dc.GetHeadSplitLength(s, len));
                    }
                    break;

                    default:
                    {
                        // left align is the default
                        // get from "abcdef" to "a"
                        s = s.Substring(0, dc.GetHeadSplitLength(s, len));
                    }
                    break;
                    }
                }
            }

            // we need to take into consideration that truncation left the string one
            // display cell short if a double cell character got truncated
            // in this case, we need to pad with a blank
            int finalValueDisplayLength = dc.Length(s);

            if (finalValueDisplayLength == width)
            {
                return(s);
            }
            // we have to pad
            System.Diagnostics.Debug.Assert(finalValueDisplayLength == width - 1, "padding is not correct");
            switch (alignment)
            {
            case TextAlignment.Right:
            {
                s = " " + s;
            }
            break;

            case TextAlignment.Center:
            {
                s += " ";
            }
            break;

            default:
            {
                // left align is the default
                s += " ";
            }
            break;
            }

            return(s);
        }
Exemplo n.º 2
0
        private static string GenerateRowField(string val, int width, int alignment, DisplayCells dc, bool addPadding)
        {
            // make sure the string does not have any embedded <CR> in it
            string s = StringManipulationHelper.TruncateAtNewLine(val);
            int    currentValueDisplayLength = dc.Length(s);

            if (currentValueDisplayLength < width)
            {
                // the string is shorter than the width of the column
                // need to pad with with blanks to reach the desired width
                int padCount = width - currentValueDisplayLength;
                switch (alignment)
                {
                case TextAlignment.Right:
                {
                    s = StringUtil.Padding(padCount) + s;
                }

                break;

                case TextAlignment.Center:
                {
                    // add a bit at both ends of the string
                    int padLeft  = padCount / 2;
                    int padRight = padCount - padLeft;

                    s = StringUtil.Padding(padLeft) + s;
                    if (addPadding)
                    {
                        s += StringUtil.Padding(padRight);
                    }
                }

                break;

                default:
                {
                    if (addPadding)
                    {
                        // left align is the default
                        s += StringUtil.Padding(padCount);
                    }
                }

                break;
                }
            }
            else if (currentValueDisplayLength > width)
            {
                // the string is longer than the width of the column
                // truncate and add ellipsis if it's too long
                int truncationDisplayLength = width - EllipsisSize;

                if (truncationDisplayLength > 0)
                {
                    // we have space for the ellipsis, add it
                    switch (alignment)
                    {
                    case TextAlignment.Right:
                    {
                        // get from "abcdef" to "...f"
                        s = s.VtSubstring(
                            startOffset: dc.TruncateHead(s, truncationDisplayLength),
                            prependStr: PSObjectHelper.EllipsisStr,
                            appendStr: null);
                    }

                    break;

                    default:
                    {
                        // left align is the default
                        // get from "abcdef" to "a..."
                        s = s.VtSubstring(
                            startOffset: 0,
                            length: dc.TruncateTail(s, truncationDisplayLength),
                            prependStr: null,
                            appendStr: PSObjectHelper.EllipsisStr);
                    }

                    break;
                    }
                }
                else
                {
                    // not enough space for the ellipsis, just truncate at the width
                    switch (alignment)
                    {
                    case TextAlignment.Right:
                    {
                        // get from "abcdef" to "f"
                        s = s.VtSubstring(startOffset: dc.TruncateHead(s, width));
                    }

                    break;

                    default:
                    {
                        // left align is the default
                        // get from "abcdef" to "a"
                        s = s.VtSubstring(startOffset: 0, length: dc.TruncateTail(s, width));
                    }

                    break;
                    }
                }
            }

            // we need to take into consideration that truncation left the string one
            // display cell short if a double cell character got truncated
            // in this case, we need to pad with a blank
            int finalValueDisplayLength = dc.Length(s);

            if (finalValueDisplayLength == width)
            {
                return(s);
            }

            switch (alignment)
            {
            case TextAlignment.Right:
            {
                s = " " + s;
            }

            break;

            case TextAlignment.Center:
            {
                if (addPadding)
                {
                    s += " ";
                }
            }

            break;

            default:
            {
                // left align is the default
                if (addPadding)
                {
                    s += " ";
                }
            }

            break;
            }

            return(s);
        }
Exemplo n.º 3
0
        private static string GenerateRowField(string val, int width, int alignment, DisplayCells dc)
        {
            string str = StringManipulationHelper.TruncateAtNewLine(val);

            if (str == null)
            {
                str = "";
            }
            string str2 = str;
            int    num  = dc.Length(str2);

            if (num < width)
            {
                int count = width - num;
                switch (alignment)
                {
                case 2:
                {
                    int num3 = count / 2;
                    int num4 = count - num3;
                    str = new string(' ', num3) + str + new string(' ', num4);
                    goto Label_0182;
                }

                case 3:
                    str = new string(' ', count) + str;
                    goto Label_0182;
                }
                str = str + new string(' ', count);
            }
            else if (num > width)
            {
                int displayCells = width - "...".Length;
                if (displayCells > 0)
                {
                    switch (alignment)
                    {
                    case 2:
                        str = str.Substring(0, dc.GetHeadSplitLength(str, displayCells)) + "...";
                        goto Label_0182;

                    case 3:
                    {
                        int tailSplitLength = dc.GetTailSplitLength(str, displayCells);
                        str = str.Substring(str.Length - tailSplitLength);
                        str = "..." + str;
                        goto Label_0182;
                    }
                    }
                    str = str.Substring(0, dc.GetHeadSplitLength(str, displayCells)) + "...";
                }
                else
                {
                    int num7 = width;
                    switch (alignment)
                    {
                    case 2:
                        str = str.Substring(0, dc.GetHeadSplitLength(str, num7));
                        goto Label_0182;

                    case 3:
                    {
                        int length = dc.GetTailSplitLength(str, num7);
                        str = str.Substring(str.Length - length, length);
                        goto Label_0182;
                    }
                    }
                    str = str.Substring(0, dc.GetHeadSplitLength(str, num7));
                }
            }
Label_0182:
            if (dc.Length(str) == width)
            {
                return(str);
            }
            switch (alignment)
            {
            case 2:
                return(str + " ");

            case 3:
                return(" " + str);
            }
            return(str + " ");
        }