Skip to main content

comp

Function comp 

Source
pub fn comp(
    left: Box<Layout>,
    right: Box<Layout>,
    pad: bool,
    fix: bool,
) -> Box<Layout>
Expand description

Creates a composition that may break across lines based on width constraints.

The comp constructor is the fundamental building block for creating layouts that adapt to available width. It combines two layouts with configurable spacing and breaking behavior. This is the most flexible constructor and the basis for most other layout combinations.

§Parameters

  • left - The left layout in the composition
  • right - The right layout in the composition
  • pad - Whether to add a space between layouts when they fit on one line
  • fix - Whether to prevent this composition from breaking across lines

§Returns

A boxed Layout::Comp with the specified breaking and padding behavior.

§Examples

use typeset::*;

// Padded composition (adds space when on one line)
let padded = comp(
    text_str("function"),
    text_str("name()"),
    true,  // pad = true
    false  // fix = false (can break)
);

// Fits on one line: "function name()"
// When broken: "function\nname()"
use typeset::*;

// Unpadded composition (no space)
let unpadded = comp(
    text_str("["),
    comp(
        text_str("content"),
        text_str("]"),
        false, false
    ),
    false, // pad = false
    false  // fix = false
);

// Always renders as: "[content]"
use typeset::*;

// Fixed composition (never breaks)
let fixed = comp(
    text_str("operator"),
    text_str("operand"),
    true,  // pad = true
    true   // fix = true (never breaks)
);

// Always on one line: "operator operand"

§Parameters

§pad Parameter

  • true: Adds a single space between layouts when they fit on one line
  • false: No space added - layouts are directly adjacent

§fix Parameter

  • true: This composition never breaks, similar to wrapping in [fix]
  • false: This composition can break based on width constraints

§Behavior

  • When both layouts fit on the current line, they’re placed adjacent (with optional padding)
  • When they don’t fit, a line break is inserted and the right layout moves to the next line
  • Breaking decisions consider the entire layout context, not just this composition
  • Fixed compositions (fix=true) never break regardless of width

§See Also

  • pad, unpad - Convenience functions for common padding patterns
  • fix_pad, fix_unpad - Convenience functions with fixing
  • line() - For unconditional line breaks
  • [fix] - Alternative way to prevent breaking