示例#1
0
        static ustring ClipAndJustify(ustring str, int width, TextAlignment talign)
        {
            // Get rid of any '\r' added by Windows
            str = str.Replace("\r", ustring.Empty);
            int slen = str.RuneCount;

            if (slen > width)
            {
                var uints = str.ToRunes(width);
                var runes = new Rune [uints.Length];
                for (int i = 0; i < uints.Length; i++)
                {
                    runes [i] = uints [i];
                }
                return(ustring.Make(runes));
            }
            else
            {
                if (talign == TextAlignment.Justified)
                {
                    // TODO: ustring needs this
                    var words     = str.ToString().Split(whitespace, StringSplitOptions.RemoveEmptyEntries);
                    int textCount = words.Sum(arg => arg.Length);

                    var spaces = (width - textCount) / (words.Length - 1);
                    var extras = (width - textCount) % words.Length;

                    var s = new System.Text.StringBuilder();
                    //s.Append ($"tc={textCount} sp={spaces},x={extras} - ");
                    for (int w = 0; w < words.Length; w++)
                    {
                        var x = words [w];
                        s.Append(x);
                        if (w + 1 < words.Length)
                        {
                            for (int i = 0; i < spaces; i++)
                            {
                                s.Append(' ');
                            }
                        }
                        if (extras > 0)
                        {
                            //s.Append ('_');
                            extras--;
                        }
                    }
                    return(ustring.Make(s.ToString()));
                }
                return(str);
            }
        }