<commit_before>﻿namespace Vurdalakov.ClipboardDotNet
{
    using System;
    using System.Text;

    public class MainViewModel : ViewModelBase
    {
        public ThreadSafeObservableCollection<EntryViewModel> Entries { get; private set; }

        private EntryViewModel _selectedEntry;
        public EntryViewModel SelectedEntry
        {
            get
            {
                return _selectedEntry;
            }
            set
            {
                if (_selectedEntry != value)
                {
                    _selectedEntry = value;
                    this.OnPropertyChanged(() => SelectedEntry);

                    UpdateHexData();
                }
            }
        }

        public String HexData { get; private set; }

        private ClipboardListener _clipboardListener = new ClipboardListener();

        public MainViewModel()
        {
            Entries = new ThreadSafeObservableCollection<EntryViewModel>();

            _clipboardListener.ClipboardUpdated += (s, e) => Refresh();
        }

        public void OnLoaded()
        {
            _clipboardListener.Start();

            Refresh();
        }

        public void OnClosing()
        {
            _clipboardListener.Stop();
        }

        public void Refresh()
        {
            Entries.Clear();

            var entries = Clipboard.GetEntries();

            foreach (var entry in entries)
            {
                Entries.Add(new EntryViewModel(entry));
            }

            if (Entries.Count > 0)
            {
                SelectedEntry = Entries[0];
            }
        }

        public void UpdateHexData()
        {
            var data = Clipboard.GetData(_selectedEntry.Format);

            var chars = new Char[16];

            var hexData = new StringBuilder();

            var pos = 0;
            for (;;)
            {
                var stringBuilder = new StringBuilder(128);
                stringBuilder.AppendFormat("{0:X08}: ", pos);

                for (var i = 0; i < 16; i++)
                {
                    if (8 == i)
                    {
                        stringBuilder.Append("| ");
                    }

                    if (pos < data.Length)
                    {
                        stringBuilder.AppendFormat("{0:X02} ", data[pos]);
                        chars[i] = data[pos] > 31 ? (char)data[pos] : ' ';
                        pos++;
                    }
                    else
                    {
                        stringBuilder.Append("   ");
                        chars[i] = ' ';
                    }
                }

                stringBuilder.Append(' ');
                stringBuilder.Append(chars);
                stringBuilder.Append(Environment.NewLine);

                hexData.Append(stringBuilder);

                if (pos >= data.Length)
                {
                    break;
                }
            }

            HexData = hexData.ToString();
            this.OnPropertyChanged(() => HexData);
        }
    }
}
<commit_msg>clipper: old format preserved on refresh<commit_after>﻿namespace Vurdalakov.ClipboardDotNet
{
    using System;
    using System.Text;

    public class MainViewModel : ViewModelBase
    {
        public ThreadSafeObservableCollection<EntryViewModel> Entries { get; private set; }

        private EntryViewModel _selectedEntry;
        public EntryViewModel SelectedEntry
        {
            get
            {
                return _selectedEntry;
            }
            set
            {
                if (_selectedEntry != value)
                {
                    _selectedEntry = value;
                    this.OnPropertyChanged(() => SelectedEntry);

                    UpdateHexData();
                }
            }
        }

        public String HexData { get; private set; }

        private ClipboardListener _clipboardListener = new ClipboardListener();

        public MainViewModel()
        {
            Entries = new ThreadSafeObservableCollection<EntryViewModel>();

            _clipboardListener.ClipboardUpdated += (s, e) => Refresh();
        }

        public void OnLoaded()
        {
            _clipboardListener.Start();

            Refresh();
        }

        public void OnClosing()
        {
            _clipboardListener.Stop();
        }

        public void Refresh()
        {
            var selectedFormat = null == SelectedEntry ? 0 : SelectedEntry.Format;

            Entries.Clear();
            SelectedEntry = null;

            var entries = Clipboard.GetEntries();

            var selectedIndex = 0;
            foreach (var entry in entries)
            {
                Entries.Add(new EntryViewModel(entry));

                if (entry.Format == selectedFormat)
                {
                    selectedIndex = Entries.Count - 1;
                }
            }

            if (Entries.Count > selectedIndex)
            {
                SelectedEntry = Entries[selectedIndex];
            }
        }

        public void UpdateHexData()
        {
            var data = Clipboard.GetData(_selectedEntry.Format);

            var chars = new Char[16];

            var hexData = new StringBuilder();

            var pos = 0;
            for (;;)
            {
                var stringBuilder = new StringBuilder(128);
                stringBuilder.AppendFormat("{0:X08}: ", pos);

                for (var i = 0; i < 16; i++)
                {
                    if (8 == i)
                    {
                        stringBuilder.Append("| ");
                    }

                    if (pos < data.Length)
                    {
                        stringBuilder.AppendFormat("{0:X02} ", data[pos]);
                        chars[i] = data[pos] > 31 ? (char)data[pos] : ' ';
                        pos++;
                    }
                    else
                    {
                        stringBuilder.Append("   ");
                        chars[i] = ' ';
                    }
                }

                stringBuilder.Append(' ');
                stringBuilder.Append(chars);
                stringBuilder.Append(Environment.NewLine);

                hexData.Append(stringBuilder);

                if (pos >= data.Length)
                {
                    break;
                }
            }

            HexData = hexData.ToString();
            this.OnPropertyChanged(() => HexData);
        }
    }
}

{'fraction_non_alphanumeric': 0.08988936693300553, 'fraction_numerical': 0.006299938537185003, 'mean_word_length': 1.074251115360102, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, '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': '3282145', 'n_tokens_mistral': 1641, 'n_tokens_neox': 1443, 'n_words': 306}