예제 #1
0
        public static int CellLength(this string text, RenderContext context)
        {
            if (context is null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            return(Cell.GetCellLength(context, text));
        }
예제 #2
0
        public static string Align(RenderContext context, string text, Justify?alignment, int maxWidth)
        {
            if (alignment == null || alignment == Justify.Left)
            {
                return(text);
            }

            var width = Cell.GetCellLength(context, text);

            if (width >= maxWidth)
            {
                return(text);
            }

            switch (alignment)
            {
            case Justify.Right:
            {
                var diff = maxWidth - width;
                return(new string(' ', diff) + text);
            }

            case Justify.Center:
            {
                var diff = (maxWidth - width) / 2;

                var left  = new string(' ', diff);
                var right = new string(' ', diff);

                // Right side
                var remainder = (maxWidth - width) % 2;
                if (remainder != 0)
                {
                    right += new string(' ', remainder);
                }

                return(left + text + right);
            }

            default:
                throw new NotSupportedException("Unknown alignment");
            }
        }
예제 #3
0
 public static int CellLength(this string text, Encoding encoding)
 {
     return(Cell.GetCellLength(encoding, text));
 }
예제 #4
0
 public static int CellLength(this string text, RenderContext context)
 {
     return(Cell.GetCellLength(context, text));
 }