<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Khondar.UNIXUtils.Concat
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var options = new Options();
            if (CommandLine.Parser.Default.ParseArguments(args, options))
            {
                if (options.Version)
                {
                    var assemblyInfo = typeof(Program).Assembly.GetName();
                    Console.WriteLine(assemblyInfo.Name + " v" + assemblyInfo.Version);
                }

                if (options.NumberNonEmptyLines)
                {
                    options.NumberAll = true;
                }

                if (options.ShowAll)
                {
                    options.ShowNonPrintingWithEnds = true;
                    options.ShowNonPrintingWithTabs = true;
                }

                if (options.ShowNonPrintingWithEnds)
                {
                    options.ShowNonPrinting = true;
                    options.ShowEnds = true;
                }

                if (options.ShowNonPrintingWithTabs)
                {
                    options.ShowNonPrinting = true;
                    options.ShowTabs = true;
                }

                foreach (var fileName in options.FileNames)
                {
                    DisplayFile(fileName, options);
                }
            }
        }

        private static void DisplayFile(string fileName, Options opts)
        {
            FileInfo file = new FileInfo(fileName);
            TextReader input = null;
            TextWriter output = Console.Out;
            long lineNo = 0;
            try
            {
                input = file.OpenText();
                bool squeezed = false;
                string buf;
                while ((buf = input.ReadLine()) != null)
                {
                    if (squeezed && buf.Length == 0)
                    {
                        continue;
                    }

                    squeezed = buf.Length == 0;

                    if (opts.ShowTabs)
                    {
                        buf = buf.Replace("\t", "^I");
                    }

                    if (opts.NumberAll)
                    {
                        if (!opts.NumberNonEmptyLines || buf.Length != 0)
                        {
                            ++lineNo;
                            buf = $"{lineNo,6:d}\t{buf}";
                        }
                    }
                    if (opts.ShowEnds)
                    {
                        buf += "$";
                    }

                    output.WriteLine(buf);
                }
            }
            catch (IOException ex)
            {
                Console.WriteLine();<commit_msg>Add handling of -v. Pretty much complete cat that works similarly to Ubuntu 16.04 one.<commit_after>﻿using System;
using System.IO;
using System.Text;
using CommandLine;

namespace Khondar.UNIXUtils.Concat
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var options = new Options();
            if (Parser.Default.ParseArguments(args, options))
            {
                if (options.Version)
                {
                    var assemblyInfo = typeof(Program).Assembly.GetName();
                    Console.WriteLine(assemblyInfo.Name + " v" + assemblyInfo.Version);
                }

                if (options.NumberNonEmptyLines)
                {
                    options.NumberAll = true;
                }

                if (options.ShowAll)
                {
                    options.ShowNonPrintingWithEnds = true;
                    options.ShowNonPrintingWithTabs = true;
                }

                if (options.ShowNonPrintingWithEnds)
                {
                    options.ShowNonPrinting = true;
                    options.ShowEnds = true;
                }

                if (options.ShowNonPrintingWithTabs)
                {
                    options.ShowNonPrinting = true;
                    options.ShowTabs = true;
                }

                foreach (var fileName in options.FileNames)
                {
                    DisplayFile(fileName, options);
                }
            }
        }

        private static void DisplayFile(string fileName, Options opts)
        {
            var file = new FileInfo(fileName);
            TextReader input = null;
            var output = Console.Out;
            long lineNo = 0;
            try
            {
                input = file.OpenText();
                var squeezed = false;
                string buf;
                while ((buf = input.ReadLine()) != null)
                {
                    if (opts.SqueezeBlanks && squeezed && buf.Length == 0)
                    {
                        continue;
                    }

                    squeezed = buf.Length == 0;

                    if (opts.ShowTabs)
                    {
                        buf = buf.Replace("\t", "^I");
                    }

                    if (opts.ShowEnds)
                    {
                        buf += "$";
                    }

                    if (opts.ShowNonPrinting)
                    {
                        var sb = new StringBuilder();

                        // logic for non-printables translated from http://git.savannah.gnu.org/cgit/coreutils.git/tree/src/cat.c
                        foreach (var c in buf)
                        {
                            if (c < 32)
                            {
                                sb.Append($"^{(char) (c + 64)}");
                            }
                            else
                            {
                                if (c < 127)
                                {
                                    sb.Append(c);
                                }
                                else if (c == 127)
                                {
                                    sb.Append("^?");
                                }
                                else
                                {
                                    sb.Append("M-");
                                    if (c >= 128 + 32)
                                    {
                                        if (c < 128 + 127)
                                        {
                                            sb.Append((char) (c - 127));
                                        }
                                        else
                                        {
                                            sb.Append("^?");
                                        }
                                    }
                                    else
                                    {
                                        sb.Append($"^{(char) (c - 128 + 64)}");
                                    }
                                }
                            }
                        }

                        buf = sb.ToString();
                    }

                    if (opts.NumberAll)
                    {
                        if (!opts.NumberNonEmptyLines || buf.Length != 0)
                        {
                            ++lineNo;
                            buf = $"{lineNo,6:d}\t{buf}";
                        }
                    }

                    output.WriteLine(buf);
                }
            }
            catch (IOException ex)
            {
                Console.WriteLine();
{'fraction_non_alphanumeric': 0.07911145752143414, 'fraction_numerical': 0.005585866458820473, 'mean_word_length': 0.6810043668122271, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '566625', 'n_tokens_mistral': 1755, 'n_tokens_neox': 1558, 'n_words': 321}