Commit 1fb246fa authored by Paul Asmuth's avatar Paul Asmuth
Browse files

add the 'base' formatter

parent df1615f4
Loading
Loading
Loading
Loading
+56 −0
Original line number Diff line number Diff line
@@ -173,6 +173,57 @@ ReturnCode format_configure_datetime(
  return OK;
}

Formatter format_base_fixed(unsigned base, size_t precision) {
  return [base, precision] (size_t idx, const std::string& vs) -> std::string {
    auto v = value_to_float(vs);
    if (v == 0) {
      return "0";
    }

    std::stringstream sstream;
    sstream
        << base
        << std::fixed
        << std::setprecision(precision)
        << format_superscript(log(v) / log(base));

    return sstream.str();
  };
}

ReturnCode format_configure_base_fixed(
    const Expr* expr,
    Formatter* formatter) {
  auto args = expr_collect(expr);

  double base;
  size_t precision = 1;
  switch (args.size()) {
    case 2:
      try {
        precision = std::stod(expr_get_value(args[1]));
      } catch (... ) {
        return ERROR;
      }
      /* fallthrough */
    case 1:
      try {
        base = std::stod(expr_get_value(args[0]));
      } catch (... ) {
        return ERROR;
      }
      break;
    default:
      return errorf(
          ERROR,
          "invalid number of arguments for 'base'; expected one or two, but got: {}",
          args.size());
  }

  *formatter = format_base_fixed(base, precision);
  return OK;
}

Formatter format_string() {
  return [] (size_t idx, const std::string& v) -> std::string {
    return v;
@@ -244,6 +295,10 @@ ReturnCode format_configure(
    return format_configure_datetime(expr_next(expr), formatter);
  }

  if (expr_is_value(expr, "base")) {
    return format_configure_base_fixed(expr_next(expr), formatter);
  }

  if (expr_is_value(expr, "string")) {
    return format_configure_string(expr_next(expr), formatter);
  }
@@ -258,6 +313,7 @@ ReturnCode format_configure(
      "  - fixed\n"
      "  - scientific\n",
      "  - datetime\n",
      "  - base\n",
      "  - string\n",
      "  - custom\n",
      expr_inspect(expr));
+1 −0
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ using Formatter = std::function<std::string (size_t idx, const std::string&)>;
Formatter format_decimal_fixed(size_t precision);
Formatter format_decimal_scientific(size_t precision);
Formatter format_datetime(const std::string& fmt);
Formatter format_base_fixed(unsigned base, size_t precision);
Formatter format_string();
Formatter format_custom(const std::vector<std::string>& values);
Formatter format_noop();
+7 −0
Original line number Diff line number Diff line
(set height 80px)
(set margin 2em)

(plot/axis
    scale (logarithmic 2)
    label-format (base 2)
    limit (0 4096))
+30 −0
Original line number Diff line number Diff line
<svg xmlns="http://www.w3.org/2000/svg" width="900.000000" height="80.000000" viewBox="0 0 900 80">
  <rect width="900.000000" height="80.000000" fill="#ffffff"/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M39.1111 40 L860.889 40 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M39.1111 37.3333 L39.1111 42.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M107.593 37.3333 L107.593 42.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M176.074 37.3333 L176.074 42.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M244.556 37.3333 L244.556 42.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M313.037 37.3333 L313.037 42.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M381.519 37.3333 L381.519 42.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M450 37.3333 L450 42.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M518.481 37.3333 L518.481 42.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M586.963 37.3333 L586.963 42.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M655.444 37.3333 L655.444 42.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M723.926 37.3333 L723.926 42.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M792.407 37.3333 L792.407 42.6667 "/>
  <path stroke-width="1.333333" stroke="#333333" fill="none" d="M860.889 37.3333 L860.889 42.6667 "/>
  <text x="29.532986" y="65.733333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">2⁰</text>
  <text x="101.069155" y="65.733333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif"></text>
  <text x="169.550637" y="65.733333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif"></text>
  <text x="238.032118" y="65.733333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif"></text>
  <text x="303.458912" y="65.733333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">2⁴</text>
  <text x="371.940394" y="65.733333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">2⁵</text>
  <text x="440.421875" y="65.733333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">2⁶</text>
  <text x="508.903356" y="65.733333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">2⁷</text>
  <text x="577.384838" y="65.733333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">2⁸</text>
  <text x="645.866319" y="65.733333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">2⁹</text>
  <text x="711.902488" y="65.733333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">2¹⁰</text>
  <text x="783.438657" y="65.733333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">2¹¹</text>
  <text x="851.920139" y="65.733333" fill="#333333" font-size="14.666667" font-family="Arial,Helvetica,'Helvetica Neue',sans-serif">2¹²</text>
</svg>
 No newline at end of file
+1 −0
Original line number Diff line number Diff line
@@ -4,4 +4,5 @@
(plot/axis
    scale (logarithmic 10)
    tick-placement (exponential-steps 10 10)
    label-format (base 10)
    limit (0 10000))
Loading