Пример #1
0
 private string FormatValue(object value, FormattingStyle formattingStyle)
 {
     return value == null ? "null"
         : value is string ? string.Format("'{0}'", value)
         : value is DateTime ? ((DateTime)value).ToIso8601String()
         : value is bool ? ((bool)value) ? "true" : "false"
         : (formattingStyle == FormattingStyle.Content && (value is long || value is ulong)) ? value.ToString() + "L"
         : value.ToString();
 }
Пример #2
0
 private string FormatValue(object value, FormattingStyle formattingStyle)
 {
     return(value == null ? "null"
         : value is string?string.Format("'{0}'", value)
                : value is DateTime ? ((DateTime)value).ToIso8601String()
         : value is bool?((bool)value) ? "true" : "false"
                : (formattingStyle == FormattingStyle.Content && (value is long || value is ulong)) ? value.ToString() + "L"
         : value.ToString());
 }
Пример #3
0
 private string FormatValue(object value, FormattingStyle formattingStyle, ExpressionContext context)
 {
     return value == null ? "null"
         : value is FilterExpression ? (value as FilterExpression).Format(context)
         : value is string ? string.Format("'{0}'", value)
         : value is DateTime ? ((DateTime)value).ToIso8601String()
         : value is bool ? value.ToString().ToLower()
         : (value is long || value is ulong) ? value + (formattingStyle == FormattingStyle.Content ? "L" : string.Empty)
         : value is float ? ((float)value).ToString(CultureInfo.InvariantCulture)
         : value is double ? ((double)value).ToString(CultureInfo.InvariantCulture)
         : value is decimal ? ((decimal)value).ToString(CultureInfo.InvariantCulture)
         : value.ToString();
 }
        public void Write(string text, Color?color = null, FormattingStyle style = FormattingStyle.Default)
        {
            if (_useAnsi && color != null)
            {
                text = ESC + $"38;2;{color.Value.R};{color.Value.G};{color.Value.B}m" + text;
            }

            SendString(text);

            if (color != null)
            {
                //reset color
                SendString(ESC + "0m");
            }
        }
Пример #5
0
 private string FormatValue(object value, FormattingStyle formattingStyle, ExpressionContext context)
 {
     return value == null ? "null"
         : value is FilterExpression ? (value as FilterExpression).Format(context)
         : value is string ? string.Format("'{0}'", value)
         : value is DateTime ? ((DateTime)value).ToODataString(formattingStyle)
         : value is DateTimeOffset ? ((DateTimeOffset)value).ToODataString(formattingStyle)
         : value is TimeSpan ? ((TimeSpan)value).ToODataString(formattingStyle)
         : value is Guid ? ((Guid)value).ToODataString(formattingStyle)
         : value is bool ? value.ToString().ToLower()
         : value is long ? ((long)value).ToODataString(formattingStyle)
         : value is ulong ? ((ulong)value).ToODataString(formattingStyle)
         : value is float ? ((float)value).ToString(CultureInfo.InvariantCulture)
         : value is double ? ((double)value).ToString(CultureInfo.InvariantCulture)
         : value is decimal ? ((decimal)value).ToODataString(formattingStyle)
         : value.ToString();
 }
Пример #6
0
        // Checks if the current position is the begining of a format symbol
        // if true - p_currentFormatSymbol will be the discovered format delimiter
        // and p_shift will denote its length
        private static bool IsFormatSymbol(string text, int currentIndex, out FormattingStyle currentFormatStyle, out int symbolLength)
        {
            const int limit     = 2;
            var       length    = currentIndex + 2 > text.Length ? 1 : limit;
            var       substring = text.Substring(currentIndex, length);

            foreach (var formatString in FormattingCharacters.Keys)
            {
                if (substring.StartsWith(formatString))
                {
                    symbolLength       = formatString.Length;
                    currentFormatStyle = FormattingCharacters[formatString];
                    return(true);
                }
            }

            symbolLength       = -1;
            currentFormatStyle = FormattingStyle.Undefined;
            return(false);
        }
        /// <summary>
        /// Configures data provider
        /// </summary>
        /// <param name="settings"></param>
        public override void Config(object settings)
        {
            base.Config(settings);

            rsdnSettings = settings as DataProviderSettings;
            if (rsdnSettings != null)
            {
                encoding = rsdnSettings.GetEncoding;
                if (rsdnSettings.Formatting != FormattingStyle.UserSettings)
                {
                    style = rsdnSettings.Formatting;
                }

                imageProcessor = (style == FormattingStyle.HtmlInlineImages) ?
                                 new ImageProcessor(messageIdPostfix, rsdnSettings.MaxImagesSize, Proxy) : null;
            }

            if (imageProcessor != null)
            {
                formatter = new TextFormatter(imageProcessor.ProcessImagesDelegate);
            }
        }
 public void WriteLine(string text, Color?color = null, FormattingStyle style = FormattingStyle.Default)
 {
     Write(text + "\r\n", color, style);
 }
