src/widgets/text_list.rs

262311623d24bff1c8fa9dbee99aa8e32cbc9cee
youtube-tui/src/widgets/text_list.rs history · blame · raw

149 lines · 4090 bytes

  1use tui::{
  2    buffer::Buffer,
  3    layout::Rect,
  4    style::{Color, Style},
  5    symbols::line::{
  6        HORIZONTAL, ROUNDED_BOTTOM_LEFT, ROUNDED_BOTTOM_RIGHT, ROUNDED_TOP_LEFT, ROUNDED_TOP_RIGHT,
  7        VERTICAL,
  8    },
  9    widgets::Widget,
 10};
 11
 12#[derive(Debug, Clone)]
 13pub struct TextList {
 14    pub items: Vec<String>,
 15    pub selected: usize,
 16    pub scroll: usize,
 17    pub area: Option<Rect>,
 18}
 19
 20impl Widget for TextList {
 21    fn render(self, area: Rect, buf: &mut Buffer) {
 22        if self.items.len() == 0 {
 23            return;
 24        }
 25
 26        let width_minus_2 = (area.width - 2) as usize;
 27        let width_minus_5 = (area.width - 5) as usize;
 28        let mut y = area.y;
 29
 30        for (i, mut item) in self
 31            .items
 32            .into_iter()
 33            .enumerate()
 34            .skip(self.scroll)
 35            .take(area.height as usize - 2)
 36        {
 37            if item.len() > width_minus_2 {
 38                item = format!("{}...", &item[0..width_minus_5]);
 39            }
 40
 41            if i == self.selected {
 42                let horizontal = HORIZONTAL.to_string().repeat(width_minus_2);
 43
 44                let lines = format!(
 45                    "{}{}{}\n{}{}{}{}\n{}{}{}",
 46                    ROUNDED_TOP_LEFT,
 47                    horizontal,
 48                    ROUNDED_TOP_RIGHT,
 49                    VERTICAL,
 50                    item,
 51                    " ".repeat(area.width as usize - 2 - item.len()),
 52                    VERTICAL,
 53                    ROUNDED_BOTTOM_LEFT,
 54                    horizontal,
 55                    ROUNDED_BOTTOM_RIGHT,
 56                );
 57
 58                let color = Color::LightYellow;
 59
 60                let mut rect = Rect {
 61                    x: area.x,
 62                    y: y,
 63                    width: area.width,
 64                    height: 1,
 65                };
 66                buf.set_style(rect, Style::default().fg(color));
 67
 68                rect = Rect {
 69                    x: area.x,
 70                    y: y + 1,
 71                    width: 1,
 72                    height: 1,
 73                };
 74                buf.set_style(rect, Style::default().fg(color));
 75
 76                rect = Rect {
 77                    x: area.x + area.width - 1,
 78                    y: y + 1,
 79                    width: 1,
 80                    height: 1,
 81                };
 82                buf.set_style(rect, Style::default().fg(color));
 83
 84                rect = Rect {
 85                    x: area.x,
 86                    y: y + 2,
 87                    width: area.width,
 88                    height: 1,
 89                };
 90                buf.set_style(rect, Style::default().fg(color));
 91
 92                for line in lines.lines() {
 93                    for (x, c) in (0..area.width).zip(line.chars()) {
 94                        buf.get_mut(area.x + x, y).set_char(c);
 95                    }
 96                    y += 1;
 97                }
 98            } else {
 99                for (x, c) in (0..area.width).zip(item.chars()) {
100                    buf.get_mut(area.x + x + 1, y).set_char(c);
101                }
102                y += 1;
103            }
104        }
105    }
106}
107
108impl Default for TextList {
109    fn default() -> Self {
110        Self {
111            items: Vec::new(),
112            selected: 0,
113            scroll: 0,
114            area: None,
115        }
116    }
117}
118
119impl TextList {
120    pub fn area(&mut self, area: Rect) {
121        self.area = Some(area);
122    }
123
124    pub fn items(&mut self, items: Vec<String>) {
125        self.items = items;
126    }
127
128    pub fn down(&mut self) {
129        if self.selected < self.items.len() - 1 {
130            self.selected += 1;
131
132            if let Some(area) = self.area {
133                if self.selected > self.scroll + area.height as usize - 3 {
134                    //panic!("Selected");
135                    self.scroll = self.selected + 3 - area.height as usize;
136                }
137            }
138        }
139    }
140
141    pub fn up(&mut self) {
142        if self.selected > 0 {
143            self.selected -= 1;
144            if self.selected < self.scroll {
145                self.scroll = self.selected;
146            }
147        }
148    }
149}