<commit_before>﻿using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Microsoft.VisualBasic;

namespace VarsViewer
{
	public partial class MainForm : Form
	{	
		float cellWidth, cellHeight;
		
		Var tooltipVar;
		RectangleF toolTipRect;
		string toolTipText;

		readonly Brush greenBrush = new SolidBrush(Color.FromArgb(255, 43, 193, 118));
		readonly Brush grayBrush = new SolidBrush(Color.FromArgb(255, 28, 28, 38));	
		readonly Brush lightGrayBrush = new SolidBrush(Color.FromArgb(255, 47, 47, 57));	
		readonly Brush darkGrayBrush = new SolidBrush(Color.FromArgb(255, 17, 17, 17));	
		readonly Brush whiteBrush = new SolidBrush(Color.FromArgb(255, 255, 255, 255));		
		readonly Brush redBrush = new SolidBrush(Color.FromArgb(255, 240, 68, 77));	
		readonly Brush blueBrush = new SolidBrush(Color.FromArgb(64, 0, 162, 232));
		readonly Brush darkGreenBrush = new SolidBrush(Color.FromArgb(255, 21, 103, 79));
		
		readonly Font font = new Font("Arial", 13.0f);	
		readonly StringFormat format = new StringFormat();	
		
		readonly Worker worker;	
		
		public MainForm()
		{					
			worker = new Worker(() => this.Invoke(new Action(NeedRefresh)));		
			InitializeComponent();			
		}
		
		void NeedRefresh()
		{
			foreach(Var var in worker.Vars.Concat(worker.Cvars))
			{
				if (var.Refresh)
				{
					var.Refresh = false;
					Invalidate(var.Rectangle);
				}
			}
		}

		void MainFormPaint(object sender, PaintEventArgs e)
		{
			cellWidth = ClientSize.Width / 21.0f;
			cellHeight = ClientSize.Height / 16.0f;
			
			DrawHeader(e);
			DrawCells(e);
			
			if (toolTipText != string.Empty)
			{
				format.LineAlignment = StringAlignment.Center;
				format.Alignment = StringAlignment.Center;
				
				e.Graphics.FillRectangle(darkGreenBrush, toolTipRect);
				e.Graphics.DrawString(toolTipText, font, whiteBrush, toolTipRect, format);				
			}
			
			if (worker.Freeze)
			{
				e.Graphics.FillRectangle(blueBrush, e.ClipRectangle);
			}
		}
		
		protected override void OnPaintBackground(PaintEventArgs e)
		{
		}
		
		void MainFormLoad(object sender, EventArgs e)
		{					
			worker.Start();
		}
		
		void MainFormFormClosed(object sender, FormClosedEventArgs e)
		{
			worker.Shutdown();
		}		
		
		protected override void WndProc(ref Message m)
		{
		    if (m.Msg == 0x0112) // WM_SYSCOMMAND
		    {
		        int wParam = (m.WParam.ToInt32() & 0xFFF0);      
		        if (wParam == 0xF030 || wParam == 0xF020 || wParam == 0xF120) //SC_MAXIMIZE / SC_MINIMIZE / SC_RESTORE
		        {
		             Invalidate();
		        }
		    }
		    
		    base.WndProc(ref m);
		}
		
		void MainFormResizeEnd(object sender, EventArgs e)
		{
			Invalidate();
		}
		
		void MainFormKeyDown(object sender, KeyEventArgs e)
		{
			switch(e.KeyCode)
			{
				case Keys.C:
					worker.Compare = !worker.Compare;
					worker.IgnoreDifferences = !worker.Compare;	
					break;
				
				case Keys.F:
					worker.Freeze = !worker.Freeze;
					Invalidate();
					break;

				case Keys.S:
					worker.SaveState();
					break;
			}
		}
					
		RectangleF DrawCell(PaintEventArgs e, int x, int y, string text, Brush back, Brush front, StringAlignment alignment)
		{
			var rect = new RectangleF(x * cellWidth, y * cellHeight, cellWidth, cellHeight);
			if(new RectangleF(e.ClipRectangle.Location, e.ClipRectangle.Size).IntersectsWith(rect))
			{
				format.LineAlignment = StringAlignment.Center;
				format.Alignment = alignment;
			
				e.Graphics.FillRectangle(back, rect);
				e.Graphics.DrawString(text, font, front, rect, format);
			}
			
			return rect;
		}
		
