src/widgets/horizontal_split.rs

46e7ed5b32a7417aabe4491e60a597344597d330
youtube-tui/src/widgets/horizontal_split.rs history · blame · raw

168 lines · 4627 bytes

  1use tui::{
  2    buffer::Buffer,
  3    layout::Rect,
  4    style::Style,
  5    symbols::line::{
  6        HORIZONTAL, HORIZONTAL_DOWN, HORIZONTAL_UP, ROUNDED_BOTTOM_LEFT, ROUNDED_BOTTOM_RIGHT,
  7        ROUNDED_TOP_LEFT, ROUNDED_TOP_RIGHT, VERTICAL,
  8    },
  9    widgets::Widget,
 10};
 11
 12#[derive(Debug, Clone)]
 13pub struct HorizontalSplit {
 14    pub percentages: Vec<usize>,
 15    pub border_style: Style,
 16}
 17
 18impl Widget for HorizontalSplit {
 19    fn render(self, rect: Rect, buffer: &mut Buffer) {
 20        let mut width_left = rect.width as usize;
 21
 22        let widths = self
 23            .percentages
 24            .iter()
 25            .enumerate()
 26            .map(|(i, p)| {
 27                if i == self.percentages.len() - 1 {
 28                    return width_left;
 29                }
 30                let width = p * rect.width as usize / 100;
 31                width_left -= width;
 32                width
 33            })
 34            .collect::<Vec<_>>();
 35
 36        let mut top = String::new();
 37        let mut mid = String::new();
 38        let mut bottom = String::new();
 39
 40        buffer.set_style(Rect { height: 1, ..rect }, self.border_style);
 41        buffer.set_style(
 42            Rect {
 43                height: 1,
 44                y: rect.y + rect.height - 1,
 45                ..rect
 46            },
 47            self.border_style,
 48        );
 49
 50        buffer.set_style(
 51            Rect {
 52                x: rect.x + rect.width - 1,
 53                y: rect.y + 1,
 54                width: 1,
 55                height: rect.height - 2,
 56            },
 57            self.border_style,
 58        );
 59
 60        let mut x = rect.x;
 61
 62        for (index, width) in widths.iter().enumerate() {
 63            buffer.set_style(
 64                Rect {
 65                    x,
 66                    y: rect.y + 1,
 67                    width: 1,
 68                    height: rect.height - 2,
 69                },
 70                self.border_style,
 71            );
 72
 73            x += *width as u16;
 74
 75            if index == 0 {
 76                top.push_str(ROUNDED_TOP_LEFT);
 77                bottom.push_str(ROUNDED_BOTTOM_LEFT);
 78            } else {
 79                top.push_str(HORIZONTAL_DOWN);
 80                bottom.push_str(HORIZONTAL_UP);
 81            }
 82
 83            mid.push_str(VERTICAL);
 84
 85            if index == widths.len() - 1 {
 86                top.push_str(&HORIZONTAL.repeat(*width - 2));
 87                mid.push_str(&" ".repeat(*width - 2));
 88                bottom.push_str(&HORIZONTAL.repeat(*width - 2));
 89
 90                top.push_str(ROUNDED_TOP_RIGHT);
 91                mid.push_str(VERTICAL);
 92                bottom.push_str(ROUNDED_BOTTOM_RIGHT);
 93            } else {
 94                top.push_str(&HORIZONTAL.repeat(*width - 1));
 95                mid.push_str(&" ".repeat(*width - 1));
 96                bottom.push_str(&HORIZONTAL.repeat(*width - 1));
 97            }
 98        }
 99
100        buffer.set_string(rect.x, rect.y, &top, Style::default());
101
102        for y in rect.y + 1..rect.y + rect.height - 1 {
103            buffer.set_string(rect.x, y, &mid, Style::default());
104        }
105
106        buffer.set_string(rect.x, rect.y + rect.height - 1, &bottom, Style::default());
107    }
108}
109
110impl Default for HorizontalSplit {
111    fn default() -> Self {
112        HorizontalSplit {
113            percentages: vec![50, 50],
114            border_style: Style::default(),
115        }
116    }
117}
118
119impl HorizontalSplit {
120    pub fn inner(&self, rect: Rect) -> Vec<Rect> {
121        let mut width_left = rect.width as usize;
122
123        let widths = self
124            .percentages
125            .iter()
126            .enumerate()
127            .map(|(i, p)| {
128                if i == self.percentages.len() - 1 {
129                    return width_left;
130                }
131                let width = p * rect.width as usize / 100;
132                width_left -= width;
133                width
134            })
135            .collect::<Vec<_>>();
136
137        let mut x = rect.x as usize + 1;
138
139        widths
140            .iter()
141            .enumerate()
142            .map(|(i, width)| {
143                let cell_rect = Rect {
144                    x: x as u16,
145                    y: rect.y + 1,
146                    width: *width as u16 - if i == widths.len() - 1 { 2 } else { 1 },
147                    height: rect.height - 2,
148                };
149                x += width;
150                cell_rect
151            })
152            .collect::<Vec<_>>()
153    }
154
155    pub fn percentages(self, percentages: Vec<usize>) -> Self {
156        Self {
157            percentages,
158            ..self
159        }
160    }
161
162    pub fn border_style(self, border_style: Style) -> Self {
163        Self {
164            border_style,
165            ..self
166        }
167    }
168}