Beispiel #1
0
        internal void Initialize(string[] propertyNames, int screenColumnWidth, DisplayCells dc)
        {
            this.columnWidth = screenColumnWidth;
            if ((propertyNames == null) || (propertyNames.Length == 0))
            {
                this.disabled = true;
            }
            else
            {
                this.disabled = false;
                if ((((screenColumnWidth - " : ".Length) - 1) - 1) < 0)
                {
                    this.disabled = true;
                }
                else
                {
                    int num = (screenColumnWidth - " : ".Length) - 1;
                    this.propertyLabelsDisplayLength = 0;
                    int[] numArray = new int[propertyNames.Length];
                    for (int i = 0; i < propertyNames.Length; i++)
                    {
                        numArray[i] = dc.Length(propertyNames[i]);
                        if (numArray[i] > this.propertyLabelsDisplayLength)
                        {
                            this.propertyLabelsDisplayLength = numArray[i];
                        }
                    }
                    if (this.propertyLabelsDisplayLength > num)
                    {
                        this.propertyLabelsDisplayLength = num;
                    }
                    this.propertyLabels = new string[propertyNames.Length];
                    for (int j = 0; j < propertyNames.Length; j++)
                    {
                        string[] strArray = null;
                        IntPtr ptr;
                        if (numArray[j] < this.propertyLabelsDisplayLength)
                        {
                            this.propertyLabels[j] = propertyNames[j] + new string(' ', this.propertyLabelsDisplayLength - numArray[j]);
                        }
                        else if (numArray[j] > this.propertyLabelsDisplayLength)
                        {
                            this.propertyLabels[j] = propertyNames[j].Substring(0, dc.GetHeadSplitLength(propertyNames[j], this.propertyLabelsDisplayLength));
                        }
                        else
                        {
                            this.propertyLabels[j] = propertyNames[j];
                        }
						strArray = this.propertyLabels;
						ptr = (IntPtr) j;
						strArray[(int) (ptr)] = strArray[(int) ptr] + " : ";
                    }
                    this.propertyLabelsDisplayLength += " : ".Length;
                }
            }
        }
 private static StringCollection GenerateLinesWithoutWordWrap(DisplayCells displayCells, string val, int firstLineLen, int followingLinesLen)
 {
     StringCollection retVal = new StringCollection();
     if (string.IsNullOrEmpty(val))
     {
         retVal.Add(val);
         return retVal;
     }
     string[] strArray = SplitLines(val);
     for (int i = 0; i < strArray.Length; i++)
     {
         if ((strArray[i] == null) || (displayCells.Length(strArray[i]) <= firstLineLen))
         {
             retVal.Add(strArray[i]);
             continue;
         }
         SplitLinesAccumulator accumulator = new SplitLinesAccumulator(retVal, firstLineLen, followingLinesLen);
         int offset = 0;
         while (true)
         {
             int activeLen = accumulator.ActiveLen;
             int num5 = displayCells.Length(strArray[i], offset) - activeLen;
             if (num5 <= 0)
             {
                 break;
             }
             int length = displayCells.GetHeadSplitLength(strArray[i], offset, activeLen);
             if (length <= 0)
             {
                 length = 1;
                 accumulator.AddLine("?");
             }
             else
             {
                 accumulator.AddLine(strArray[i].Substring(offset, length));
             }
             offset += length;
         }
         accumulator.AddLine(strArray[i].Substring(offset));
     }
     return retVal;
 }
Beispiel #3
0
 private StringCollection GenerateMultiLineRowField(string val, int k, int aligment, DisplayCells dc)
 {
     StringCollection strings = StringManipulationHelper.GenerateLines(dc, val, this.si.columnInfo[k].width, this.si.columnInfo[k].width);
     for (int i = 0; i < strings.Count; i++)
     {
         if (dc.Length(strings[i]) < this.si.columnInfo[k].width)
         {
             strings[i] = GenerateRowField(strings[i], this.si.columnInfo[k].width, aligment, dc);
         }
     }
     return strings;
 }
        private StringCollection GenerateMultiLineRowField(string val, int k, int aligment, DisplayCells dc)
        {
            StringCollection sc = StringManipulationHelper.GenerateLines(dc, val,
                                                                         _si.columnInfo[k].width, _si.columnInfo[k].width);

            // if length is shorter, do some padding
            for (int col = 0; col < sc.Count; col++)
            {
                if (dc.Length(sc[col]) < _si.columnInfo[k].width)
                {
                    sc[col] = GenerateRowField(sc[col], _si.columnInfo[k].width, aligment, dc);
                }
            }
            return(sc);
        }