		void DrawHeader(PaintEventArgs e)
		{ 
			DrawCell(e, 0, 0,  string.Empty, greenBrush, grayBrush, StringAlignment.Center);
			DrawCell(e, 0, 12, string.Empty, greenBrush, grayBrush, StringAlignment.Near);	
			
			for(int i = 0 ; i < 20 ; i++)
			{
				DrawCell(e, i + 1, 0, i.ToString(), greenBrush, grayBrush, StringAlignment.Center);				
				DrawCell(e, i + 1, 12, i.ToString(), greenBrush, grayBrush, StringAlignment.Center);
			}
			
			for(int i = 0 ; i < 11 ; i++)
			{
				DrawCell(e, 0, i + 1, (i * 20).ToString(), greenBrush, grayBrush, StringAlignment.Far);
			}
			
			for(int i = 0 ; i < 3 ; i++)
			{
				DrawCell(e, 0, i + 13, (i * 20).ToString(), greenBrush, grayBrush, StringAlignment.Far);
			}
		}
		
		void DrawCells(PaintEventArgs e)
		{							
			for(int j = 0 ; j < 11 ; j++)
			{
				for(int i = 0 ; i < 20 ; i++)
				{
					int index = j * 20 + i;					
					if(index < worker.Vars.Length)
					{
						Var var = worker.Vars[index];
						var.Rectangle = DrawCell(e, i + 1, j + 1, var.Text, GetBackgroundBrush(var), whiteBrush, StringAlignment.Center);
					}
					else
					{
						DrawCell(e, i + 1, j + 1, string.Empty, lightGrayBrush, grayBrush, StringAlignment.Center);
					}
				}
			}	

			for(int j = 0 ; j < 3 ; j++)
			{
				for(int i = 0 ; i < 20 ; i++)
				{
					int index = j * 20 + i;
					if(index < worker.Cvars.Length)
					{
						Var var = worker.Cvars[index];
						var.Rectangle = DrawCell(e, i + 1, j + 13, var.Text, GetBackgroundBrush(var), whiteBrush, StringAlignment.Center);
					}
					else
					{
						DrawCell(e, i + 1, j + 13, string.Empty, lightGrayBrush, grayBrush, StringAlignment.Center);
					}
				}
			}	
		}
		
