70 lines
1.4 KiB
Rust
70 lines
1.4 KiB
Rust
use crate::widget::Widget;
|
|
|
|
pub struct ScrollState<const N: usize>(pub usize, pub [usize; N]);
|
|
|
|
pub struct ScrollText {
|
|
pub scroll: usize,
|
|
pub title: String,
|
|
pub text: String,
|
|
}
|
|
|
|
impl Widget for ScrollText {
|
|
fn render(&self, scope: &mut crate::render::RenderScope) {
|
|
let title_lines = scope.draw_text(0, &self.title);
|
|
|
|
let text = self
|
|
.text
|
|
.lines()
|
|
.skip(self.scroll)
|
|
.map(|v| v.trim_end())
|
|
.collect::<Vec<_>>()
|
|
.join("\n");
|
|
|
|
scope.draw_text((0, title_lines), text);
|
|
}
|
|
}
|
|
|
|
impl<const N: usize> ScrollState<N> {
|
|
pub fn get_selected(&self, index: usize) -> &'static str {
|
|
if index == self.0 {
|
|
"\x1b[4m\x1b[1m"
|
|
} else {
|
|
"\x1b[1m"
|
|
}
|
|
}
|
|
|
|
pub fn scroll(&self, index: usize, title: String, text: String) -> ScrollText {
|
|
ScrollText {
|
|
scroll: self.1[index],
|
|
title,
|
|
text,
|
|
}
|
|
}
|
|
|
|
pub fn up(&mut self) {
|
|
if self.1[self.0] > 0 {
|
|
self.1[self.0] -= 1;
|
|
}
|
|
}
|
|
|
|
pub fn down(&mut self) {
|
|
self.1[self.0] += 1;
|
|
}
|
|
|
|
pub fn back_tab(&mut self) {
|
|
if self.0 > 0 {
|
|
self.0 -= 1;
|
|
} else {
|
|
self.0 = N - 1;
|
|
}
|
|
}
|
|
|
|
pub fn tab(&mut self) {
|
|
if self.0 + 1 < N {
|
|
self.0 += 1;
|
|
} else {
|
|
self.0 = 0;
|
|
}
|
|
}
|
|
}
|