Type Alias StyleFunc

Source
pub type StyleFunc = fn(&dyn Children, usize) -> Style;
Expand description

Function type for generating styles based on child position.

A StyleFunc dynamically determines the styling to apply to tree elements based on the child’s position within its parent’s children collection. This allows for positional styling like alternating colors, highlighting specific indices, or applying different styles to first/last children.

§Arguments

  • children - The children collection containing the styled element
  • index - The zero-based index of the current child being styled

§Returns

A Style object with the desired formatting for this position

§Examples

use lipgloss::{Style, Color};
use lipgloss_tree::{Children, StyleFunc};

// Alternating colors based on index
let alternating_style: StyleFunc = |_, index| {
    if index % 2 == 0 {
        Style::new().foreground(Color::from("blue"))
    } else {
        Style::new().foreground(Color::from("green"))
    }
};

// Highlight first and last items
let highlight_ends: StyleFunc = |children, index| {
    if index == 0 || index == children.length() - 1 {
        Style::new().bold(true).foreground(Color::from("red"))
    } else {
        Style::new()
    }
};