		Brush GetBackgroundBrush(Var var)
		{
			if(tooltipVar == var)
			{
				return darkGrayBrush;
			}
			if(var.Difference)
			{
				return redBrush;
			}
			<commit_msg>Refactor table<commit_after>﻿using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Microsoft.VisualBasic;

namespace VarsViewer
{
	public partial class MainForm : Form
	{	
		float cellWidth, cellHeight;
		
		Var tooltipVar;
		RectangleF toolTipRect;
		string toolTipText;

		readonly Brush greenBrush = new SolidBrush(Color.FromArgb(255, 43, 193, 118));
		readonly Brush grayBrush = new SolidBrush(Color.FromArgb(255, 28, 28, 38));	
		readonly Brush lightGrayBrush = new SolidBrush(Color.FromArgb(255, 47, 47, 57));	
		readonly Brush darkGrayBrush = new SolidBrush(Color.FromArgb(255, 17, 17, 17));	
		readonly Brush whiteBrush = new SolidBrush(Color.FromArgb(255, 255, 255, 255));		
		readonly Brush redBrush = new SolidBrush(Color.FromArgb(255, 240, 68, 77));	
		readonly Brush blueBrush = new SolidBrush(Color.FromArgb(64, 0, 162, 232));
		readonly Brush darkGreenBrush = new SolidBrush(Color.FromArgb(255, 21, 103, 79));
		
		readonly Font font = new Font("Arial", 13.0f);	
		readonly StringFormat format = new StringFormat();	
		
		readonly Worker worker;	
		
		public MainForm()
		{					
			worker = new Worker(() => this.Invoke(new Action(NeedRefresh)));		
			InitializeComponent();			
		}
		
		void NeedRefresh()
		{
			foreach(Var var in worker.Vars.Concat(worker.Cvars))
			{
				if (var.Refresh)
				{
					var.Refresh = false;
					Invalidate(var.Rectangle);
				}
			}
		}

		void MainFormPaint(object sender, PaintEventArgs e)
		{
			cellWidth = ClientSize.Width / 21.0f;
			cellHeight = ClientSize.Height / 16.0f;
			
			DrawTab(e, worker.Vars, 0);
			DrawTab(e, worker.Cvars, 12);
			
			if (toolTipText != string.Empty)
			{
				format.LineAlignment = StringAlignment.Center;
				format.Alignment = StringAlignment.Center;
				
				e.Graphics.FillRectangle(darkGreenBrush, toolTipRect);
				e.Graphics.DrawString(toolTipText, font, whiteBrush, toolTipRect, format);				
			}
			
			if (worker.Freeze)
			{
				e.Graphics.FillRectangle(blueBrush, e.ClipRectangle);
			}
		}
		
		protected override void OnPaintBackground(PaintEventArgs e)
		{
		}
		
		void MainFormLoad(object sender, EventArgs e)
		{					
			worker.Start();
		}
		
		void MainFormFormClosed(object sender, FormClosedEventArgs e)
		{
			worker.Shutdown();
		}		
		
		protected override void WndProc(ref Message m)
		{
		    if (m.Msg == 0x0112) // WM_SYSCOMMAND
		    {
		        int wParam = (m.WParam.ToInt32() & 0xFFF0);      
		        if (wParam == 0xF030 || wParam == 0xF020 || wParam == 0xF120) //SC_MAXIMIZE / SC_MINIMIZE / SC_RESTORE
		        {
		             Invalidate();
		        }
		    }
		    
		    base.WndProc(ref m);
		}
		
		void MainFormResizeEnd(object sender, EventArgs e)
		{
			Invalidate();
		}
		
		void MainFormKeyDown(object sender, KeyEventArgs e)
		{
			switch(e.KeyCode)
			{
				case Keys.C:
					worker.Compare = !worker.Compare;
					worker.IgnoreDifferences = !worker.Compare;	
					break;
				
				case Keys.F:
					worker.Freeze = !worker.Freeze;
					Invalidate();
					break;

				case Keys.S:
					worker.SaveState();
					break;
			}
		}
					
		RectangleF DrawCell(PaintEventArgs e, int x, int y, string text, Brush back, Brush front, StringAlignment alignment)
		{
			var rect = new RectangleF(x * cellWidth, y * cellHeight, cellWidth, cellHeight);
			if(new RectangleF(e.ClipRectangle.Location, e.ClipRectangle.Size).IntersectsWith(rect))
			{
				format.LineAlignment = StringAlignment.Center;
				format.Alignment = alignment;
			
				e.Graphics.FillRectangle(back, rect);
				e.Graphics.DrawString(text, font, front, rect, format);
			}
			
			return rect;
		}
				
		void DrawTab(PaintEventArgs e, Var[] vars, int position)
		{
			DrawHeader(e, vars, position);
			DrawCells(e, vars, position);
		}
		
		void DrawHeader(PaintEventArgs e, Var[] vars, int position)
		{ 
			DrawCell(e, 0, position,  string.Empty, greenBrush, grayBrush, StringAlignment.Center);
			
			for(int i = 0 ; i < 20 ; i++)
			{
				DrawCell(e, i + 1, position, i.ToString(), greenBrush, grayBrush, StringAlignment.Center);		
			}
			
			int rows = (vars.Length + 19) / 20;
			for(int i = 0 ; i < rows ; i++)
			{
				DrawCell(e, 0, i + 1 + position, (i * 20).ToString(), greenBrush, grayBrush, StringAlignment.Far);
			}
		}

		void DrawCells(PaintEventArgs e, Var[] vars, int position)
		{						
			int rows = (vars.Length + 19) / 20;
			for(int j = 0 ; j < rows ; j++)
			{
				for(int i = 0 ; i < 20 ; i++)
				{
					int index = j * 20 + i;					
					if(index < vars.Length)
					{
						Var var = vars[index];
						var.Rectangle = DrawCell(e, i + 1, j + position + 1, var.Text, GetBackgroundBrush(var), whiteBrush, StringAlignment.Center);
					}
					else
					{
						DrawCell(e, i + 1, j + position + 1, string.Empty, lightGrayBrush, grayBrush, StringAlignment.Center);
					}
				}
			}	
		}
		
		Brush GetBackgroundBrush(Var var)
		{
			if(tooltipVar == var)
			{
				return darkGrayBrush;
			}
			if(var.Difference)
			{
				return redBrush;
			}
			
{'fraction_non_alphanumeric': 0.12154008236758931, 'fraction_numerical': 0.028828656258979023, 'mean_word_length': 2.760172848397551, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1535565', 'n_tokens_mistral': 4799, 'n_tokens_neox': 3481, 'n_words': 833}