Exemplo n.º 1
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);
        }