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