diff --git a/example/kalman_filter/kalman.h b/example/kalman_filter/kalman.h index c44ec7e7..3e416a8b 100644 --- a/example/kalman_filter/kalman.h +++ b/example/kalman_filter/kalman.h @@ -151,26 +151,53 @@ constexpr Q covariance_extrapolation(Q uncertainty, Q process_noise_variance) } // namespace kalman template -struct fmt::formatter> : formatter { +struct fmt::formatter> { + constexpr auto parse(format_parse_context& ctx) + { + detail::dynamic_specs_handler handler(specs, ctx); + return detail::parse_format_specs(ctx.begin(), ctx.end(), handler); + } + template auto format(const kalman::state& s, FormatContext& ctx) { - std::string txt = [&]{ + memory_buffer value_buffer; + auto to_value_buffer = std::back_inserter(value_buffer); + if (specs.precision != -1) { if constexpr(sizeof...(Qs) == 1) - return fmt::format("{1:%.{0}Q %q}", precision, kalman::get<0>(s)); + format_to(to_value_buffer, "{1:%.{0}Q %q}", specs.precision, kalman::get<0>(s)); else if constexpr(sizeof...(Qs) == 2) - return fmt::format("{{ {1:%.{0}Q %q}, {2:%.{0}Q %q} }}", precision, kalman::get<0>(s), kalman::get<1>(s)); + format_to(to_value_buffer, "{{ {1:%.{0}Q %q}, {2:%.{0}Q %q} }}", specs.precision, kalman::get<0>(s), kalman::get<1>(s)); else - return fmt::format("{{ {1:%.{0}Q %q}, {2:%.{0}Q %q}, {3:%.{0}Q %q} }}", precision, kalman::get<0>(s), kalman::get<1>(s), kalman::get<2>(s)); - }(); - return formatter::format(txt, ctx); + format_to(to_value_buffer, "{{ {1:%.{0}Q %q}, {2:%.{0}Q %q}, {3:%.{0}Q %q} }}", specs.precision, kalman::get<0>(s), kalman::get<1>(s), kalman::get<2>(s)); + } + else { + if constexpr(sizeof...(Qs) == 1) + format_to(to_value_buffer, "{}", kalman::get<0>(s)); + else if constexpr(sizeof...(Qs) == 2) + format_to(to_value_buffer, "{{ {}, {} }}", kalman::get<0>(s), kalman::get<1>(s)); + else + format_to(to_value_buffer, "{{ {}, {}, {} }}", kalman::get<0>(s), kalman::get<1>(s), kalman::get<2>(s)); + } + + basic_memory_buffer global_format_buffer; + units::detail::global_format_specs global_specs = { specs.fill, specs.align, specs.width }; + units::detail::format_global_buffer(std::back_inserter(global_format_buffer), global_specs); + + return format_to(ctx.out(), to_string(global_format_buffer), to_string(value_buffer)); } private: - int precision = 1; // TODO find the easiest way to parse it from context + detail::dynamic_format_specs specs; }; template -struct fmt::formatter> : formatter { +struct fmt::formatter> { + constexpr auto parse(format_parse_context& ctx) + { + detail::dynamic_specs_handler handler(specs, ctx); + return detail::parse_format_specs(ctx.begin(), ctx.end(), handler); + } + template auto format(kalman::estimation e, FormatContext& ctx) { @@ -180,9 +207,22 @@ struct fmt::formatter> : formatter { else return t.relative(); }(kalman::get<0>(e.state)); - std::string txt = fmt::format("{0:%.{2}Q} ± {1:%.{2}Q} {0:%q}", q, sqrt(e.uncertainty), precision); - return formatter::format(txt, ctx); + + memory_buffer value_buffer; + auto to_value_buffer = std::back_inserter(value_buffer); + if (specs.precision != -1) { + format_to(to_value_buffer, "{0:%.{2}Q} ± {1:%.{2}Q} {0:%q}", q, sqrt(e.uncertainty), specs.precision); + } + else { + format_to(to_value_buffer, "{0:%Q} ± {1:%Q} {0:%q}", q, sqrt(e.uncertainty)); + } + + basic_memory_buffer global_format_buffer; + units::detail::global_format_specs global_specs = { specs.fill, specs.align, specs.width }; + units::detail::format_global_buffer(std::back_inserter(global_format_buffer), global_specs); + + return format_to(ctx.out(), to_string(global_format_buffer), to_string(value_buffer)); } private: - int precision = 3; // TODO find the easiest way to parse it from context + detail::dynamic_format_specs specs; }; diff --git a/example/kalman_filter/kalman_filter-example_2.cpp b/example/kalman_filter/kalman_filter-example_2.cpp index 759e8439..cfb9c51d 100644 --- a/example/kalman_filter/kalman_filter-example_2.cpp +++ b/example/kalman_filter/kalman_filter-example_2.cpp @@ -40,7 +40,7 @@ void print_header(const kalman::State auto& initial) void print(auto iteration, Quantity auto measured, const kalman::State auto& current, const kalman::State auto& next) { - fmt::print("{:2} | {:8} | {} | {}\n", iteration, measured, current, next); + fmt::print("{:2} | {:8} | {:.1} | {:.1}\n", iteration, measured, current, next); } int main() diff --git a/example/kalman_filter/kalman_filter-example_3.cpp b/example/kalman_filter/kalman_filter-example_3.cpp index 16e9ff74..32cb41ed 100644 --- a/example/kalman_filter/kalman_filter-example_3.cpp +++ b/example/kalman_filter/kalman_filter-example_3.cpp @@ -35,12 +35,12 @@ using namespace units; void print_header(const kalman::State auto& initial) { fmt::print("Initial: {}\n", initial); - fmt::print("{:>2} | {:>8} | {:>23} | {:>23}\n", "N", "Measured", "Curr. Estimate", "Next Estimate"); + fmt::print("{:>2} | {:>8} | {:>24} | {:>24}\n", "N", "Measured", "Curr. Estimate", "Next Estimate"); } void print(auto iteration, Quantity auto measured, const kalman::State auto& current, const kalman::State auto& next) { - fmt::print("{:2} | {:8} | {} | {}\n", iteration, measured, current, next); + fmt::print("{:2} | {:8} | {:>24.1} | {:>24.1}\n", iteration, measured, current, next); } int main() diff --git a/example/kalman_filter/kalman_filter-example_4.cpp b/example/kalman_filter/kalman_filter-example_4.cpp index d875b26c..41cdea07 100644 --- a/example/kalman_filter/kalman_filter-example_4.cpp +++ b/example/kalman_filter/kalman_filter-example_4.cpp @@ -41,7 +41,7 @@ void print_header(const kalman::State auto& initial) void print(auto iteration, Quantity auto measured, const kalman::State auto& current, const kalman::State auto& next) { - fmt::print("{:2} | {:8} | {:>35} | {:>35}\n", iteration, measured, current, next); + fmt::print("{:2} | {:8} | {:>35.1} | {:>35.1}\n", iteration, measured, current, next); } int main() diff --git a/example/kalman_filter/kalman_filter-example_5.cpp b/example/kalman_filter/kalman_filter-example_5.cpp index de5385c0..1a5b9834 100644 --- a/example/kalman_filter/kalman_filter-example_5.cpp +++ b/example/kalman_filter/kalman_filter-example_5.cpp @@ -40,7 +40,7 @@ void print_header(kalman::estimation initial) template void print(auto iteration, K gain, Q measured, kalman::estimation current, kalman::estimation next) { - fmt::print("{:2} | {:5%.2Q} | {:8} | {:16} | {:16}\n", iteration, gain, measured, current, next); + fmt::print("{:2} | {:5%.2Q} | {:8} | {:>16.2} | {:>16.2}\n", iteration, gain, measured, current, next); } int main() diff --git a/example/kalman_filter/kalman_filter-example_6.cpp b/example/kalman_filter/kalman_filter-example_6.cpp index b9c590ab..d170bcf6 100644 --- a/example/kalman_filter/kalman_filter-example_6.cpp +++ b/example/kalman_filter/kalman_filter-example_6.cpp @@ -61,7 +61,7 @@ void print_header(kalman::estimation initial) template void print(auto iteration, K gain, QP measured, kalman::estimation current, kalman::estimation next) { - fmt::print("{:2} | {:7%.4Q} | {:10%.3Q %q} | {:>18} | {:>18}\n", iteration, gain, measured.relative(), current, next); + fmt::print("{:2} | {:7%.4Q} | {:10%.3Q %q} | {:>18.3} | {:>18.3}\n", iteration, gain, measured.relative(), current, next); } int main() diff --git a/example/kalman_filter/kalman_filter-example_7.cpp b/example/kalman_filter/kalman_filter-example_7.cpp index 0af1d939..65808aa1 100644 --- a/example/kalman_filter/kalman_filter-example_7.cpp +++ b/example/kalman_filter/kalman_filter-example_7.cpp @@ -61,7 +61,7 @@ void print_header(kalman::estimation initial) template void print(auto iteration, K gain, QP measured, kalman::estimation current, kalman::estimation next) { - fmt::print("{:2} | {:7%.4Q} | {:10%.3Q %q} | {:>18} | {:>18}\n", iteration, gain, measured.relative(), current, next); + fmt::print("{:2} | {:7%.4Q} | {:10%.3Q %q} | {:>18.3} | {:>18.3}\n", iteration, gain, measured.relative(), current, next); } int main() diff --git a/example/kalman_filter/kalman_filter-example_8.cpp b/example/kalman_filter/kalman_filter-example_8.cpp index 09d3659a..2ea5138e 100644 --- a/example/kalman_filter/kalman_filter-example_8.cpp +++ b/example/kalman_filter/kalman_filter-example_8.cpp @@ -55,13 +55,13 @@ template void print_header(kalman::estimation initial) { fmt::print("Initial: {}\n", initial); - fmt::print("{:>2} | {:>7} | {:>10} | {:>18} | {:>18}\n", "N", "Gain", "Measured", "Curr. Estimate", "Next Estimate"); + fmt::print("{:>2} | {:>7} | {:>10} | {:>16} | {:>16}\n", "N", "Gain", "Measured", "Curr. Estimate", "Next Estimate"); } template void print(auto iteration, K gain, QP measured, kalman::estimation current, kalman::estimation next) { - fmt::print("{:2} | {:7%.4Q} | {:10%.3Q %q} | {:>18} | {:>18}\n", iteration, gain, measured.relative(), current, next); + fmt::print("{:2} | {:7%.3Q} | {:10%.3Q %q} | {:>16.2} | {:>16.2}\n", iteration, gain, measured.relative(), current, next); } int main()