Beispiel #5
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);
        }
Beispiel #6
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;
        }
Beispiel #7
0
 private StringCollection GenerateMultiLineRowField(string val, int k, int alignment, DisplayCells dc)
 {
     StringCollection sc = StringManipulationHelper.GenerateLines(dc, val,
                                 _si.columnInfo[k].width, _si.columnInfo[k].width);
     // if length is shorter, do some padding
     for (int col = 0; col < sc.Count; col++)
     {
         if (dc.Length(sc[col]) < _si.columnInfo[k].width)
             sc[col] = GenerateRowField(sc[col], _si.columnInfo[k].width, alignment, dc);
     }
     return sc;
 }
 private static StringCollection GenerateLinesWithWordWrap(DisplayCells displayCells, string val, int firstLineLen, int followingLinesLen)
 {
     StringCollection strings = new StringCollection();
     if (string.IsNullOrEmpty(val))
     {
         strings.Add(val);
         return strings;
     }
     string[] strArray = SplitLines(val);
     for (int i = 0; i < strArray.Length; i++)
     {
         if ((strArray[i] == null) || (displayCells.Length(strArray[i]) <= firstLineLen))
         {
             strings.Add(strArray[i]);
             continue;
         }
         int num2 = firstLineLen;
         int num3 = firstLineLen;
         bool flag = true;
         StringBuilder builder = new StringBuilder();
         foreach (GetWordsResult result in GetWords(strArray[i]))
         {
             string word = result.Word;
             if (result.Delim == SoftHyphen.ToString())
             {
                 int num4 = displayCells.Length(word) + displayCells.Length(SoftHyphen.ToString());
                 if (num4 == num2)
                 {
                     word = word + "-";
                 }
             }
             else if (!string.IsNullOrEmpty(result.Delim))
             {
                 word = word + result.Delim;
             }
             int num5 = displayCells.Length(word);
             if (num3 == 0)
             {
                 if (flag)
                 {
                     flag = false;
                     num3 = followingLinesLen;
                 }
                 if (num3 == 0)
                 {
                     break;
                 }
                 num2 = num3;
             }
             if (num5 > num3)
             {
                 foreach (char ch in word)
                 {
                     char ch2 = ch;
                     int num6 = displayCells.Length(ch);
                     if (num6 > num3)
                     {
                         ch2 = '?';
                         num6 = 1;
                     }
                     if (num6 > num2)
                     {
                         strings.Add(builder.ToString());
                         builder.Clear();
                         builder.Append(ch2);
                         if (flag)
                         {
                             flag = false;
                             num3 = followingLinesLen;
                         }
                         num2 = num3 - num6;
                     }
                     else
                     {
                         builder.Append(ch2);
                         num2 -= num6;
                     }
                 }
             }
             else if (num5 > num2)
             {
                 strings.Add(builder.ToString());
                 builder.Clear();
                 builder.Append(word);
                 if (flag)
                 {
                     flag = false;
                     num3 = followingLinesLen;
                 }
                 num2 = num3 - num5;
             }
             else
             {
                 builder.Append(word);
                 num2 -= num5;
             }
         }
         strings.Add(builder.ToString());
     }
     return strings;
 }
Beispiel #9
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 + " ");
        }
