示例#1
0
        /// <summary>
        /// Write to the output interface.
        /// </summary>
        private void WriteToScreen()
        {
            int leftIndentation      = _indentationManager.LeftIndentation;
            int rightIndentation     = _indentationManager.RightIndentation;
            int firstLineIndentation = _indentationManager.FirstLineIndentation;

            // VALIDITY CHECKS:

            // check the useful ("active") width
            int usefulWidth = _textColumns - rightIndentation - leftIndentation;

            if (usefulWidth <= 0)
            {
                // fatal error, there is nothing to write to the device
                // just clear the buffer and return
                _stringBuffer = new StringBuilder();
            }

            // check indentation or hanging is not larger than the active width
            int indentationAbsoluteValue = (firstLineIndentation > 0) ? firstLineIndentation : -firstLineIndentation;

            if (indentationAbsoluteValue >= usefulWidth)
            {
                // valu too big, we reset it to zero
                firstLineIndentation = 0;
            }

            // compute the first line indentation or hanging
            int firstLineWidth      = _textColumns - rightIndentation - leftIndentation;
            int followingLinesWidth = firstLineWidth;

            if (firstLineIndentation >= 0)
            {
                // the first line has an indentation
                firstLineWidth -= firstLineIndentation;
            }
            else
            {
                // the first line is hanging
                followingLinesWidth += firstLineIndentation;
            }

            // error checking on invalid values

            // generate the lines using the computed widths
            StringCollection sc = StringManipulationHelper.GenerateLines(_lo.DisplayCells, _stringBuffer.ToString(),
                                                                         firstLineWidth, followingLinesWidth);

            // compute padding
            int firstLinePadding      = leftIndentation;
            int followingLinesPadding = leftIndentation;

            if (firstLineIndentation >= 0)
            {
                // the first line has an indentation
                firstLinePadding += firstLineIndentation;
            }
            else
            {
                // the first line is hanging
                followingLinesPadding -= firstLineIndentation;
            }

            // now write the lines on the screen
            bool firstLine = true;

            foreach (string s in sc)
            {
                if (firstLine)
                {
                    firstLine = false;
                    _lo.WriteLine(StringManipulationHelper.PadLeft(s, firstLinePadding));
                }
                else
                {
                    _lo.WriteLine(StringManipulationHelper.PadLeft(s, followingLinesPadding));
                }
            }

            _stringBuffer = new StringBuilder();
        }
示例#2
0
 /// <summary>
 /// Determine how many rows the prompt should take.
 /// </summary>
 /// <param name="cols">Current number of columns on the screen.</param>
 /// <param name="displayCells">String manipulation helper.</param>
 /// <returns></returns>
 internal int ComputePromptLines(DisplayCells displayCells, int cols)
 {
     // split the prompt string into lines
     _actualPrompt = StringManipulationHelper.GenerateLines(displayCells, _promptString, cols, cols);
     return(_actualPrompt.Count);
 }
示例#3
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);

            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 = 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 - 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);
            }
            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);
        }
示例#4
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 + " ");
        }