<commit_before>﻿using Hangfire.Common;
using Hangfire.Console.Serialization;
using Hangfire.Server;
using System;

namespace Hangfire.Console
{
    /// <summary>
    /// Provides extension methods for writing to console.
    /// </summary>
    public static class ConsoleExtensions
    {
        /// <summary>
        /// Sets text color for next console lines.
        /// </summary>
        /// <param name="context">Context</param>
        /// <param name="color">Text color to use</param>
        public static void SetTextColor(this PerformContext context, ConsoleTextColor color)
        {
            if (context == null)
                throw new ArgumentNullException(nameof(context));

            if (color == null)
                throw new ArgumentNullException(nameof(color));

            context.Items["ConsoleTextColor"] = color;
        }

        /// <summary>
        /// Resets text color for next console lines.
        /// </summary>
        /// <param name="context">Context</param>
        public static void ResetTextColor(this PerformContext context)
        {
            if (context == null)
                throw new ArgumentNullException(nameof(context));

            context.Items.Remove("ConsoleTextColor");
        }
        
        /// <summary>
        /// Adds a string to console.
        /// </summary>
        /// <param name="context">Context</param>
        /// <param name="value">String</param>
        public static void WriteLine(this PerformContext context, string value)
        {
            if (context == null)
                throw new ArgumentNullException(nameof(context));
            
            if (!context.Items.ContainsKey("ConsoleId"))
            {
                // Absence of ConsoleId means ConsoleServerFilter was not properly added
                return;
            }
            
            var consoleId = (ConsoleId)context.Items["ConsoleId"];

            var line = new ConsoleLine();
            line.TimeOffset = Math.Round((DateTime.UtcNow - consoleId.DateValue).TotalSeconds, 6);
            line.Message = value ?? "";

            if (context.Items.ContainsKey("ConsoleTextColor"))
            {
                line.TextColor = context.Items["ConsoleTextColor"].ToString();
            }

            using (var tran = context.Connection.CreateWriteTransaction())
            {<commit_msg>Fix occasional duplicate lines collapsing<commit_after>﻿using Hangfire.Common;
using Hangfire.Console.Serialization;
using Hangfire.Server;
using System;

namespace Hangfire.Console
{
    /// <summary>
    /// Provides extension methods for writing to console.
    /// </summary>
    public static class ConsoleExtensions
    {
        /// <summary>
        /// Sets text color for next console lines.
        /// </summary>
        /// <param name="context">Context</param>
        /// <param name="color">Text color to use</param>
        public static void SetTextColor(this PerformContext context, ConsoleTextColor color)
        {
            if (context == null)
                throw new ArgumentNullException(nameof(context));

            if (color == null)
                throw new ArgumentNullException(nameof(color));

            context.Items["ConsoleTextColor"] = color;
        }

        /// <summary>
        /// Resets text color for next console lines.
        /// </summary>
        /// <param name="context">Context</param>
        public static void ResetTextColor(this PerformContext context)
        {
            if (context == null)
                throw new ArgumentNullException(nameof(context));

            context.Items.Remove("ConsoleTextColor");
        }
        
        /// <summary>
        /// Adds a string to console.
        /// </summary>
        /// <param name="context">Context</param>
        /// <param name="value">String</param>
        public static void WriteLine(this PerformContext context, string value)
        {
            if (context == null)
                throw new ArgumentNullException(nameof(context));
            
            if (!context.Items.ContainsKey("ConsoleId"))
            {
                // Absence of ConsoleId means ConsoleServerFilter was not properly added
                return;
            }
            
            var consoleId = (ConsoleId)context.Items["ConsoleId"];

            var line = new ConsoleLine();
            line.TimeOffset = Math.Round((DateTime.UtcNow - consoleId.DateValue).TotalSeconds, 3);
            line.Message = value ?? "";

            // prevent duplicate lines collapsing
            if (context.Items.ContainsKey("ConsoleLastOffset"))
            {
                var lastOffset = (double)context.Items["ConsoleLastOffset"];
                if (lastOffset >= line.TimeOffset)
                {
                    line.TimeOffset = lastOffset + 0.0001;
                }
            }
            context.Items["ConsoleLastOffset"] = line.TimeOffset;

            if (context.Items.ContainsKey("ConsoleTextColor"))
            {
                line.TextColor = context.Items["ConsoleTextColor"].ToString();
            }

            using (var tran = context.Connection.CreateWriteTransaction())
            {
{'fraction_non_alphanumeric': 0.11113243761996161, 'fraction_numerical': 0.001343570057581574, 'mean_word_length': 1.7792, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '661505', 'n_tokens_mistral': 1264, 'n_tokens_neox': 1211, 'n_words': 307}