상속: IJsonImportable
예제 #1
0
        public static JsonPalette Auto(ConsoleBrush defaultBrush)
        {
            string setting;

            try
            {
                setting = Settings.Default[defaultBrush.Background + "Palette"];
            }
            catch (KeyNotFoundException)
            {
                setting = null;
            }

            if (string.IsNullOrEmpty(setting))
            {
                setting      = Settings.BlackPalette;
                defaultBrush = new ConsoleBrush(ConsoleColor.White, ConsoleColor.Black);
            }

            var palette = new JsonPalette(defaultBrush);

            palette.ImportJson(setting);

            return(palette);
        }
예제 #2
0
        static int Main(string[] args)
        {
            try
            {
                var defaultBrush = ConsoleBrush.Current;

                var options = new ProgramOptions();
                options.Help   += delegate { Help(); Environment.Exit(0); };
                options.Palette = JsonPalette.Auto(defaultBrush);
                args            = options.Parse(args);

                var path = args.Length > 0 ? args[0] : "-";

                try
                {
                    try
                    {
                        PrettyColorPrint(path, Console.Out, options.Palette);
                    }
                    finally
                    {
                        //
                        // The location of this finally clause is significant
                        // and should not be merged with the outer catch
                        // block. The default brush needs to be restored
                        // in case an error message is about to be printed
                        // and the standard output and error point to the
                        // same console device.
                        //

                        defaultBrush.Apply();
                    }
                }
                catch (JsonException e)
                {
                    //
                    // In case of JsonException, we don't display the
                    // base exception since the root cause would not provide
                    // line and position information and which JsonException
                    // does. For example, "Unterminated string" has the
                    // root case of FormatException, but which bubble as
                    // JsonException with line and position about where the
                    // error was found in the source.
                    //

                    Console.Error.WriteLine(e.Message);
                    Trace.WriteLine(e.ToString());
                    return(2);
                }

                return(0);
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e.GetBaseException().Message);
                Trace.WriteLine(e.ToString());
                return(1);
            }
        }
예제 #3
0
        static void PrettyColorPrint(string path, TextWriter output, JsonPalette palette)
        {
            Debug.Assert(output != null);

            using (var input = path.Equals("-") ? Console.In : new StreamReader(path))
                using (var reader = new JsonTextReader(input))
                    using (var writer = new JsonTextWriter(output))
                    {
                        writer.PrettyPrint = true;
                        var colorWriter = new JsonColorWriter(writer, palette);
                        colorWriter.WriteFromReader(reader);
                        output.WriteLine();
                    }
        }
예제 #4
0
        private static void PrettyColorPrint(string path, TextWriter output, JsonPalette palette) 
        {
            Debug.Assert(output != null);

            using (TextReader input = path.Equals("-") ? Console.In : new StreamReader(path))
            using (JsonTextReader reader = new JsonTextReader(input))
            using (JsonTextWriter writer = new JsonTextWriter(output))
            {
                writer.PrettyPrint = true;
                JsonColorWriter colorWriter = new JsonColorWriter(writer, palette);
                colorWriter.WriteFromReader(reader);
                output.WriteLine();
            }
        }
예제 #5
0
파일: JsonPalette.cs 프로젝트: db48x/KeeFox
        public static JsonPalette Auto(ConsoleBrush defaultBrush)
        {
            string setting;

            try
            {
                setting = (string) Settings.Default[defaultBrush.Background + "Palette"];
            }
            catch (SettingsPropertyNotFoundException)
            {
                setting = null;
            }

            if (string.IsNullOrEmpty(setting))
            {
                setting = Settings.Default.BlackPalette;
                defaultBrush = new ConsoleBrush(ConsoleColor.White, ConsoleColor.Black);
            }

            var palette = new JsonPalette(defaultBrush);
            palette.ImportJson(setting);

            return palette;
        }
예제 #6
0
 public JsonColorWriter(JsonWriter inner, JsonPalette palette)
 {
     this.inner   = inner;
     this.Palette = palette ?? JsonPalette.Auto();
 }
예제 #7
0
        public string[] Parse(string[] args)
        {
            Debug.Assert(args != null);

            var inputs    = new Queue <string>(args);
            var anonymous = new Queue <string>(args.Length);

            char?altNamedToken = null;

            if (Path.DirectorySeparatorChar != '/')
            {
                altNamedToken = '/';
            }

            while (inputs.Count > 0)
            {
                var arg = DequeueSafely(inputs);

                if (arg.Length > 1 && (arg[0] == '-' || (altNamedToken.HasValue && arg[0] == altNamedToken.Value)))
                {
                    var parts = arg.Split(new[] { '=', ':' }, 2);
                    var name  = parts[0].TrimStart(arg[0]);

                    if (name.Length == 0)
                    {
                        break;
                    }

                    var value = parts.Length > 1 ? parts[1] : string.Empty;

                    switch (name)
                    {
                    case "p":
                    case "palette":
                    {
                        if (value.Length == 0)
                        {
                            continue;
                        }

                        if (value[0] != '{')
                        {
                            value = "{" + value + "}";
                        }

                        Palette.ImportJson(value);
                        break;
                    }

                    case "m":
                    case "mono":
                    {
                        if (Convert.ToBoolean(Mask.EmptyString(value, Boolean.TrueString)))
                        {
                            Palette = new JsonPalette(ConsoleBrush.Current);
                        }
                        break;
                    }

                    case "?":
                    case "help":
                    {
                        if (Help != null)
                        {
                            Help(this, EventArgs.Empty);
                        }
                        break;
                    }

                    default:
                    {
                        throw new ApplicationException(string.Format("Unknown option '{0}'.", arg));
                    }
                    }
                }
                else
                {
                    anonymous.Enqueue(arg);
                }
            }

            args = anonymous.ToArray();
            return(args);
        }
예제 #8
0
 public JsonColorWriter(JsonWriter inner, JsonPalette palette)
 {
     this.inner = inner;
     this.Palette = palette ?? JsonPalette.Auto();
 }