Beispiel #10
0
        private static StringCollection GenerateLinesWithWordWrap(DisplayCells displayCells, string val, int firstLineLen, int followingLinesLen)
        {
            StringCollection retVal = new StringCollection();

            if (string.IsNullOrEmpty(val))
            {
                // if null or empty, just add and we are done
                retVal.Add(val);
                return(retVal);
            }

            // break string on newlines and process each line separately
            string[] lines = SplitLines(val);

            for (int k = 0; k < lines.Length; k++)
            {
                if (lines[k] == null || displayCells.Length(lines[k]) <= firstLineLen)
                {
                    // we do not need to split further, just add
                    retVal.Add(lines[k]);
                    continue;
                }

                int           spacesLeft = firstLineLen;
                int           lineWidth  = firstLineLen;
                bool          firstLine  = true;
                StringBuilder singleLine = new StringBuilder();

                foreach (GetWordsResult word in GetWords(lines[k]))
                {
                    string wordToAdd = word.Word;

                    // Handle soft hyphen
                    if (word.Delim == s_softHyphen.ToString())
                    {
                        int wordWidthWithHyphen = displayCells.Length(wordToAdd) + displayCells.Length(s_softHyphen);

                        // Add hyphen only if necessary
                        if (wordWidthWithHyphen == spacesLeft)
                        {
                            wordToAdd += "-";
                        }
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(word.Delim))
                        {
                            wordToAdd += word.Delim;
                        }
                    }

                    int wordWidth = displayCells.Length(wordToAdd);

                    // Handle zero width
                    if (lineWidth == 0)
                    {
                        if (firstLine)
                        {
                            firstLine = false;
                            lineWidth = followingLinesLen;
                        }

                        if (lineWidth == 0)
                        {
                            break;
                        }

                        spacesLeft = lineWidth;
                    }

                    // Word is wider than a single line
                    if (wordWidth > lineWidth)
                    {
                        foreach (char c in wordToAdd)
                        {
                            char charToAdd = c;
                            int  charWidth = displayCells.Length(c);

                            // corner case: we have a two cell character and the current
                            // display length is one.
                            // add a single cell arbitrary character instead of the original
                            // one and keep going
                            if (charWidth > lineWidth)
                            {
                                charToAdd = '?';
                                charWidth = 1;
                            }

                            if (charWidth > spacesLeft)
                            {
                                retVal.Add(singleLine.ToString());
                                singleLine.Clear();
                                singleLine.Append(charToAdd);

                                if (firstLine)
                                {
                                    firstLine = false;
                                    lineWidth = followingLinesLen;
                                }

                                spacesLeft = lineWidth - charWidth;
                            }
                            else
                            {
                                singleLine.Append(charToAdd);
                                spacesLeft -= charWidth;
                            }
                        }
                    }
                    else
                    {
                        if (wordWidth > spacesLeft)
                        {
                            retVal.Add(singleLine.ToString());
                            singleLine.Clear();
                            singleLine.Append(wordToAdd);

                            if (firstLine)
                            {
                                firstLine = false;
                                lineWidth = followingLinesLen;
                            }

                            spacesLeft = lineWidth - wordWidth;
                        }
                        else
                        {
                            singleLine.Append(wordToAdd);
                            spacesLeft -= wordWidth;
                        }
                    }
                }

                retVal.Add(singleLine.ToString());
            }

            return(retVal);
        }
Beispiel #11
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 + " ");
        }
