Skip to content

Commit

Permalink
feat(block): support for having more than one title (ratatui#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
samyosm authored Jun 19, 2023
1 parent e869869 commit a04b190
Show file tree
Hide file tree
Showing 7 changed files with 510 additions and 82 deletions.
12 changes: 6 additions & 6 deletions examples/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use ratatui::{
layout::{Alignment, Constraint, Direction, Layout},
style::{Color, Modifier, Style},
text::Span,
widgets::{Block, BorderType, Borders, Padding, Paragraph},
widgets::{block::title::Title, Block, BorderType, Borders, Padding, Paragraph},
Frame, Terminal,
};

Expand Down Expand Up @@ -62,8 +62,7 @@ fn ui<B: Backend>(f: &mut Frame<B>) {
// Surrounding block
let block = Block::default()
.borders(Borders::ALL)
.title("Main block with round corners")
.title_alignment(Alignment::Center)
.title(Title::from("Main block with round corners").alignment(Alignment::Center))
.border_type(BorderType::Rounded);
f.render_widget(block, size);

Expand All @@ -89,15 +88,16 @@ fn ui<B: Backend>(f: &mut Frame<B>) {
f.render_widget(block, top_chunks[0]);

// Top right inner block with styled title aligned to the right
let block = Block::default()
.title(Span::styled(
let block = Block::default().title(
Title::from(Span::styled(
"Styled title",
Style::default()
.fg(Color::White)
.bg(Color::Red)
.add_modifier(Modifier::BOLD),
))
.title_alignment(Alignment::Right);
.alignment(Alignment::Right),
);
f.render_widget(block, top_chunks[1]);

// Bottom two inner blocks
Expand Down
6 changes: 2 additions & 4 deletions examples/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use ratatui::{
style::{Color, Modifier, Style},
symbols,
text::{Line, Span},
widgets::{Block, Gauge, LineGauge, List, ListItem, Paragraph, Widget},
widgets::{block::title::Title, Block, Gauge, LineGauge, List, ListItem, Paragraph, Widget},
Frame, Terminal, TerminalOptions, Viewport,
};

Expand Down Expand Up @@ -227,9 +227,7 @@ fn run_app<B: Backend>(
fn ui<B: Backend>(f: &mut Frame<B>, downloads: &Downloads) {
let size = f.size();

let block = Block::default()
.title("Progress")
.title_alignment(Alignment::Center);
let block = Block::default().title(Title::from("Progress").alignment(Alignment::Center));
f.render_widget(block, size);

let chunks = Layout::default()
Expand Down
2 changes: 1 addition & 1 deletion src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub struct Margin {
pub horizontal: u16,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Alignment {
Left,
Center,
Expand Down
57 changes: 57 additions & 0 deletions src/title.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use crate::{layout::Alignment, text::Line};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Title<'a> {
pub content: Line<'a>,
/// Defaults to Left if unset
pub alignment: Option<Alignment>,

/// Defaults to Top if unset
pub position: Option<Position>,
}

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Position {
#[default]
Top,
Bottom,
}

impl<'a> Title<'a> {
pub fn content<T>(mut self, content: T) -> Title<'a>
where
T: Into<Line<'a>>,
{
self.content = content.into();
self
}

pub fn alignment(mut self, alignment: Alignment) -> Title<'a> {
self.alignment = Some(alignment);
self
}

pub fn position(mut self, position: Position) -> Title<'a> {
self.position = Some(position);
self
}
}

impl<'a, T> From<T> for Title<'a>
where
T: Into<Line<'a>>,
{
fn from(value: T) -> Self {
Self::default().content(value.into())
}
}

impl<'a> Default for Title<'a> {
fn default() -> Self {
Self {
content: Line::from(""),
alignment: Some(Alignment::Left),
position: Some(Position::Top),
}
}
}
Loading

0 comments on commit a04b190

Please sign in to comment.