ctl-layout 0.7.1

Terminal widgets built on ctl-text: tables, records, rules, stacks, and indents
Documentation
//! One column of a table.

use ctl_text::{Overflow, Text};

use crate::align::Align;

/// A column of a table: its header, what it does with a cell wider than it
/// gets, and which edge its cells sit against.
///
/// The overflow applies to the header as well as to the cells.
#[derive(Clone, Debug)]
pub struct Column {
    pub(crate) header: Option<Text>,
    pub(crate) overflow: Overflow,
    pub(crate) align: Align,
}

impl Column {
    /// A column headed by `header`. A `&str` or a `String` is in the default
    /// style, and a [`Text`] keeps its own styles.
    #[must_use]
    pub fn new(header: impl Into<Text>) -> Self {
        Self::headed(Some(header.into()))
    }

    /// A column with no header. A table draws a header row only when at least
    /// one of its columns has a header. Then this column's header cell is
    /// empty.
    #[must_use]
    pub fn untitled() -> Self {
        Self::headed(None)
    }

    fn headed(header: Option<Text>) -> Self {
        Self {
            header,
            overflow: Overflow::default(),
            align: Align::default(),
        }
    }

    /// Sets what the column does with a cell wider than it gets: keep words
    /// whole, fold them, or cut the line with an ellipsis. The default is
    /// [`Overflow::KeepWords`].
    #[must_use]
    pub fn overflow(mut self, overflow: Overflow) -> Self {
        self.overflow = overflow;
        self
    }

    /// Aligns every cell of the column to the right, header included, so
    /// numbers line up on their last digit. This is [`align`](Self::align)
    /// with [`Align::Right`].
    #[must_use]
    pub fn numeric(self) -> Self {
        self.align(Align::Right)
    }

    /// Aligns every cell of the column, header included, against the edge
    /// that `align` selects. The default is [`Align::Left`].
    #[must_use]
    pub fn align(mut self, align: Align) -> Self {
        self.align = align;
        self
    }
}