Beispiel #12
0
        private static StringCollection GenerateLinesWithWordWrap(DisplayCells displayCells, string val, int firstLineLen, int followingLinesLen)
        {
            StringCollection retVal = new StringCollection();

            if (string.IsNullOrEmpty(val))
            {
                // if null or empty, just add and we are done
                retVal.Add(val);
                return retVal;
            }

            // break string on newlines and process each line separately
            string[] lines = SplitLines(val);

            for (int k = 0; k < lines.Length; k++)
            {
                if (lines[k] == null || displayCells.Length(lines[k]) <= firstLineLen)
                {
                    // we do not need to split further, just add
                    retVal.Add(lines[k]);
                    continue;
                }

                int spacesLeft = firstLineLen;
                int lineWidth = firstLineLen;
                bool firstLine = true;
                StringBuilder singleLine = new StringBuilder();

                foreach (GetWordsResult word in GetWords(lines[k]))
                {
                    string wordToAdd = word.Word;

                    // Handle soft hyphen
                    if (word.Delim == s_softHyphen.ToString())
                    {
                        int wordWidthWithHyphen = displayCells.Length(wordToAdd) + displayCells.Length(s_softHyphen.ToString());

                        // Add hyphen only if necessary
                        if (wordWidthWithHyphen == spacesLeft)
                        {
                            wordToAdd += "-";
                        }
                    }
                    else
                    {
                        if (!String.IsNullOrEmpty(word.Delim))
                        {
                            wordToAdd += word.Delim;
                        }
                    }

                    int wordWidth = displayCells.Length(wordToAdd);

                    // Handle zero width
                    if (lineWidth == 0)
                    {
                        if (firstLine)
                        {
                            firstLine = false;
                            lineWidth = followingLinesLen;
                        }

                        if (lineWidth == 0)
                        {
                            break;
                        }

                        spacesLeft = lineWidth;
                    }

                    // Word is wider than a single line
                    if (wordWidth > lineWidth)
                    {
                        foreach (char c in wordToAdd)
                        {
                            char charToAdd = c;
                            int charWidth = displayCells.Length(c);

                            // corner case: we have a two cell character and the current
                            // display length is one.
                            // add a single cell arbitrary character instead of the original
                            // one and keep going
                            if (charWidth > lineWidth)
                            {
                                charToAdd = '?';
                                charWidth = 1;
                            }

                            if (charWidth > spacesLeft)
                            {
                                retVal.Add(singleLine.ToString());
                                singleLine.Clear();
                                singleLine.Append(charToAdd);

                                if (firstLine)
                                {
                                    firstLine = false;
                                    lineWidth = followingLinesLen;
                                }

                                spacesLeft = lineWidth - charWidth;
                            }
                            else
                            {
                                singleLine.Append(charToAdd);
                                spacesLeft -= charWidth;
                            }
                        }
                    }
                    else
                    {
                        if (wordWidth > spacesLeft)
                        {
                            retVal.Add(singleLine.ToString());
                            singleLine.Clear();
                            singleLine.Append(wordToAdd);

                            if (firstLine)
                            {
                                firstLine = false;
                                lineWidth = followingLinesLen;
                            }

                            spacesLeft = lineWidth - wordWidth;
                        }
                        else
                        {
                            singleLine.Append(wordToAdd);
                            spacesLeft -= wordWidth;
                        }
                    }
                }

                retVal.Add(singleLine.ToString());
            }

            return retVal;
        }
Beispiel #13
0
        private static StringCollection GenerateLinesWithoutWordWrap(DisplayCells displayCells, string val, int firstLineLen, int followingLinesLen)
        {
            StringCollection retVal = new StringCollection();

            if (string.IsNullOrEmpty(val))
            {
                // if null or empty, just add and we are done
                retVal.Add(val);
                return retVal;
            }

            // break string on newlines and process each line separately
            string[] lines = SplitLines(val);

            for (int k = 0; k < lines.Length; k++)
            {
                if (lines[k] == null || displayCells.Length(lines[k]) <= firstLineLen)
                {
                    // we do not need to split further, just add
                    retVal.Add(lines[k]);
                    continue;
                }

                // the string does not fit, so we have to wrap around on multiple lines
                // for each of these lines in the string, the first line will have
                // a (potentially) different length (indentation or hanging)

                // for each line, start a new state
                SplitLinesAccumulator accumulator = new SplitLinesAccumulator(retVal, firstLineLen, followingLinesLen);

                int offset = 0; // offset into the line we are splitting

                while (true)
                {
                    // acquire the current active display line length (it can very from call to call)
                    int currentDisplayLen = accumulator.ActiveLen;

                    // determine if the current tail would fit or not

                    // for the remaining part of the string, determine its display cell count
                    int currentCellsToFit = displayCells.Length(lines[k], offset);

                    // determine if we fit into the line
                    int excessCells = currentCellsToFit - currentDisplayLen;

                    if (excessCells > 0)
                    {
                        // we are not at the end of the string, select a sub string
                        // that would fit in the remaining display length
                        int charactersToAdd = displayCells.GetHeadSplitLength(lines[k], offset, currentDisplayLen);

                        if (charactersToAdd <= 0)
                        {
                            // corner case: we have a two cell character and the current
                            // display length is one.
                            // add a single cell arbitrary character instead of the original
                            // one and keep going
                            charactersToAdd = 1;
                            accumulator.AddLine("?");
                        }
                        else
                        {
                            // of the given length, add it to the accumulator
                            accumulator.AddLine(lines[k].Substring(offset, charactersToAdd));
                        }

                        // increase the offset by the # of characters added
                        offset += charactersToAdd;
                    }
                    else
                    {
                        // we reached the last (partial) line, we add it all
                        accumulator.AddLine(lines[k].Substring(offset));
                        break;
                    }
                }
            }

            return retVal;
        }
