예제 #1
0
        internal override int Length(string str, int offset)
        {
            if (string.IsNullOrEmpty(str))
            {
                return(0);
            }

            if (offset < 0 || offset >= str.Length)
            {
                throw PSTraceSource.NewArgumentException(nameof(offset));
            }

            try
            {
                var valueStrDec = new ValueStringDecorated(str);
                if (valueStrDec.IsDecorated)
                {
                    str = valueStrDec.ToString(OutputRendering.PlainText);
                }

                int length = 0;
                for (; offset < str.Length; offset++)
                {
                    length += _rawUserInterface.LengthInBufferCells(str[offset]);
                }

                return(length);
            }
            catch
            {
                // thrown when external host rawui is not implemented, in which case
                // we will fallback to the default value.
                return(base.Length(str, offset));
            }
        }
예제 #2
0
        /// <summary>
        /// Truncate from the tail of the string.
        /// </summary>
        /// <param name="str">String that may contain VT escape sequences.</param>
        /// <param name="offset">
        /// When the string doesn't contain VT sequences, it's the starting index.
        /// When the string contains VT sequences, it means starting from the 'n-th' char that doesn't belong to a escape sequence.</param>
        /// <param name="displayCells">Number of buffer cells to fit in.</param>
        /// <returns>Number of non-escape-sequence characters from head of the string that can fit in the space.</returns>
        internal int TruncateTail(string str, int offset, int displayCells)
        {
            var valueStrDec = new ValueStringDecorated(str);

            if (valueStrDec.IsDecorated)
            {
                str = valueStrDec.ToString(OutputRendering.PlainText);
            }

            return(GetFitLength(str, offset, displayCells, startFromHead: true));
        }
예제 #3
0
        /// <summary>
        /// Truncate from the head of the string.
        /// </summary>
        /// <param name="str">String that may contain VT escape sequences.</param>
        /// <param name="displayCells">Number of buffer cells to fit in.</param>
        /// <returns>Number of non-escape-sequence characters from head of the string that should be skipped.</returns>
        internal int TruncateHead(string str, int displayCells)
        {
            var valueStrDec = new ValueStringDecorated(str);

            if (valueStrDec.IsDecorated)
            {
                str = valueStrDec.ToString(OutputRendering.PlainText);
            }

            int tailCount = GetFitLength(str, offset: 0, displayCells, startFromHead: false);

            return(str.Length - tailCount);
        }
예제 #4
0
        /// <summary>
        /// Calculate the buffer cell length of the given string.
        /// </summary>
        /// <param name="str">String that may contain VT escape sequences.</param>
        /// <param name="offset">
        /// When the string doesn't contain VT sequences, it's the starting index.
        /// When the string contains VT sequences, it means starting from the 'n-th' char that doesn't belong to a escape sequence.</param>
        /// <returns>Number of buffer cells the string needs to take.</returns>
        internal virtual int Length(string str, int offset)
        {
            if (string.IsNullOrEmpty(str))
            {
                return(0);
            }

            var valueStrDec = new ValueStringDecorated(str);

            if (valueStrDec.IsDecorated)
            {
                str = valueStrDec.ToString(OutputRendering.PlainText);
            }

            int length = 0;

            for (; offset < str.Length; offset++)
            {
                length += CharLengthInBufferCells(str[offset]);
            }

            return(length);
        }