Skip to content

Commit

Permalink
Add example with multiple variants
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp Reiter committed Sep 7, 2024
1 parent 73303e5 commit d1a1d69
Show file tree
Hide file tree
Showing 11 changed files with 381 additions and 214 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ name = "demo_legacy"
[[example]]
name = "simple_legacy"

[[example]]
name = "variants"

[features]
default = ["unstable-widget-ref"]
unstable-widget-ref = ["ratatui/unstable-widget-ref"]
49 changes: 49 additions & 0 deletions examples/common/item_container.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use ratatui::{
buffer::Buffer,
layout::Rect,
style::{Color, Style, Styled},
text::Line,
widgets::{Padding, Widget},
};
#[allow(dead_code)]
pub type PaddedLine<'a> = ListItemContainer<'a, Line<'a>>;

pub struct ListItemContainer<'a, W> {
child: W,
block: ratatui::widgets::Block<'a>,
style: Style,
}

impl<'a, W> ListItemContainer<'a, W> {
pub fn new(child: W, padding: Padding) -> Self {
let block = ratatui::widgets::Block::default().padding(padding);
let style = Style::default().fg(Color::White);
Self {
child,
block,
style,
}
}
}

impl<T> Styled for ListItemContainer<'_, T> {
type Item = Self;

fn style(&self) -> Style {
self.style
}

fn set_style<S: Into<Style>>(mut self, style: S) -> Self::Item {
self.style = style.into();
self
}
}

impl<W: Widget> Widget for ListItemContainer<'_, W> {
fn render(self, area: Rect, buf: &mut Buffer) {
let inner_area = self.block.inner(area);
buf.set_style(area, self.style);
self.block.render(area, buf);
self.child.render(inner_area, buf);
}
}
30 changes: 3 additions & 27 deletions examples/common/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
pub mod item_container;
#[allow(unused_imports)]
use crossterm::{
event::{DisableMouseCapture, EnableMouseCapture},
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::{
buffer::Buffer,
layout::Rect,
prelude::CrosstermBackend,
style::{Color, Style},
text::Line,
widgets::{BorderType, Borders, Padding, StatefulWidget, Widget},
widgets::{BorderType, Borders, StatefulWidget},
};
use std::{
error::Error,
Expand All @@ -29,29 +28,6 @@ impl Colors {
pub const TEAL: Color = Color::Rgb(0, 128, 128);
}

pub struct PaddedLine<'a> {
pub line: Line<'a>,
pub block: ratatui::widgets::Block<'a>,
pub style: Style,
}

impl<'a> PaddedLine<'a> {
pub fn new(line: Line<'a>, padding: Padding) -> Self {
let block = ratatui::widgets::Block::default().padding(padding);
let style = Style::default().fg(Color::White);
Self { line, block, style }
}
}

impl Widget for PaddedLine<'_> {
fn render(self, area: Rect, buf: &mut Buffer) {
let inner_area = self.block.inner(area);
buf.set_style(area, self.style);
self.block.render(area, buf);
self.line.render(inner_area, buf);
}
}

pub struct Block;
impl Block {
pub fn disabled() -> ratatui::widgets::Block<'static> {
Expand Down
187 changes: 0 additions & 187 deletions examples/scroll_config.rs

This file was deleted.

Binary file added examples/tapes/variants.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions examples/tapes/variants.tape
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# To run this script, install vhs and run `vhs ./examples/tapes/variants.tape`
Output "examples/tapes/variants.gif"
Set Margin 10
Set Padding 2
Set BorderRadius 10
Set Width 2000
Set Height 1100
Set PlaybackSpeed 1.0

Hide
Type "cargo run --example variants"
Enter
Sleep 0.5s
Show

Sleep 1.5s
Right@0.5s 1
Down@0.5s 15

Left@0.5s 1
Down@0.5s 1

Right@0.5s 1
Down@0.5s 20

Left@0.5s 1
Down@0.5s 1

Right@0.5s 1
Down@0.5s 10

Sleep 1.5s
Loading

0 comments on commit d1a1d69

Please sign in to comment.