Beispiel #14
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="propertyNames">names of the properties to display</param>
        /// <param name="screenColumnWidth">column width of the screen</param>
        /// <param name="dc">instance of the DisplayCells helper object</param>
        internal void Initialize(string[] propertyNames, int screenColumnWidth, DisplayCells dc)
        {
            _columnWidth = screenColumnWidth;
            if (propertyNames == null || propertyNames.Length == 0)
            {
                // there is nothing to show
                _disabled = true;
                return;
            }

            _disabled = false;

            Debug.Assert(propertyNames != null, "propertyNames is null");
            Debug.Assert(propertyNames.Length > 0, "propertyNames has zero length");

            // assess the useful widths
            if ((screenColumnWidth - Separator.Length - MinFieldWidth - MinLabelWidth) < 0)
            {
                // we do not have enough space for any meaningful display
                _disabled = true;
                return;
            }

            // check if we have to truncate the labels
            int maxAllowableLabelLength = screenColumnWidth - Separator.Length - MinFieldWidth;

            // find out the max display length (cell count) of the property names
            _propertyLabelsDisplayLength = 0; // reset max

            // cache the cell lengths for each property
            int[] propertyNameCellCounts = new int[propertyNames.Length];
            for (int k = 0; k < propertyNames.Length; k++)
            {
                Debug.Assert(propertyNames[k] != null, "propertyNames[k] is null");
                propertyNameCellCounts[k] = dc.Length(propertyNames[k]);
                if (propertyNameCellCounts[k] > _propertyLabelsDisplayLength)
                    _propertyLabelsDisplayLength = propertyNameCellCounts[k];
            }

            if (_propertyLabelsDisplayLength > maxAllowableLabelLength)
            {
                // need to truncate
                _propertyLabelsDisplayLength = maxAllowableLabelLength;
            }

            _propertyLabels = new string[propertyNames.Length];

            for (int k = 0; k < propertyNames.Length; k++)
            {
                if (propertyNameCellCounts[k] < _propertyLabelsDisplayLength)
                {
                    // shorter than the max, add padding
                    _propertyLabels[k] = propertyNames[k] + new string(' ', _propertyLabelsDisplayLength - propertyNameCellCounts[k]);
                }
                else if (propertyNameCellCounts[k] > _propertyLabelsDisplayLength)
                {
                    // longer than the max, clip
                    _propertyLabels[k] = propertyNames[k].Substring(0, dc.GetHeadSplitLength(propertyNames[k], _propertyLabelsDisplayLength));
                }
                else
                {
                    _propertyLabels[k] = propertyNames[k];
                }

                _propertyLabels[k] += Separator;
            }
            _propertyLabelsDisplayLength += Separator.Length;
        }
Beispiel #15
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);
        }
