Skip to content

Commit

Permalink
break lines up on init
Browse files Browse the repository at this point in the history
  • Loading branch information
cartercanedy committed Sep 20, 2024
1 parent 4f1a444 commit 6726338
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions tui/src/floating_text.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(soft_unstable)]

use std::io::{Cursor, Read as _, Seek, SeekFrom, Write as _};

use crate::{
Expand Down Expand Up @@ -28,9 +30,8 @@ pub enum FloatingTextMode {
}

pub struct FloatingText {
src: String,
pub src: Vec<String>,
scroll: usize,
n_lines: usize,
mode: FloatingTextMode,
}

Expand Down Expand Up @@ -111,14 +112,9 @@ fn get_highlighted_string(s: &str) -> Option<String> {

impl FloatingText {
pub fn new(text: String, mode: FloatingTextMode) -> Self {
let mut n_lines = 0;

text.split("\n").for_each(|_| n_lines += 1);

Self {
src: text,
src: text.split("\n").map(|s| s.to_string()).collect(),
scroll: 0,
n_lines,
mode,
}
}
Expand Down Expand Up @@ -147,7 +143,7 @@ impl FloatingText {
}

fn scroll_down(&mut self) {
if self.scroll + 1 < self.n_lines {
if self.scroll + 1 < self.src.len() {
self.scroll += 1;
}
}
Expand Down Expand Up @@ -179,12 +175,11 @@ impl FloatContent for FloatingText {

// Calculate the inner area to ensure text is not drawn over the border
let inner_area = block.inner(area);
let Rect { height, .. } = inner_area;
let lines = self
.src
.lines()
.iter()
.skip(self.scroll)
.take(height as usize)
.take(inner_area.height as usize)
.map(|l| l.into_text().unwrap())
.collect::<Vec<_>>();

Expand Down

0 comments on commit 6726338

Please sign in to comment.