Skip to main content

Segment

Trait Segment 

Source
pub trait Segment: Send {
    // Required method
    fn render(&self, ctx: &DataContext, rc: &RenderContext) -> RenderResult;

    // Provided methods
    fn shrink_to_fit(
        &self,
        ctx: &DataContext,
        rc: &RenderContext,
        target: u16,
    ) -> Option<RenderedSegment> { ... }
    fn data_deps(&self) -> &'static [DataDep] { ... }
    fn defaults(&self) -> SegmentDefaults { ... }
}

Required Methods§

Source

fn render(&self, ctx: &DataContext, rc: &RenderContext) -> RenderResult

Render this segment for the given context.

Returns Ok(None) to hide, Ok(Some(_)) to render, or Err on a runtime failure that the layout engine logs and treats as hidden. See RenderResult. ctx owns the parsed stdin payload (ctx.status) plus lazy accessors for other sources (ctx.usage(), ctx.git(), etc.) declared in data_deps. rc is per-render layout state — terminal width today — for segments that pick their own shape based on available room.

Provided Methods§

Source

fn shrink_to_fit( &self, ctx: &DataContext, rc: &RenderContext, target: u16, ) -> Option<RenderedSegment>

Layout-pressure-aware compaction hook. The reflow loop calls this on any segment under width pressure (truncatable or not), asking whether it can produce a render at most target cells wide. It runs before truncatable end-ellipsis truncation, so segment-side intelligence beats generic string clipping when both apply. Default returns None (no compact form available; engine falls through to truncatable or drop). Segments with structured tail content override to shed decoration while keeping the signal-bearing prefix.

The returned render must lie in [width.min, target] cells: wider violates the layout-fit invariant (engine rejects and warns), narrower violates the user’s width.min contract (engine rejects silently, same as apply_width_bounds). Implementations should return None rather than emit a render outside this range.

See docs/specs/segment-system.md §Layout algorithm for the reflow loop’s full ordering and target derivation.

Source

fn data_deps(&self) -> &'static [DataDep]

Declare which data sources this segment reads. The runtime computes the union across all enabled segments and lazy-fetches only those sources. Defaults to the stdin payload only; segments that read other sources must override. See docs/specs/data-fetching.md §Segment dependency declaration.

The &'static lifetime is deliberate: built-in segments return a const &[DataDep] at zero cost, and runtime-loaded plugin segments (e.g. RhaiSegment) promote their parsed Vec<DataDep> via Vec::leak once at plugin-load time. The plugin registry is built once per process and lives until exit, so the leak is bounded. If plugin hot-reload arrives (deferred feature), swap to an arena allocator or Arc<[DataDep]>.

Source

fn defaults(&self) -> SegmentDefaults

Layout defaults (priority, width bounds, truncatable opt-in). User config may override each field via OverriddenSegment. Implementations must be O(1), do no I/O, and avoid allocation: the layout engine snapshots this at collect time and the [LineItem::Debug] impl reads it for dbg! / panic-backtrace formatting.

Implementors§