Beispiel #16
0
        private static StringCollection GenerateLinesWithoutWordWrap(DisplayCells displayCells, string val, int firstLineLen, int followingLinesLen)
        {
            StringCollection retVal = new StringCollection();

            if (string.IsNullOrEmpty(val))
            {
                // if null or empty, just add and we are done
                retVal.Add(val);
                return(retVal);
            }

            // break string on newlines and process each line separately
            string[] lines = SplitLines(val);

            for (int k = 0; k < lines.Length; k++)
            {
                string currentLine = lines[k];

                if (currentLine == null || displayCells.Length(currentLine) <= firstLineLen)
                {
                    // we do not need to split further, just add
                    retVal.Add(currentLine);
                    continue;
                }

                // the string does not fit, so we have to wrap around on multiple lines
                // for each of these lines in the string, the first line will have
                // a (potentially) different length (indentation or hanging)

                // for each line, start a new state
                SplitLinesAccumulator accumulator = new SplitLinesAccumulator(retVal, firstLineLen, followingLinesLen);

                int offset = 0; // offset into the line we are splitting

                while (offset < currentLine.Length)
                {
                    // acquire the current active display line length (it can very from call to call)
                    int currentDisplayLen = accumulator.ActiveLen;

                    // determine if the current tail would fit or not

                    // for the remaining part of the string, determine its display cell count
                    int currentCellsToFit = displayCells.Length(currentLine, offset);

                    // determine if we fit into the line
                    int excessCells = currentCellsToFit - currentDisplayLen;

                    if (excessCells > 0)
                    {
                        // we are not at the end of the string, select a sub string
                        // that would fit in the remaining display length
                        int charactersToAdd = displayCells.TruncateTail(currentLine, offset, currentDisplayLen);

                        if (charactersToAdd <= 0)
                        {
                            // corner case: we have a two cell character and the current
                            // display length is one.
                            // add a single cell arbitrary character instead of the original
                            // one and keep going
                            charactersToAdd = 1;
                            accumulator.AddLine("?");
                        }
                        else
                        {
                            // of the given length, add it to the accumulator
                            accumulator.AddLine(currentLine.VtSubstring(offset, charactersToAdd));
                        }

                        // increase the offset by the # of characters added
                        offset += charactersToAdd;
                    }
                    else
                    {
                        // we reached the last (partial) line, we add it all
                        accumulator.AddLine(currentLine.VtSubstring(offset));
                        break;
                    }
                }
            }

            return(retVal);
        }
