marzer / tomlplusplus

Header-only TOML config file parser and serializer for C++17.
https://marzer.github.io/tomlplusplus/
MIT License
1.53k stars 146 forks source link

Indendation customization? #212

Closed Raildex closed 11 months ago

Raildex commented 11 months ago

Is your feature request related to a problem? Please describe. I wam using TOML for config files. Nested tables receive an indendation when using an std::ostream like in stream << table << std::endl

toml::table tbl {
    {"graphics", toml::table{
        {"display",toml::table{
            {"windowed",s.graphics.windowed},
            {"refresh_rate",int(s.graphics.refresh_rate)},
            {"resolution",toml::table{
                {"width",int(s.graphics.resolution_width)},
                {"height",int(s.graphics.resolution_height)},
            }}
    }},
        {"quality",toml::table{
            {"animation",int(s.graphics.animation)},
            {"fog",int(s.graphics.fog)},
            {"lighting",int(s.graphics.lighting)},
            {"particle",int(s.graphics.particle)},
            {"post-processing",int(s.graphics.post_processing)},
            {"reflection",int(s.graphics.reflection)},
            {"texture",int(s.graphics.texture)},
            {"shadow",int(s.graphics.shadow)},
        }}
    }},
    {"audio", toml::table{
        {"volume", toml::table{
            {"dialogue",s.audio.dialogue_volume},
            {"music",s.audio.music_volume},
            {"effect",s.audio.effect_volume},
        }},
        {"quality",toml::table{
            {"sound",int(s.audio.quality)},
            {"reverb",int(s.audio.reverb)}
        }},
        {"mic-mode",int(s.audio.mic_mode)},

    }},
    {"controls", toml::table{
        {"control-method",int(s.controls.method)},
        {"keyboard-layout",keyboard_layout_arr},
        {"pad-layout",pad_layout_arr}
    }},
    {"language",get_language_string(s.lang)}
};
stream << tbl << std::endl;
language = 'english'

[audio]
mic-mode = 0

    [audio.quality]
    reverb = 2
    sound = 2

    [audio.volume]
    dialogue = 1.0
    effect = 1.0
    music = 1.0

[controls]
control-method = 0
keyboard-layout = [ 200, 208, 203, 205, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
pad-layout = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]

[graphics.display]
refresh_rate = 60
windowed = true

    [graphics.display.resolution]
    height = 450
    width = 800

[graphics.quality]
animation = 2
fog = 2
lighting = 2
particle = 2
post-processing = 2
reflection = 2
shadow = 2
texture = 2

Describe the solution you'd like

I would like to have an option to alter the indendation/completely remove indentation.

Additional context

marzer commented 11 months ago

This option is available to you already, using the toml_formatter and format_flags:

// given (for exposition purposes):
using namespace toml;
using std::cout;
auto tbl = toml::parse_file("config.toml");

// instead of this:
cout << tbl;

// do this:
cout << toml_formatter{ tbl, toml_formatter::default_flags & ~format_flags::indent_sub_tables };

A toml_formatter instance is implicitly created when you do a 'raw' operator<<, so here I'm just making it explicit to modify the format flags.

Hope that helps :)