My agentic slop goes here. Not intended for anyone else!

more

Changed files
+201 -206
tgp
+31 -13
tgp/example/demo.ml
···
(* Custom metadata *)
print_newline ();
-
let custom =
-
Textsize.empty
-
|> Textsize.with_scale (Textsize.make_scale 3)
-
|> Textsize.with_width (Textsize.make_width 5)
-
in
+
let custom = Textsize.v ~scale:3 ~width:5 () in
print_string (Textsize.render custom "Custom sized text");
print_endline "\n\n";
(* Fractional with alignment *)
-
let aligned =
-
Textsize.empty
-
|> Textsize.with_fraction
-
(Textsize.make_numerator 1)
-
(Textsize.make_denominator 3)
-
|> Textsize.with_vertical_align Textsize.Center
-
|> Textsize.with_horizontal_align Textsize.Center
-
in
+
let aligned = Textsize.v ~fraction:(1, 3) ~vertical:`Center ~horizontal:`Center () in
print_string (Textsize.render aligned "Centered 1/3 size text");
print_newline ();
+
+
(* Fmt-style combinators *)
+
print_endline "\n=== Fmt-style Examples ===\n";
+
+
(* Basic pp formatter *)
+
Fmt.pr "This is %a text\n" Textsize.pp_double "double-sized";
+
Fmt.pr "This is %a text\n" Textsize.pp_triple "triple-sized";
+
+
(* Superscripts and subscripts with Fmt *)
+
Fmt.pr "E=mc%a\n" Textsize.pp_superscript "2";
+
Fmt.pr "H%aO\n" Textsize.pp_subscript "2";
+
+
(* Styled combinators wrapping other formatters *)
+
Fmt.pr "The answer is %a\n" (Textsize.styled_double Fmt.int) 42;
+
Fmt.pr "Pi is approximately %a\n" (Textsize.styled_triple Fmt.float) 3.14159;
+
+
(* Complex formatting with Fmt *)
+
Fmt.pr "Temperature: %a°C (that's %a°F)\n"
+
(Textsize.styled_superscript Fmt.int) 25
+
(Textsize.styled_subscript Fmt.int) 77;
+
+
(* Custom metadata with Fmt *)
+
let custom_meta = Textsize.v ~scale:4 () in
+
Fmt.pr "\n%a\n" (Textsize.pp custom_meta) "Custom Fmt-styled text";
+
+
(* Composing with other Fmt combinators *)
+
Fmt.pr "\nList items: %a\n"
+
(Fmt.list ~sep:(Fmt.any ", ") (Textsize.styled_double Fmt.string))
+
["apple"; "banana"; "cherry"];
print_endline "\n=== Demo Complete ===";
+1 -1
tgp/example/dune
···
(executable
(name demo)
-
(libraries textsize))
+
(libraries textsize fmt))
+2 -1
tgp/src/dune
···
(library
(name textsize)
-
(public_name textsize))
+
(public_name textsize)
+
(libraries fmt))
+85 -97
tgp/src/textsize.ml
···
(* Types *)
-
type scale = int
-
type width = int
-
type numerator = int
-
type denominator = int
-
-
type vertical_align =
-
| Bottom
-
| Center
-
| Top
+
type vertical = [ `Bottom | `Center | `Top ]
+
type horizontal = [ `Left | `Center | `Right ]
-
type horizontal_align =
-
| Left
-
| Center
-
| Right
-
-
type metadata = {
-
scale : scale option;
-
width : width option;
-
numerator : numerator option;
-
denominator : denominator option;
-
vertical : vertical_align option;
-
horizontal : horizontal_align option;
+
type t = {
+
scale : int option;
+
width : int option;
+
numerator : int option;
+
denominator : int option;
+
vertical : vertical option;
+
horizontal : horizontal option;
}
-
(* Constructors with validation *)
+
(* Validation helpers *)
-
let make_scale n =
+
let validate_scale n =
if n < 1 || n > 7 then
invalid_arg (Printf.sprintf "scale must be in range 1-7, got %d" n)
-
else
-
n
-
let make_width n =
+
let validate_width n =
if n < 0 || n > 7 then
invalid_arg (Printf.sprintf "width must be in range 0-7, got %d" n)
-
else
-
n
-
let make_numerator n =
+
let validate_numerator n =
if n < 0 || n > 15 then
invalid_arg (Printf.sprintf "numerator must be in range 0-15, got %d" n)
-
else
-
n
-
let make_denominator n =
+
let validate_denominator n =
if n < 0 || n > 15 then
invalid_arg (Printf.sprintf "denominator must be in range 0-15, got %d" n)
-
else
-
n
-
(* Metadata creation *)
+
(* Constructor *)
-
let empty = {
-
scale = None;
-
width = None;
-
numerator = None;
-
denominator = None;
-
vertical = None;
-
horizontal = None;
-
}
+
let v ?scale ?width ?fraction ?vertical ?horizontal () =
+
Option.iter validate_scale scale;
+
Option.iter validate_width width;
+
let numerator, denominator = match fraction with
+
| None -> None, None
+
| Some (num, den) ->
+
validate_numerator num;
+
validate_denominator den;
+
Some num, Some den
+
in
+
{
+
scale;
+
width;
+
numerator;
+
denominator;
+
vertical;
+
horizontal;
+
}
-
let with_scale s metadata = { metadata with scale = Some s }
-
let with_width w metadata = { metadata with width = Some w }
-
-
let with_fraction num den metadata =
-
{ metadata with numerator = Some num; denominator = Some den }
-
-
let with_vertical_align v metadata = { metadata with vertical = Some v }
-
let with_horizontal_align h metadata = { metadata with horizontal = Some h }
+
let empty = v ()
(* Conversion helpers *)
-
let vertical_align_to_int = function
-
| Bottom -> 0
-
| Center -> 1
-
| Top -> 2
+
let vertical_to_int = function
+
| `Bottom -> 0
+
| `Center -> 1
+
| `Top -> 2
-
let horizontal_align_to_int = function
-
| Left -> 0
-
| Center -> 1
-
| Right -> 2
+
let horizontal_to_int = function
+
| `Left -> 0
+
| `Center -> 1
+
| `Right -> 2
(* Escape sequence generation *)
-
let metadata_to_string metadata =
+
let metadata_to_string t =
let parts = [] in
-
let parts = match metadata.scale with
+
let parts = match t.scale with
| Some s -> (Printf.sprintf "s=%d" s) :: parts
| None -> parts
in
-
let parts = match metadata.width with
+
let parts = match t.width with
| Some w -> (Printf.sprintf "w=%d" w) :: parts
| None -> parts
in
-
let parts = match metadata.numerator with
+
let parts = match t.numerator with
| Some n -> (Printf.sprintf "n=%d" n) :: parts
| None -> parts
in
-
let parts = match metadata.denominator with
+
let parts = match t.denominator with
| Some d -> (Printf.sprintf "d=%d" d) :: parts
| None -> parts
in
-
let parts = match metadata.vertical with
-
| Some v -> (Printf.sprintf "v=%d" (vertical_align_to_int v)) :: parts
+
let parts = match t.vertical with
+
| Some v -> (Printf.sprintf "v=%d" (vertical_to_int v)) :: parts
| None -> parts
in
-
let parts = match metadata.horizontal with
-
| Some h -> (Printf.sprintf "h=%d" (horizontal_align_to_int h)) :: parts
+
let parts = match t.horizontal with
+
| Some h -> (Printf.sprintf "h=%d" (horizontal_to_int h)) :: parts
| None -> parts
in
String.concat ":" (List.rev parts)
-
let render metadata text =
+
let render t text =
(* Validate text length (max 4096 bytes of UTF-8) *)
let text_len = String.length text in
if text_len > 4096 then
invalid_arg (Printf.sprintf "text exceeds 4096 bytes (got %d)" text_len);
-
let metadata_str = metadata_to_string metadata in
+
let metadata_str = metadata_to_string t in
(* OSC 66 ; metadata ; text BEL *)
(* Using \x1b for ESC and \x07 for BEL *)
···
else
Printf.sprintf "\x1b]66;%s;%s\x07" metadata_str text
-
let render_to_channel oc metadata text =
-
output_string oc (render metadata text);
+
let render_to_channel oc t text =
+
output_string oc (render t text);
flush oc
-
(* Convenience functions *)
+
(* Fmt-style combinators *)
+
+
let pp t ppf text =
+
Format.pp_print_string ppf (render t text)
+
+
let styled t pp_inner ppf value =
+
(* We need to capture the inner formatter's output as a string first *)
+
let inner_str = Format.asprintf "%a" pp_inner value in
+
Format.pp_print_string ppf (render t inner_str)
+
+
(* Convenience Fmt formatters *)
-
let double text =
-
render (with_scale (make_scale 2) empty) text
+
let pp_double = pp (v ~scale:2 ())
+
let pp_triple = pp (v ~scale:3 ())
+
let pp_quadruple = pp (v ~scale:4 ())
+
let pp_half = pp (v ~fraction:(1, 2) ())
-
let triple text =
-
render (with_scale (make_scale 3) empty) text
+
let pp_superscript = pp (v ~fraction:(1, 2) ~vertical:`Top ())
+
let pp_subscript = pp (v ~fraction:(1, 2) ~vertical:`Bottom ())
-
let quadruple text =
-
render (with_scale (make_scale 4) empty) text
+
let pp_scaled n = pp (v ~scale:n ())
-
let half text =
-
render
-
(with_fraction (make_numerator 1) (make_denominator 2) empty)
-
text
+
let styled_double pp_inner = styled (v ~scale:2 ()) pp_inner
+
let styled_triple pp_inner = styled (v ~scale:3 ()) pp_inner
-
let superscript text =
-
render
-
(empty
-
|> with_fraction (make_numerator 1) (make_denominator 2)
-
|> with_vertical_align Top)
-
text
+
let styled_superscript pp_inner = styled (v ~fraction:(1, 2) ~vertical:`Top ()) pp_inner
+
let styled_subscript pp_inner = styled (v ~fraction:(1, 2) ~vertical:`Bottom ()) pp_inner
-
let subscript text =
-
render
-
(empty
-
|> with_fraction (make_numerator 1) (make_denominator 2)
-
|> with_vertical_align Bottom)
-
text
+
(* Convenience functions *)
-
let scaled n text =
-
render (with_scale (make_scale n) empty) text
+
let double text = render (v ~scale:2 ()) text
+
let triple text = render (v ~scale:3 ()) text
+
let quadruple text = render (v ~scale:4 ()) text
+
let half text = render (v ~fraction:(1, 2) ()) text
+
let superscript text = render (v ~fraction:(1, 2) ~vertical:`Top ()) text
+
let subscript text = render (v ~fraction:(1, 2) ~vertical:`Bottom ()) text
+
let scaled n text = render (v ~scale:n ()) text
+74 -77
tgp/src/textsize.mli
···
(** {1 Types} *)
-
(** Scale factor for text rendering (1-7).
+
(** Vertical alignment for fractionally scaled text. *)
+
type vertical = [ `Bottom | `Center | `Top ]
-
Controls the overall size of the text block. A scale of [s] will render text
-
in a block of [s * w] by [s] cells (where [w] is the width parameter). *)
-
type scale = private int
+
(** Horizontal alignment for fractionally scaled text. *)
+
type horizontal = [ `Left | `Center | `Right ]
-
(** Width in cells for text rendering (0-7).
+
(** Abstract metadata for text sizing. *)
+
type t
-
When set to 0, width is auto-calculated based on text length. *)
-
type width = private int
+
(** {1 Constructor} *)
-
(** Fractional scaling numerator (0-15).
+
(** [v ?scale ?width ?fraction ?vertical ?horizontal ()] creates text sizing metadata.
-
Used together with denominator for precise fractional scaling. *)
-
type numerator = private int
+
@param scale Scale factor (1-7)
+
@param width Width in cells (0-7)
+
@param fraction Fractional scaling as (numerator, denominator) where both are 0-15
+
@param vertical Vertical alignment
+
@param horizontal Horizontal alignment
+
@raise Invalid_argument if any parameter is out of range *)
+
val v :
+
?scale:int ->
+
?width:int ->
+
?fraction:(int * int) ->
+
?vertical:vertical ->
+
?horizontal:horizontal ->
+
unit -> t
-
(** Fractional scaling denominator (0-15).
+
(** Empty metadata (all fields set to [None]). Equivalent to [v ()]. *)
+
val empty : t
-
Used together with numerator for precise fractional scaling. *)
-
type denominator = private int
+
(** {1 Escape Sequence Generation} *)
-
(** Vertical alignment for fractionally scaled text.
-
- [Bottom]: Align to bottom (0)
-
- [Center]: Align to center (1)
-
- [Top]: Align to top (2) *)
-
type vertical_align =
-
| Bottom
-
| Center
-
| Top
+
(** [render t text] generates the complete escape sequence for sized text.
-
(** Horizontal alignment for fractionally scaled text.
-
- [Left]: Align to left (0)
-
- [Center]: Align to center (1)
-
- [Right]: Align to right (2) *)
-
type horizontal_align =
-
| Left
-
| Center
-
| Right
+
@param t The sizing metadata
+
@param text The text to render (max 4096 bytes of UTF-8)
+
@return The escape sequence string
+
@raise Invalid_argument if text exceeds 4096 bytes *)
+
val render : t -> string -> string
-
(** Metadata for text sizing. *)
-
type metadata = {
-
scale : scale option;
-
width : width option;
-
numerator : numerator option;
-
denominator : denominator option;
-
vertical : vertical_align option;
-
horizontal : horizontal_align option;
-
}
+
(** [render_to_channel oc t text] writes the escape sequence to a channel.
-
(** {1 Constructors} *)
+
@param oc Output channel
+
@param t The sizing metadata
+
@param text The text to render (max 4096 bytes of UTF-8)
+
@raise Invalid_argument if text exceeds 4096 bytes *)
+
val render_to_channel : out_channel -> t -> string -> unit
-
(** [make_scale n] creates a scale value.
-
@raise Invalid_argument if [n] is not in range 1-7. *)
-
val make_scale : int -> scale
+
(** {1 Fmt-style Combinators} *)
-
(** [make_width n] creates a width value.
-
@raise Invalid_argument if [n] is not in range 0-7. *)
-
val make_width : int -> width
+
(** [pp t] creates a Fmt formatter that wraps text with sizing metadata.
-
(** [make_numerator n] creates a numerator value.
-
@raise Invalid_argument if [n] is not in range 0-15. *)
-
val make_numerator : int -> numerator
+
Example:
+
{[
+
Fmt.pr "This is %a" (Textsize.pp (Textsize.with_scale 2 Textsize.empty)) "big"
+
]}
+
*)
+
val pp : t -> string Fmt.t
-
(** [make_denominator n] creates a denominator value.
-
@raise Invalid_argument if [n] is not in range 0-15. *)
-
val make_denominator : int -> denominator
+
(** [styled t pp_inner] wraps any formatter with sizing metadata.
-
(** {1 Metadata Creation} *)
+
Example:
+
{[
+
Fmt.pr "Value: %a" (Textsize.styled (Textsize.with_scale 3 Textsize.empty) Fmt.int) 42
+
]}
+
*)
+
val styled : t -> 'a Fmt.t -> 'a Fmt.t
-
(** Empty metadata (all fields set to [None]). *)
-
val empty : metadata
+
(** Fmt formatter for double-sized text. *)
+
val pp_double : string Fmt.t
-
(** [with_scale s metadata] sets the scale. *)
-
val with_scale : scale -> metadata -> metadata
+
(** Fmt formatter for triple-sized text. *)
+
val pp_triple : string Fmt.t
-
(** [with_width w metadata] sets the width. *)
-
val with_width : width -> metadata -> metadata
+
(** Fmt formatter for quadruple-sized text. *)
+
val pp_quadruple : string Fmt.t
-
(** [with_fraction num den metadata] sets fractional scaling. *)
-
val with_fraction : numerator -> denominator -> metadata -> metadata
+
(** Fmt formatter for half-sized text. *)
+
val pp_half : string Fmt.t
-
(** [with_vertical_align v metadata] sets vertical alignment. *)
-
val with_vertical_align : vertical_align -> metadata -> metadata
+
(** Fmt formatter for superscript text. *)
+
val pp_superscript : string Fmt.t
-
(** [with_horizontal_align h metadata] sets horizontal alignment. *)
-
val with_horizontal_align : horizontal_align -> metadata -> metadata
+
(** Fmt formatter for subscript text. *)
+
val pp_subscript : string Fmt.t
-
(** {1 Escape Sequence Generation} *)
+
(** [pp_scaled n] creates a Fmt formatter for text at scale [n].
+
@raise Invalid_argument if [n] is not in range 1-7. *)
+
val pp_scaled : int -> string Fmt.t
-
(** [render metadata text] generates the complete escape sequence for sized text.
+
(** [styled_double pp_inner] wraps any formatter with double sizing. *)
+
val styled_double : 'a Fmt.t -> 'a Fmt.t
-
@param metadata The sizing metadata
-
@param text The text to render (max 4096 bytes of UTF-8)
-
@return The escape sequence string
-
@raise Invalid_argument if text exceeds 4096 bytes *)
-
val render : metadata -> string -> string
+
(** [styled_triple pp_inner] wraps any formatter with triple sizing. *)
+
val styled_triple : 'a Fmt.t -> 'a Fmt.t
-
(** [render_to_channel oc metadata text] writes the escape sequence to a channel.
+
(** [styled_superscript pp_inner] wraps any formatter with superscript styling. *)
+
val styled_superscript : 'a Fmt.t -> 'a Fmt.t
-
@param oc Output channel
-
@param metadata The sizing metadata
-
@param text The text to render (max 4096 bytes of UTF-8)
-
@raise Invalid_argument if text exceeds 4096 bytes *)
-
val render_to_channel : out_channel -> metadata -> string -> unit
+
(** [styled_subscript pp_inner] wraps any formatter with subscript styling. *)
+
val styled_subscript : 'a Fmt.t -> 'a Fmt.t
(** {1 Convenience Functions} *)
+8 -17
tgp/test/test_textsize.ml
···
(* Test scale validation *)
test "scale validation - valid range" (fun () ->
-
let _ = Textsize.make_scale 1 in
-
let _ = Textsize.make_scale 7 in
+
let _ = Textsize.v ~scale:1 () in
+
let _ = Textsize.v ~scale:7 () in
()
);
test "scale validation - rejects invalid" (fun () ->
try
-
let _ = Textsize.make_scale 0 in
+
let _ = Textsize.v ~scale:0 () in
failwith "Should have raised Invalid_argument"
with Invalid_argument _ -> ()
);
(* Test width validation *)
test "width validation - valid range" (fun () ->
-
let _ = Textsize.make_width 0 in
-
let _ = Textsize.make_width 7 in
+
let _ = Textsize.v ~width:0 () in
+
let _ = Textsize.v ~width:7 () in
()
);
···
(* Test custom metadata *)
test "custom metadata - scale and width" (fun () ->
-
let metadata =
-
Textsize.empty
-
|> Textsize.with_scale (Textsize.make_scale 3)
-
|> Textsize.with_width (Textsize.make_width 5)
-
in
+
let metadata = Textsize.v ~scale:3 ~width:5 () in
let result = Textsize.render metadata "custom" in
assert (result = "\x1b]66;s=3:w=5;custom\x07")
);
test "custom metadata - fractional" (fun () ->
-
let metadata =
-
Textsize.empty
-
|> Textsize.with_fraction
-
(Textsize.make_numerator 2)
-
(Textsize.make_denominator 3)
-
in
+
let metadata = Textsize.v ~fraction:(2, 3) () in
let result = Textsize.render metadata "frac" in
assert (result = "\x1b]66;n=2:d=3;frac\x07")
);
test "empty metadata" (fun () ->
-
let result = Textsize.render Textsize.empty "plain" in
+
let result = Textsize.render (Textsize.v ()) "plain" in
assert (result = "\x1b]66;;plain\x07")
);