Beispiel #17
0
        private static StringCollection GenerateLinesWithWordWrap(DisplayCells displayCells, string val, int firstLineLen, int followingLinesLen)
        {
            StringCollection retVal = new StringCollection();

            if (string.IsNullOrEmpty(val))
            {
                // if null or empty, just add and we are done
                retVal.Add(val);
                return(retVal);
            }

            // break string on newlines and process each line separately
            List <string> lines = SplitLines(val);

            for (int k = 0; k < lines.Count; k++)
            {
                if (lines[k] == null || displayCells.Length(lines[k]) <= firstLineLen)
                {
                    // we do not need to split further, just add
                    retVal.Add(lines[k]);
                    continue;
                }

                int           spacesLeft = firstLineLen;
                int           lineWidth  = firstLineLen;
                bool          firstLine  = true;
                StringBuilder singleLine = new StringBuilder();
                string        resetStr   = PSStyle.Instance.Reset;

                foreach (GetWordsResult word in GetWords(lines[k]))
                {
                    string wordToAdd = word.Word;
                    string suffix    = null;

                    // Handle soft hyphen
                    if (word.Delim.Length == 1 && word.Delim[0] == s_softHyphen)
                    {
                        int wordWidthWithHyphen = displayCells.Length(wordToAdd) + displayCells.Length(s_softHyphen);

                        // Add hyphen only if necessary
                        if (wordWidthWithHyphen == spacesLeft)
                        {
                            suffix = "-";
                        }
                    }
                    else if (!string.IsNullOrEmpty(word.Delim))
                    {
                        suffix = word.Delim;
                    }

                    if (suffix is not null)
                    {
                        wordToAdd = wordToAdd.EndsWith(resetStr)
                            ? wordToAdd.Insert(wordToAdd.Length - resetStr.Length, suffix)
                            : wordToAdd + suffix;
                    }

                    int wordWidth = displayCells.Length(wordToAdd);

                    // Handle zero width
                    if (lineWidth == 0)
                    {
                        if (firstLine)
                        {
                            firstLine = false;
                            lineWidth = followingLinesLen;
                        }

                        if (lineWidth == 0)
                        {
                            break;
                        }

                        spacesLeft = lineWidth;
                    }

                    // Word is wider than a single line
                    if (wordWidth > lineWidth)
                    {
                        Dictionary <int, int> vtRanges = null;
                        StringBuilder         vtSeqs   = null;

                        var valueStrDec = new ValueStringDecorated(wordToAdd);
                        if (valueStrDec.IsDecorated)
                        {
                            vtSeqs   = new StringBuilder();
                            vtRanges = valueStrDec.EscapeSequenceRanges;
                        }

                        bool hasEscSeqs = false;
                        for (int i = 0; i < wordToAdd.Length; i++)
                        {
                            if (vtRanges?.TryGetValue(i, out int len) == true)
                            {
                                var vtSpan = wordToAdd.AsSpan(i, len);
                                singleLine.Append(vtSpan);
                                vtSeqs.Append(vtSpan);

                                hasEscSeqs = true;
                                i         += len - 1;
                                continue;
                            }

                            char charToAdd = wordToAdd[i];
                            int  charWidth = displayCells.Length(charToAdd);

                            // Corner case: we have a two cell character and the current display length is one.
                            // Add a single cell arbitrary character instead of the original one and keep going.
                            if (charWidth > lineWidth)
                            {
                                charToAdd = '?';
                                charWidth = 1;
                            }

                            if (charWidth > spacesLeft)
                            {
                                if (hasEscSeqs && !singleLine.EndsWith(resetStr))
                                {
                                    singleLine.Append(resetStr);
                                }

                                retVal.Add(singleLine.ToString());
                                singleLine.Clear().Append(vtSeqs).Append(charToAdd);

                                if (firstLine)
                                {
                                    firstLine = false;
                                    lineWidth = followingLinesLen;
                                }

                                spacesLeft = lineWidth - charWidth;
                            }
                            else
                            {
                                singleLine.Append(charToAdd);
                                spacesLeft -= charWidth;
                            }
                        }
                    }
                    else
                    {
                        if (wordWidth > spacesLeft)
                        {
                            retVal.Add(singleLine.ToString());
                            singleLine.Clear().Append(wordToAdd);

                            if (firstLine)
                            {
                                firstLine = false;
                                lineWidth = followingLinesLen;
                            }

                            spacesLeft = lineWidth - wordWidth;
                        }
                        else
                        {
                            singleLine.Append(wordToAdd);
                            spacesLeft -= wordWidth;
                        }
                    }
                }

                retVal.Add(singleLine.ToString());
            }

            return(retVal);
        }
Beispiel #18
0
        /// <summary>
        /// </summary>
        /// <param name="propertyNames">names of the properties to display.</param>
        /// <param name="screenColumnWidth">column width of the screen.</param>
        /// <param name="dc">instance of the DisplayCells helper object.</param>
        internal void Initialize(string[] propertyNames, int screenColumnWidth, DisplayCells dc)
        {
            _columnWidth = screenColumnWidth;
            if (propertyNames == null || propertyNames.Length == 0)
            {
                // there is nothing to show
                _disabled = true;
                return;
            }

            _disabled = false;

            Debug.Assert(propertyNames != null, "propertyNames is null");
            Debug.Assert(propertyNames.Length > 0, "propertyNames has zero length");

            // assess the useful widths
            if ((screenColumnWidth - Separator.Length - MinFieldWidth - MinLabelWidth) < 0)
            {
                // we do not have enough space for any meaningful display
                _disabled = true;
                return;
            }

            // check if we have to truncate the labels
            int maxAllowableLabelLength = screenColumnWidth - Separator.Length - MinFieldWidth;

            // find out the max display length (cell count) of the property names
            _propertyLabelsDisplayLength = 0; // reset max

            // cache the cell lengths for each property
            Span <int> propertyNameCellCounts = propertyNames.Length <= OutCommandInner.StackAllocThreshold ? stackalloc int[propertyNames.Length] : new int[propertyNames.Length];;

            for (int k = 0; k < propertyNames.Length; k++)
            {
                Debug.Assert(propertyNames[k] != null, "propertyNames[k] is null");
                propertyNameCellCounts[k] = dc.Length(propertyNames[k]);
                if (propertyNameCellCounts[k] > _propertyLabelsDisplayLength)
                {
                    _propertyLabelsDisplayLength = propertyNameCellCounts[k];
                }
            }

            if (_propertyLabelsDisplayLength > maxAllowableLabelLength)
            {
                // need to truncate
                _propertyLabelsDisplayLength = maxAllowableLabelLength;
            }

            _propertyLabels = new string[propertyNames.Length];

            for (int k = 0; k < propertyNames.Length; k++)
            {
                if (propertyNameCellCounts[k] < _propertyLabelsDisplayLength)
                {
                    // shorter than the max, add padding
                    _propertyLabels[k] = propertyNames[k] + StringUtil.Padding(_propertyLabelsDisplayLength - propertyNameCellCounts[k]);
                }
                else if (propertyNameCellCounts[k] > _propertyLabelsDisplayLength)
                {
                    // longer than the max, clip
                    _propertyLabels[k] = propertyNames[k].Substring(0, dc.GetHeadSplitLength(propertyNames[k], _propertyLabelsDisplayLength));
                }
                else
                {
                    _propertyLabels[k] = propertyNames[k];
                }

                _propertyLabels[k] += Separator;
            }

            _propertyLabelsDisplayLength += Separator.Length;
        }