Пример #9
0
        /// <summary>
        /// Print a string containing formatting-tags like Symfony uses.
        /// Builtin styles are <i>error</i>, <i>info</i> and <i>notice</i>.
        /// Custom styles can be applied with setting <i>fg=...</i> or <i>bg=...</i> respectively to names
        /// of ConsoleColor values. fg and bg are separated with a semicolon (;).
        /// Examples:
        /// <pre>
        /// "Something <error>failed</error>"
        /// "Some <bg=Cyan>cyan text</>"
        /// "<fg=White;bg=Green>White text on green background</>"
        /// </pre>
        /// </summary>
        /// <param name="text"></param>
        /// <param name="indent">Optionally indent the text</param>
        public static void Write(string text, int indent = 0)
        {
            for (int i = 0; i < indent; i++)
            {
                Console.Write(' ');
            }
            if (text.Split("<").Length < 3)
            {
                Console.Write(text);
                return;
            }
            var formattingStack = new Stack <FormattingStyle>();

            formattingStack.Push(new FormattingStyle(Console.ForegroundColor, Console.BackgroundColor));
            var tagStack = new Stack <string>();

            for (int i = 0; i < text.Length; i++)
            {
                var c = text[i];
                if (c == '<')
                {
                    var tagEnd = text.IndexOf('>', i);
                    if (tagEnd < 0)
                    {
                        continue;
                    }
                    var tagContent = text.Substring(i + 1, tagEnd - i - 1);
                    if (tagContent[0] == '/')
                    {
                        if (tagStack.Count == 0)
                        {
                            Console.Write("<");
                            continue;
                        }
                        var closingTag = $"<{tagContent}>";
                        if (closingTag == tagStack.Peek())
                        {
                            tagStack.Pop();
                            formattingStack.Pop();
                            var currentStyle = formattingStack.Peek();
                            Console.ForegroundColor = currentStyle.Foreground;
                            Console.BackgroundColor = currentStyle.Background;
                            i += tagContent.Length + 1;
                            continue;
                        }
                    }
                    if (knownStyles.ContainsKey(tagContent))
                    {
                        var endTag    = $"</{tagContent}>";
                        var hasEndTag = text.IndexOf(endTag, i + 1) > 0;
                        if (hasEndTag)
                        {
                            var nextStyle = knownStyles[tagContent];
                            formattingStack.Push(nextStyle);
                            Console.ForegroundColor = nextStyle.Foreground;
                            Console.BackgroundColor = nextStyle.Background;
                            i += tagContent.Length + 1;
                            tagStack.Push(endTag);
                            continue;
                        }
                    }
                    else if (tagContent.Contains("="))
                    {
                        var endTag    = "</>";
                        var hasEndTag = text.IndexOf(endTag, i + 1) > 0;
                        if (hasEndTag)
                        {
                            var assignments = tagContent.Split(";");
                            var foreground  = formattingStack.Peek().Foreground;
                            var background  = formattingStack.Peek().Background;
                            foreach (var assignment in assignments)
                            {
                                var parts = assignment.Split("=");
                                if (parts[0] == "fg")
                                {
                                    foreground = Util.StringToConsoleColor(parts[1]);
                                }
                                else if (parts[0] == "bg")
                                {
                                    background = Util.StringToConsoleColor(parts[1]);
                                }
                            }
                            var nextStyle = new FormattingStyle(foreground, background);
                            formattingStack.Push(nextStyle);
                            Console.ForegroundColor = nextStyle.Foreground;
                            Console.BackgroundColor = nextStyle.Background;
                            i += tagContent.Length + 1;
                            tagStack.Push(endTag);
                            continue;
                        }
                    }
                }
                Console.Write(c);
            }
            Console.ResetColor();
        }
Пример #10
0
 public CalendarPrinter(int maxWidth, FormattingStyle formattingStyle, Calendar calendar)
 {
     _maxWidth        = maxWidth;
     _formattingStyle = formattingStyle;
     _calendar        = calendar;
 }