Module Spectrum.Simple

Simple one-shot printing interface that handles formatter setup/teardown.

These functions automatically prepare and reset the formatter for each call, making them convenient for one-off printing. For repeated printing to the same formatter, use prepare_ppf directly for better performance.

val printf : ('a, Stdlib.Format.formatter, unit, unit) Stdlib.format4 -> 'a

Equivalent to Format.printf with color tag support.

Automatically prepares stdout formatter, prints with color tags, and resets the formatter after printing.

  (* Basic colored output *)
  Spectrum.Simple.printf "@{<green>Hello@} @{<bold>world@}!@."
  (* Multiple colors and styles *)
  Spectrum.Simple.printf
    "@{<bg:blue,white,bold> INFO @} %s@."
    "Server started"
  (* Format string with values *)
  let count = 42 in
  let status = "ready" in
  Spectrum.Simple.printf
    "@{<cyan>Status:@} %s, @{<yellow>Count:@} %d@."
    status count
  (* Nested tags *)
  Spectrum.Simple.printf
    "@{<green>Success: @{<bold>%d@} items processed@}@."
    100
val eprintf : ('a, Stdlib.Format.formatter, unit, unit) Stdlib.format4 -> 'a

Equivalent to Format.eprintf with color tag support.

Prints to stderr with automatic formatter setup/teardown.

  (* Error message *)
  Spectrum.Simple.eprintf "@{<red>Error:@} File not found@."
  (* Warning message *)
  Spectrum.Simple.eprintf
    "@{<yellow>Warning:@} @{<italic>%s@} is deprecated@."
    "old_function"
  (* Debug output *)
  Spectrum.Simple.eprintf
    "@{<dim>Debug:@} Value = @{<cyan>%d@}@."
    42
val sprintf : ('a, Stdlib.Format.formatter, unit, string) Stdlib.format4 -> 'a

Equivalent to Format.sprintf with color tag support.

Returns a formatted string with ANSI color codes. The returned string can be printed later or used in other contexts.

  (* Create colored string *)
  let msg = Spectrum.Simple.sprintf
      "@{<green>Success:@} %s" "Done" in
  print_endline msg
  (* Build complex message *)
  let error_msg = Spectrum.Simple.sprintf
      "@{<red,bold>ERROR@} [@{<yellow>%s@}]: %s"
      "file.ml" "Syntax error" in
  prerr_endline error_msg
  (* Compose messages *)
  let status = Spectrum.Simple.sprintf "@{<cyan>%d@}" 42 in
  let msg = Spectrum.Simple.sprintf
      "Items: %s @{<dim>(updated)@}" status in
  Printf.printf "%s\n" msg
  (* Format for logging *)
  let log_message level msg =
    let colored = match level with
      | "ERROR" -> Spectrum.Simple.sprintf "@{<red>%s@}" msg
      | "WARN" -> Spectrum.Simple.sprintf "@{<yellow>%s@}" msg
      | "INFO" -> Spectrum.Simple.sprintf "@{<green>%s@}" msg
      | _ -> msg
    in
    Printf.printf "[%s] %s\n" level colored
  in
  log_message "ERROR" "Connection failed";
  log_message "INFO" "Server started"