예제 #1
0
 public string Decode(string text)
 {
     try
     {
         return(Ecoji.DecodeUtf8(text));
     }
     catch
     {
         return(null);
     }
 }
예제 #2
0
 public string Encode(string text)
 {
     try
     {
         return(Ecoji.Encode(text));
     }
     catch
     {
         return(null);
     }
 }
예제 #3
0
    static int Main(string[] args)
    {
        var decode      = false;
        var wrap        = 76;
        var showHelp    = false;
        var showVersion = false;

        var options = new OptionSet
        {
            { "usage: ecoji [OPTIONS]... [FILE]" },
            { "" },
            { "Encode or decode data as Unicode emojis. 😻🍹" },
            { "" },
            { "Options:" },
            {
                "d|decode",
                "Decode data.",
                v => decode = v != null
            },
            {
                "w|wrap=",
                "Wrap encoded lines after {COLS} characters (default 76). " +
                "Use 0 to disable line wrapping.",
                (int v) => wrap = v
            },
            {
                "h|help",
                "Print this message.",
                v => showHelp = v != null
            },
            {
                "v|version",
                "Print version information.",
                v => showVersion = v != null
            }
        };

        void ShowHelp()
        => options.WriteOptionDescriptions(Console.Out);

        void ShowVersion()
        {
            Console.WriteLine($"Ecoji (.NET Core) version {version}");
            Console.WriteLine($"  Copyright   : {copyright}");
            Console.WriteLine($"  License     : MIT");
            Console.WriteLine($"  Source code : https://github.com/abock/dotnet-ecoji");
            Console.WriteLine();
            Console.WriteLine($"Based on Ecoji by Keith Turner:");
            Console.WriteLine($"  https://github.com/keith-turner/ecoji");
        }

        try
        {
            var positional = options.Parse(args);

            if (showHelp)
            {
                ShowHelp();
                return(ExitShowHelp);
            }

            if (showVersion)
            {
                ShowVersion();
                return(ExitShowVersion);
            }

            Stream inputStream;

            if (positional is null ||
                positional.Count == 0 ||
                positional[0] == "-")
            {
                inputStream = Console.OpenStandardInput();
            }
            else if (positional.Count == 1)
            {
                inputStream = new FileStream(
                    positional[0],
                    FileMode.Open,
                    FileAccess.Read);
            }
            else
            {
                throw new Exception("more than one file was provided");
            }

            try
            {
                using (inputStream)
                {
                    if (decode)
                    {
                        using var stdout = Console.OpenStandardOutput();
                        Ecoji.Decode(inputStream, stdout);
                    }
                    else
                    {
                        Ecoji.Encode(inputStream, Console.Out, wrap);
                    }
                }
            }
            catch (Exception e)
            {
                WriteError($"pipe or encoding/decoding error: {e}");
                return(ExitDataError);
            }

            return(ExitSuccess);
        }