diff --git a/examples/demo2.sh b/examples/demo2.sh index db84f06..ee4da88 100755 --- a/examples/demo2.sh +++ b/examples/demo2.sh @@ -1,6 +1,7 @@ #!/usr/bin/env bash +echo -en "0" for i in `seq 1 100`; do - sleep 0.05s + sleep 0.005s echo -en "\n$i" done diff --git a/src/lib.rs b/src/lib.rs index c0c8f41..09544a2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -99,7 +99,7 @@ impl Multiview { /// Renders the (x, y) tile. pub fn render_tile_content(&mut self, (i, j): (u16, u16)) -> String { let tile = self.tile((i, j)); - tile.render_content() + tile.render_content(self.selected == (i, j)) } /// Renders all the tiles of the multiview. diff --git a/src/tile.rs b/src/tile.rs index 9437e56..b3fcc43 100644 --- a/src/tile.rs +++ b/src/tile.rs @@ -346,7 +346,7 @@ impl Tile { } /// Renders the content of the tile. - pub fn render_content(&self) -> String { + pub fn render_content(&self, selected: bool) -> String { const DELETE_CHAR: char = ' '; let (x, y) = self.inner_position; @@ -509,6 +509,51 @@ impl Tile { buffer.push(spaces); } + // Render scrollbar,thanks @gdamms + // I have no idea what this code does, I copied/pasted it from gdamms, and then modified + // some stuff so that it would look right + if last_line_index > h { + let mut subbuffer = vec![]; + subbuffer.push(format!( + "{}{}{}{}", + style::Reset, + if selected { color::Green.fg_str() } else { "" }, + cursor::Goto(x + w + 1, y), + "▲" + )); + + let bar_portion = h as f32 / self.stdout.len() as f32; + let bar_nb = f32::max(1.0, (bar_portion * (h) as f32).round()) as u16; + let max_scroll = self.stdout.len() as isize - h as isize - 1; + + let (scroll_nb_bottom, scroll_nb_top) = if self.scroll > max_scroll / 2 { + let scroll_nb_bottom = (self.stdout.len() as isize - self.scroll) as u16 - h; + let scroll_nb_bottom = scroll_nb_bottom as f32 / self.stdout.len() as f32; + let scroll_nb_bottom = (scroll_nb_bottom * (h as f32)).ceil() as u16; + let scroll_nb_top = h - bar_nb - scroll_nb_bottom; + (scroll_nb_bottom, scroll_nb_top) + } else { + let scroll_nb_top = self.scroll as f32 / self.stdout.len() as f32; + let scroll_nb_top = (scroll_nb_top * (h) as f32).ceil() as u16; + let scroll_nb_bottom = h - bar_nb - scroll_nb_top; + (scroll_nb_bottom, scroll_nb_top) + }; + + for i in 1..=scroll_nb_top { + subbuffer.push(format!("{}{}", cursor::Goto(x + w + 1, y + i), "│")); + } + for i in scroll_nb_top + 1..=scroll_nb_top + bar_nb { + subbuffer.push(format!("{}{}", cursor::Goto(x + w + 1, y + i), "█")); + } + for i in scroll_nb_top + bar_nb + 1..=scroll_nb_top + bar_nb + scroll_nb_bottom { + subbuffer.push(format!("{}{}", cursor::Goto(x + w + 1, y + i), "│")); + } + + subbuffer.push(format!("{}{}", cursor::Goto(x + w + 1, y + h), "▼")); + + buffer.push(subbuffer.join("")); + } + buffer.push(format!("{}", style::Reset)); buffer.join("") }