Beispiel #19
0
        private static StringCollection GenerateLinesWithWordWrap(DisplayCells displayCells, string val, int firstLineLen, int followingLinesLen)
        {
            StringCollection strings = new StringCollection();

            if (string.IsNullOrEmpty(val))
            {
                strings.Add(val);
                return(strings);
            }
            string[] strArray = SplitLines(val);
            for (int i = 0; i < strArray.Length; i++)
            {
                if ((strArray[i] == null) || (displayCells.Length(strArray[i]) <= firstLineLen))
                {
                    strings.Add(strArray[i]);
                    continue;
                }
                int           num2    = firstLineLen;
                int           num3    = firstLineLen;
                bool          flag    = true;
                StringBuilder builder = new StringBuilder();
                foreach (GetWordsResult result in GetWords(strArray[i]))
                {
                    string word = result.Word;
                    if (result.Delim == SoftHyphen.ToString())
                    {
                        int num4 = displayCells.Length(word) + displayCells.Length(SoftHyphen.ToString());
                        if (num4 == num2)
                        {
                            word = word + "-";
                        }
                    }
                    else if (!string.IsNullOrEmpty(result.Delim))
                    {
                        word = word + result.Delim;
                    }
                    int num5 = displayCells.Length(word);
                    if (num3 == 0)
                    {
                        if (flag)
                        {
                            flag = false;
                            num3 = followingLinesLen;
                        }
                        if (num3 == 0)
                        {
                            break;
                        }
                        num2 = num3;
                    }
                    if (num5 > num3)
                    {
                        foreach (char ch in word)
                        {
                            char ch2  = ch;
                            int  num6 = displayCells.Length(ch);
                            if (num6 > num3)
                            {
                                ch2  = '?';
                                num6 = 1;
                            }
                            if (num6 > num2)
                            {
                                strings.Add(builder.ToString());
                                builder.Clear();
                                builder.Append(ch2);
                                if (flag)
                                {
                                    flag = false;
                                    num3 = followingLinesLen;
                                }
                                num2 = num3 - num6;
                            }
                            else
                            {
                                builder.Append(ch2);
                                num2 -= num6;
                            }
                        }
                    }
                    else if (num5 > num2)
                    {
                        strings.Add(builder.ToString());
                        builder.Clear();
                        builder.Append(word);
                        if (flag)
                        {
                            flag = false;
                            num3 = followingLinesLen;
                        }
                        num2 = num3 - num5;
                    }
                    else
                    {
                        builder.Append(word);
                        num2 -= num5;
                    }
                }
                strings.Add(builder.ToString());
            }
            return(strings);
        }